diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-06-01 12:46:01 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-06-01 12:55:07 +0200 |
| commit | 7218892c722ca583297c0ebbda350bdf6f16d3ce (patch) | |
| tree | 27ebbfaf0662c1e0dd01e7c5e9e360ab288cae4d /src/util.rs | |
| parent | 9bdb0bdeffa5e4b6da9e3f6d3c1b79c506005fc5 (diff) | |
Refactor path handling
Diffstat (limited to 'src/util.rs')
| -rw-r--r-- | src/util.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs index 72db4518..8a8c04b6 100644 --- a/src/util.rs +++ b/src/util.rs @@ -2,6 +2,7 @@ use std::cmp::Ordering; use std::ops::Range; +use std::path::{Component, Path, PathBuf}; /// Additional methods for slices. pub trait SliceExt<T> { @@ -79,3 +80,28 @@ impl RangeExt for Range<usize> { } } } + +/// Additional methods for [`Path`]. +pub trait PathExt { + /// Lexically normalize a path. + fn normalize(&self) -> PathBuf; +} + +impl PathExt for Path { + fn normalize(&self) -> PathBuf { + let mut out = PathBuf::new(); + for component in self.components() { + match component { + Component::CurDir => {} + Component::ParentDir => match out.components().next_back() { + Some(Component::Normal(_)) => { + out.pop(); + } + _ => out.push(component), + }, + _ => out.push(component), + } + } + out + } +} |
