summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorA Farzat <a@farzat.xyz>2026-06-04 10:19:28 +0300
committerA Farzat <a@farzat.xyz>2026-06-04 10:19:28 +0300
commita2581e7ca0156599c19555efbc03160ad55bed72 (patch)
treeaeb132fb6da8be2d5a979cf6fd7f581d522f3ac9 /src
parente7910b55d1970979a857951f14e893d04d26d9f9 (diff)
downloadrepo2markdown-a2581e7ca0156599c19555efbc03160ad55bed72.tar.gz
repo2markdown-a2581e7ca0156599c19555efbc03160ad55bed72.zip
Make NormalizeError implement Error trait
Diffstat (limited to 'src')
-rw-r--r--src/normalizer.rs37
1 files changed, 32 insertions, 5 deletions
diff --git a/src/normalizer.rs b/src/normalizer.rs
index 0878126..d757512 100644
--- a/src/normalizer.rs
+++ b/src/normalizer.rs
@@ -1,3 +1,4 @@
+use core::fmt;
use std::{
env,
path::{Component, Path, PathBuf},
@@ -6,11 +7,32 @@ use std::{
#[derive(Debug)]
pub enum NormalizeError {
EmptyInput,
- EscapesFilesystemRoot,
+ EscapesFilesystemRoot(PathBuf),
FailedToGetCurDir,
- InvalidMultiplePrefix,
+ InvalidMultiplePrefix(PathBuf),
}
+impl fmt::Display for NormalizeError {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Self::EmptyInput => {
+ write!(f, "Encountered an empty filename")
+ }
+ Self::EscapesFilesystemRoot(path) => {
+ write!(f, "Path escapes filesystem root: {:?}", path)
+ }
+ Self::FailedToGetCurDir => {
+ write!(f, "Failed to get current directory")
+ }
+ Self::InvalidMultiplePrefix(path) => {
+ write!(f, "Path invalid multiple prefixes: {:?}", path)
+ }
+ }
+ }
+}
+
+impl std::error::Error for NormalizeError {}
+
pub struct Normalizer {
root: PathBuf,
origin_base: PathBuf,
@@ -86,12 +108,14 @@ fn normalize_components(path: &Path) -> Result<PathBuf, NormalizeError> {
for component in iter {
match component {
Component::RootDir => unreachable!(),
- Component::Prefix(_) => return Err(NormalizeError::InvalidMultiplePrefix),
+ Component::Prefix(_) => {
+ return Err(NormalizeError::InvalidMultiplePrefix(path.to_path_buf()));
+ }
Component::CurDir => continue,
Component::ParentDir => {
// It's an error if ParentDir causes us to go above the "root".
if normalized.as_os_str().len() == root {
- return Err(NormalizeError::EscapesFilesystemRoot);
+ return Err(NormalizeError::EscapesFilesystemRoot(path.to_path_buf()));
} else {
normalized.pop();
}
@@ -219,7 +243,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!(matches!(result, Err(NormalizeError::EscapesFilesystemRoot)));
+ assert!(matches!(
+ result,
+ Err(NormalizeError::EscapesFilesystemRoot(_))
+ ));
}
#[test]