diff --git a/pathextractiontest/Cargo.lock b/pathextractiontest/Cargo.lock new file mode 100644 index 0000000..c3f3e50 --- /dev/null +++ b/pathextractiontest/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "pathextractiontest" +version = "0.1.0" diff --git a/pathextractiontest/Cargo.toml b/pathextractiontest/Cargo.toml new file mode 100644 index 0000000..2a8f913 --- /dev/null +++ b/pathextractiontest/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "pathextractiontest" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/pathextractiontest/README.md b/pathextractiontest/README.md new file mode 100644 index 0000000..2015f7f --- /dev/null +++ b/pathextractiontest/README.md @@ -0,0 +1,12 @@ +# PathExtractTest + +Extracts some information from a directory based on its structure. + +The basic gist is: + +- There is a magic directory, called ".run". +- Things go as "something/.run" or "something/.run/special/path". +- If we send "something/.run", we need to get a pair with path "something" and + the name "default"; +- If we send "something/.run/special/path", we need to get a pair with path + "something and the name "path. diff --git a/pathextractiontest/src/main.rs b/pathextractiontest/src/main.rs new file mode 100644 index 0000000..2be2cac --- /dev/null +++ b/pathextractiontest/src/main.rs @@ -0,0 +1,46 @@ +use std::path::Path; + +fn extract<'a>( + path: &'a Path, + current_name: Option<&'a str>, +) -> Option<(&'a Path, Option<&'a str>)> { + let name = path.file_name().map(|x| x.to_str()).flatten(); + let parent = path.parent()?; + + // println!("path={path:?}, current_name={current_name:?}, parent={parent:?}, name={name:?}"); + + if name == Some(".run") { + Some((parent, current_name.or(Some("default")))) + } else { + extract(&parent, current_name.or(name)) + } +} + +fn main() { + println!("Hello, world!"); +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn base() { + let result = extract(Path::new("something/.run"), None).unwrap(); + assert_eq!(result.0, Path::new("something")); + assert_eq!(result.1, Some("default")); + } + + #[test] + fn child() { + let result = extract(Path::new("something/.run/special/path"), None).unwrap(); + assert_eq!(result.0, Path::new("something")); + assert_eq!(result.1, Some("path")); + } + + #[test] + fn broken() { + let result = extract(Path::new("something/fun/for/everyone"), None); + assert!(result.is_none()); + } +}