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

#540 in Rust patterns

37 downloads per month
Used in mdbook-readme-summary

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