#path #platform-independent #component #interaction #unix-style #file

uni-path

Platform-independent path manipulation

2 stable releases

1.51.1 Apr 17, 2021

#646 in Filesystem

Download history 1/week @ 2023-12-18 6/week @ 2023-12-25 37/week @ 2024-01-15 41/week @ 2024-02-05 9/week @ 2024-02-12 12/week @ 2024-02-19 86/week @ 2024-02-26 72/week @ 2024-03-04 8/week @ 2024-03-11 29/week @ 2024-03-18 116/week @ 2024-03-25

225 downloads per month

MIT/Apache

76KB
1.5K SLoC

uni-path

Platform-independent Unix-style path manipulation.

Rationale

Rust's std::path module provides convenient way of path manipulation. It would be nice to use such paths not only with OS file system, but with virtual one (e.g. in-memory fs). Unfortunately, std::path is platform-dependent what means that its behavior is different on different platform.

About

This crate is very similar to std::path because its source code was simply copied from std::path implementation and only the following points were modified:

  • Remove all platform-dependent conditions and leave only Unix code.
  • Use str and String instead of OsStr and OsString.
  • Remove all interactions with OS file system.

lib.rs:

Platform-independent path manipulation.

This module provides two types, PathBuf and Path (akin to String and [str]), for working with paths abstractly. These types are thin wrappers around String and [str] respectively, meaning that they work directly on strings.

Paths can be parsed into Components by iterating over the structure returned by the components method on Path. Components roughly correspond to the substrings between path separators (/). You can reconstruct an equivalent path from components with the push method on PathBuf; note that the paths may differ syntactically by the normalization described in the documentation for the components method.

Simple usage

Path manipulation includes both parsing components from slices and building new owned paths.

To parse a path, you can create a Path slice from a [str] slice and start asking questions:

use uni_path::Path;

let path = Path::new("/tmp/foo/bar.txt");

let parent = path.parent();
assert_eq!(parent, Some(Path::new("/tmp/foo")));

let file_stem = path.file_stem();
assert_eq!(file_stem, Some("bar"));

let extension = path.extension();
assert_eq!(extension, Some("txt"));

To build or modify paths, use PathBuf:

use uni_path::PathBuf;

// This way works...
let mut path = PathBuf::from("/");

path.push("lib");
path.push("libc");

path.set_extension("so");

// ... but push is best used if you don't know everything up
// front. If you do, this way is better:
let path: PathBuf = ["/", "lib", "libc.so"].iter().collect();

No runtime deps