Julio Biason
6 months ago
4 changed files with 71 additions and 0 deletions
@ -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" |
@ -0,0 +1,6 @@
|
||||
[package] |
||||
name = "pathextractiontest" |
||||
version = "0.1.0" |
||||
edition = "2021" |
||||
|
||||
[dependencies] |
@ -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. |
@ -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…
Reference in new issue