Crate path_abs [−] [src]
Absolute serializable path types and associated methods.
This library provides the following types:
- PathAbs
: an absolute (canonicalized) path that is guaranteed (when
created) to exist.
- PathFile
: a PathAbs
that is guaranteed to be a file, with
associated methods.
- PathDir
: a PathAbs
that is guaranteed to be a directory, with
associated methods.
- PathType
: an enum containing either a file or a directory. Returned
by PathDir::list
.
In addition, all types are serializable through serde (even on windows!) by using the crate
stfu8
to encode/decode, allowing ill-formed UTF-16.
See that crate for more details on how the resulting encoding can be edited (by hand)
even in the case of what would be ill-formed UTF-16.
Also see the project repo and consider leaving a star!
Examples
Recreating Cargo.init
in target/example
use std::collections::HashSet; use path_abs::{PathAbs, PathDir, PathFile, PathType}; let example = "target/example"; // Create your paths let project = PathDir::create_all(example).unwrap(); let src = PathDir::create(project.join("src")).unwrap(); let lib = PathFile::create(src.join("lib.rs")).unwrap(); let cargo = PathFile::create(project.join("Cargo.toml")).unwrap(); // Write the templates lib.write_str(r#" #[cfg(test)] mod tests { #[test] fn it_works() { assert_eq!(2 + 2, 4); } }"#).unwrap(); cargo.write_str(r#" [package] name = "example" version = "0.1.0" authors = ["Garrett Berg <googberg@gmail.com>"] [dependencies] "#).unwrap(); let mut result = HashSet::new(); for p in project.list().unwrap() { result.insert(p.unwrap()); } let mut expected = HashSet::new(); expected.insert(PathType::Dir(src)); expected.insert(PathType::File(cargo)); assert_eq!(expected, result); // Get a file let abs = PathAbs::new("target/example/src/lib.rs").unwrap(); // or get the file of known type let file = PathType::new("target/example/src/lib.rs") .unwrap() .unwrap_file(); // or use `into_file` let file2 = abs.clone().into_file().unwrap(); assert!(abs.is_file()); assert!(file.is_file()); assert!(file2.is_file());
Structs
PathAbs |
An absolute (canonicalized) path that is guaranteed (when created) to exist. |
PathDir |
A |
PathFile |
a |
Enums
PathType |
An an enum containing either a file or a directory. |