normal-path
A Rust library to ensure paths are normalized without touching the filesystem.
This library provides two types, NormpathBuf and Normpath (akin to PathBuf and Path), for working with normalized paths abstractly.
Similar to the standard library, these types are thin wrappers around OsString and OsStr that represent normalized paths according to the local platform's path syntax.
Features
- No I/O: Normalizes paths purely on the string level, without touching the filesystem.
- Cross-platform: Handles Unix and Windows path semantics correctly.
- Type-checked: Ensures paths are absolute, canonical, and free of parent directory components (
..). - Normalization: Can normalize non-canonical patterns (like
//,./, trailing slashes, and on Windows, forward slashes and lowercase drive letters) into their canonical forms. - Serde support: Optional
serdefeature for serialization and deserialization.
Example
use ;
// Validate an already normalized path
let path = validate.unwrap;
// Normalize a path with non-canonical patterns
let normalized = normalize.unwrap;
assert_eq!;
Normalization Rules
In general, a path is considered normalized if and only if:
- It is absolute, i.e., independent of the current directory.
- It is canonical, meaning it does not contain any pattern that is considered non-canonical and has a corresponding canonical form depending on the platform.
- It does not contain any parent directory component (
..).
A normalized path is not necessarily the "real" path to the filesystem object it denotes, which may not even exist. Instead, it defines a path unambiguously on the string level, unaffected by the state of the filesystem.
Canonicality
These patterns are considered non-canonical on both Unix and Windows:
- Multiple consecutive slashes (
foo//bar). - Trailing slashes (
foo/bar/). - Current directory components (
foo/.or./foo).
Specifically on Windows, the following patterns are also considered non-canonical:
- Forward slashes (
D:\foo/baror//./COM11). - Lowercase drive letters (
d:\). - Parent directory components (that can be normalized).
Handling Parent Directories
On Unix, any parent component is an error, since it is impossible to eliminate them without filesystem access.
Windows, on the other hand, collapses parent components lexically before walking the filesystem (unless the path starts with \\?\).
Therefore, it is only a hard error if a parent component points outside of the base directory, like C:\...
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.