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

#623 in Filesystem

Download history 96/week @ 2024-11-13 44/week @ 2024-11-20 76/week @ 2024-11-27 178/week @ 2024-12-04 128/week @ 2024-12-11 110/week @ 2024-12-18 149/week @ 2024-12-25 75/week @ 2025-01-01 94/week @ 2025-01-08 83/week @ 2025-01-15 118/week @ 2025-01-22 259/week @ 2025-01-29 241/week @ 2025-02-05 94/week @ 2025-02-12 265/week @ 2025-02-19 315/week @ 2025-02-26

959 downloads per month
Used in 5 crates (4 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