Browse Source

Playing with some path structure

master
Julio Biason 6 months ago
parent
commit
34c16c7323
  1. 7
      pathextractiontest/Cargo.lock
  2. 6
      pathextractiontest/Cargo.toml
  3. 12
      pathextractiontest/README.md
  4. 46
      pathextractiontest/src/main.rs

7
pathextractiontest/Cargo.lock generated

@ -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"

6
pathextractiontest/Cargo.toml

@ -0,0 +1,6 @@
[package]
name = "pathextractiontest"
version = "0.1.0"
edition = "2021"
[dependencies]

12
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.

46
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());
}
}
Loading…
Cancel
Save