diff options
| -rw-r--r-- | src/md_generator.rs | 4 | ||||
| -rw-r--r-- | src/normalizer.rs | 33 | ||||
| -rw-r--r-- | src/renderer.rs | 10 |
3 files changed, 28 insertions, 19 deletions
diff --git a/src/md_generator.rs b/src/md_generator.rs index 98ab22d..54b7cbb 100644 --- a/src/md_generator.rs +++ b/src/md_generator.rs @@ -41,10 +41,10 @@ pub fn generate_markdown_from_paths<R: Read, W: Write>( let path = Path::new(OsStr::from_bytes(segment)); let normalized_path = normalizer.normalize(path)?; - if !seen_paths.insert(normalized_path.relative.clone()) { + if !seen_paths.insert(normalized_path.clone()) { logger.warn(format!( "skipping duplicate file: {}", - display_path(&normalized_path.relative) + display_path(&normalized_path.root_relative) )); continue; } diff --git a/src/normalizer.rs b/src/normalizer.rs index 116cca1..3ad3da2 100644 --- a/src/normalizer.rs +++ b/src/normalizer.rs @@ -4,12 +4,12 @@ use std::{ path::{Component, Path, PathBuf}, }; -#[derive(PartialEq, Eq, Hash)] +#[derive(PartialEq, Eq, Hash, Clone)] pub struct NormalizedPath { /// Path normalized and made absolute, suitable for filesystem access pub absolute: PathBuf, - /// Path normalized and made relative to root, used as identifier - pub relative: PathBuf, + /// Path normalized and made relative to root, used as identifier and for display + pub root_relative: PathBuf, } #[derive(Debug)] @@ -66,7 +66,7 @@ impl Normalizer { let input = absolutize(input, &self.origin_base); let normalized_input = normalize_components(&input)?; Ok(NormalizedPath { - relative: make_relative_to_root(&normalized_input, &self.root), + root_relative: make_relative_to_root(&normalized_input, &self.root), absolute: normalized_input, }) } @@ -184,7 +184,7 @@ mod tests { let fake_cwd = Path::new("/sandbox"); let normalizer = Normalizer::new_with_cwd(Path::new(""), Path::new(""), fake_cwd).unwrap(); let result = normalizer.normalize(Path::new("main.rs")); - assert_eq!(result.unwrap().relative, Path::new("main.rs")); + assert_eq!(result.unwrap().root_relative, Path::new("main.rs")); } #[test] @@ -194,7 +194,7 @@ mod tests { let origin_base = Path::new("src"); let normalizer = Normalizer::new_with_cwd(root, origin_base, fake_cwd).unwrap(); let result = normalizer.normalize(Path::new("../main.rs")); - assert_eq!(result.unwrap().relative, Path::new("main.rs")); + assert_eq!(result.unwrap().root_relative, Path::new("main.rs")); } #[test] @@ -204,7 +204,7 @@ mod tests { let origin_base = Path::new("/project/src"); let normalizer = Normalizer::new_with_cwd(root, origin_base, fake_cwd).unwrap(); let result = normalizer.normalize(Path::new("main.rs")); - assert_eq!(result.unwrap().relative, Path::new("src/main.rs")); + assert_eq!(result.unwrap().root_relative, Path::new("src/main.rs")); } #[test] @@ -214,7 +214,10 @@ mod tests { let origin_base = Path::new("/outside"); let normalizer = Normalizer::new_with_cwd(root, origin_base, fake_cwd).unwrap(); let result = normalizer.normalize(Path::new("main.rs")); - assert_eq!(result.unwrap().relative, Path::new("../outside/main.rs")); + assert_eq!( + result.unwrap().root_relative, + Path::new("../outside/main.rs") + ); } #[test] @@ -224,7 +227,10 @@ mod tests { let origin_base = Path::new("/sandbox/outside"); let normalizer = Normalizer::new_with_cwd(root, origin_base, fake_cwd).unwrap(); let result = normalizer.normalize(Path::new("main.rs")); - assert_eq!(result.unwrap().relative, Path::new("../outside/main.rs")); + assert_eq!( + result.unwrap().root_relative, + Path::new("../outside/main.rs") + ); } #[test] @@ -234,7 +240,7 @@ mod tests { let origin_base = Path::new("/sandbox/outside"); let normalizer = Normalizer::new_with_cwd(root, origin_base, fake_cwd).unwrap(); let result = normalizer.normalize(Path::new("/sandbox/main.rs")); - assert_eq!(result.unwrap().relative, Path::new("../main.rs")); + assert_eq!(result.unwrap().root_relative, Path::new("../main.rs")); } #[test] @@ -244,7 +250,10 @@ mod tests { let origin_base = Path::new("outside"); let normalizer = Normalizer::new_with_cwd(root, origin_base, fake_cwd).unwrap(); let result = normalizer.normalize(Path::new("main.rs")); - assert_eq!(result.unwrap().relative, Path::new("../outside/main.rs")); + assert_eq!( + result.unwrap().root_relative, + Path::new("../outside/main.rs") + ); } #[test] @@ -268,7 +277,7 @@ mod tests { let normalizer = Normalizer::new_with_cwd(root, origin_base, fake_cwd).unwrap(); let result = normalizer.normalize(Path::new("main.rs")); assert_eq!( - result.unwrap().relative, + result.unwrap().root_relative, Path::new("../sandbox/outside/main.rs") ); } diff --git a/src/renderer.rs b/src/renderer.rs index 1bb28fd..90162e6 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -65,11 +65,11 @@ impl<W: Write> Renderer<W> { pub fn render_path(&mut self, normalized_path: &NormalizedPath) -> std::io::Result<()> { let metadata = std::fs::metadata(&normalized_path.absolute)?; if metadata.len() > self.max_file_size { - self.warn_about_filesize(&normalized_path.relative, metadata.len()); - self.render_large_file(&normalized_path.relative) + self.warn_about_filesize(&normalized_path.root_relative, metadata.len()); + self.render_large_file(&normalized_path.root_relative) } else { let file = File::open(&normalized_path.absolute)?; - self.render_file(&normalized_path.relative, file) + self.render_file(&normalized_path.root_relative, file) } } @@ -357,7 +357,7 @@ mod tests { let content = "A".repeat(10); // 10 bytes -> bigger than the limit std::fs::write(&file_path, &content).unwrap(); let normalized_path = NormalizedPath { - relative: PathBuf::from("big.txt"), + root_relative: PathBuf::from("big.txt"), absolute: file_path, }; @@ -380,7 +380,7 @@ mod tests { let content = "A".repeat(10); // 10 bytes -> bigger than the limit std::fs::write(&file_path, &content).unwrap(); let normalized_path = NormalizedPath { - relative: PathBuf::from("big.txt"), + root_relative: PathBuf::from("big.txt"), absolute: file_path, }; |
