diff options
| author | A Farzat <a@farzat.xyz> | 2026-06-02 13:25:52 +0300 |
|---|---|---|
| committer | A Farzat <a@farzat.xyz> | 2026-06-02 13:25:52 +0300 |
| commit | 648142a37c36c1061117eb2d7c7f334a74b4bbc1 (patch) | |
| tree | 08b1433ba750f9ec1f9d00474eff414d978ed818 /src/lib.rs | |
| parent | 9dc2499bcdaa961a42ccee59025d6d52a296bbdf (diff) | |
| download | repo2markdown-648142a37c36c1061117eb2d7c7f334a74b4bbc1.tar.gz repo2markdown-648142a37c36c1061117eb2d7c7f334a74b4bbc1.zip | |
Add basic path normalization relative to origin
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 35 |
1 files changed, 28 insertions, 7 deletions
@@ -1,4 +1,4 @@ -use std::path::{Path, PathBuf}; +use std::path::{Path, PathBuf, Component}; #[derive(Debug)] pub enum NormalizeError { @@ -7,13 +7,24 @@ pub enum NormalizeError { pub fn normalize_path( _root: &Path, - _origin: &Path, + origin: &Path, input: &Path, ) -> Result<PathBuf, NormalizeError> { if input.as_os_str().is_empty() { return Err(NormalizeError::EmptyInput); } - Ok(input.to_path_buf()) + let mut stack = Vec::new(); + let origin_joint_input = origin.join(input); + for component in origin_joint_input.components() { + match component { + Component::CurDir => (), + Component::ParentDir => { stack.pop(); }, + Component::Prefix(_) => { stack.push(component); }, + Component::Normal(_) => { stack.push(component); }, + Component::RootDir => { stack.push(component) }, + } + } + Ok(PathBuf::from_iter(stack)) } #[cfg(test)] @@ -24,8 +35,8 @@ mod tests { #[test] fn empty_path_returns_error() { - let root = Path::new("/project"); - let origin_dir = Path::new("/project"); + let root = Path::new(""); + let origin_dir = Path::new(""); let input = Path::new(""); let result = normalize_path(root, origin_dir, input); assert!(matches!(result, Err(NormalizeError::EmptyInput))); @@ -33,11 +44,21 @@ mod tests { #[test] fn plain_filename_with_root_at_cwd_returns_filename() { - let root = Path::new("/project"); - let origin_dir = Path::new("/project"); + let root = Path::new(""); + let origin_dir = Path::new(""); let input = Path::new("main.rs"); let result = normalize_path(root, origin_dir, input); assert!(result.is_ok()); assert_eq!(result.unwrap(), Path::new("main.rs")); } + + #[test] + fn relative_path_from_origin_is_resolved() { + let root = Path::new(""); + let origin_dir = Path::new("src"); + let input = Path::new("../main.rs"); + let result = normalize_path(root, origin_dir, input); + assert!(result.is_ok()); + assert_eq!(result.unwrap(), Path::new("main.rs")); + } } |
