diff --git a/src/context.rs b/src/context.rs index 322267d..6874f7b 100644 --- a/src/context.rs +++ b/src/context.rs @@ -48,7 +48,7 @@ impl Context { } } - pub fn from_files<'a, T: AsRef>( + pub fn from_files<'a, T: AsRef + std::fmt::Debug>( files: Vec<&PathBuf>, file_path: &'a FilePath, ) -> Result { diff --git a/src/file_path.rs b/src/file_path.rs index 0298482..92a615e 100644 --- a/src/file_path.rs +++ b/src/file_path.rs @@ -16,7 +16,7 @@ impl<'a, P: AsRef + 'a> FilePath<'a, P> { pub fn new(input: &'a P, output: &'a P) -> Self { Self { input: Self::make_recursive(input), - root_dir: Self::strip_wildcards(input), + root_dir: Self::strip_wildcards(Self::strip_dot(&input)), output, slug_word_limit: Default::default(), } @@ -30,6 +30,10 @@ impl<'a, P: AsRef + 'a> FilePath<'a, P> { !path.as_ref().contains("*") } + fn strip_dot(path: &P) -> &Path { + path.as_ref().strip_prefix("./").unwrap_or(path.as_ref()) + } + fn strip_wildcards + ?Sized>(path: &'a P2) -> PathBuf { path.as_ref() .ancestors() @@ -77,3 +81,20 @@ impl<'a, P: AsRef + 'a> FilePath<'a, P> { value.as_ref().strip_prefix(&self.root_dir) } } + +#[cfg(test)] +mod tests { + use std::path::Path; + + use super::FilePath; + + #[test] + fn relative_paths() { + let relative = FilePath::new(&"./in", &"./out"); + let bare = FilePath::new(&"in", &"out"); + let path = "in/nested/deeply/test.md" ; + let expected = Path::new("nested/deeply/test.md"); + assert_eq!(expected, relative.strip_root(&path).unwrap()); + assert_eq!(expected, bare.strip_root(&path).unwrap()); + } +}