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

#487 in Filesystem

Download history 9/week @ 2024-07-20 14/week @ 2024-07-27 29/week @ 2024-08-03 49/week @ 2024-08-10 13/week @ 2024-08-17 8/week @ 2024-08-24 38/week @ 2024-08-31 61/week @ 2024-09-07 41/week @ 2024-09-14 55/week @ 2024-09-21 40/week @ 2024-09-28 17/week @ 2024-10-05 34/week @ 2024-10-12 35/week @ 2024-10-19 18/week @ 2024-10-26 54/week @ 2024-11-02

141 downloads per month
Used in 4 crates

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