4 releases (1 stable)

1.0.0 Jan 2, 2024
0.3.1 Jan 13, 2023
0.3.0 Jan 13, 2023
0.2.0 Jan 13, 2023
0.1.0 Jan 13, 2023

#538 in Filesystem

Download history 24/week @ 2024-10-11 44/week @ 2024-10-18 14/week @ 2024-10-25 43/week @ 2024-11-01 140/week @ 2024-11-08 75/week @ 2024-11-15 42/week @ 2024-11-22 168/week @ 2024-11-29 124/week @ 2024-12-06 104/week @ 2024-12-13 187/week @ 2024-12-20 53/week @ 2024-12-27 92/week @ 2025-01-03 103/week @ 2025-01-10 88/week @ 2025-01-17 145/week @ 2025-01-24

429 downloads per month
Used in 6 crates (5 directly)

Apache-2.0

7KB

pathbuf

pathbuf is a simple crate which provides the pathbuf macro to conveniently construct the Rust PathBuf type.

Example

use pathbuf::pathbuf;
use std::path::PathBuf;

fn main() {
	let p = pathbuf!["hello", "filename.txt"];

	let expected = {
		let mut temp = PathBuf::new();
		temp.push("hello");
		temp.push("filename.txt");
		temp
	};

	assert_eq!(p, expected);
}

License

pathbuf is licensed under the Apache 2.0 license, and is itself a reproduction of the hc_pathbuf crate found in Hipcheck, pulled out into its own distinct crate for reuse.


lib.rs:

pathbuf provides a single macro, pathbuf!, which gives a vec!-like syntax for constructing PathBufs.

Example

#
fn do_something(dir: &Path) {
    let file_name = pathbuf![dir, "filename.txt"];

    if file_name.exists() {
        // do something...
    }
}

Security

As the macro relies on std::path::PathBuf::push there is also no protection against path traversal attacks. Therefore no path element shall be untrusted user input without validation or sanitisation.

An example for a path traversal/override on an UNIX system:

#
let user_input = "/etc/shadow";
assert_eq!(pathbuf!["/tmp", user_input], PathBuf::from("/etc/shadow"));

No runtime deps