use tempfile::TempDir;
use tree_type_proc_macro::tree_type;
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
struct Key(String);
impl std::fmt::Display for Key {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for Key {
type Err = std::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Key(s.to_string()))
}
}
tree_type! {
TestRoot {
tasks/ {
[id: String]/ as TaskDir {
metadata("metadata.toml")
}
[key: Key]/ as KeyDir {
}
}
}
}
#[test]
fn test_dynamic_id_compiles() {
let tempdir = TempDir::new().unwrap();
let root = TestRoot::new(tempdir.path()).unwrap();
let tasks = root.tasks();
assert_eq!(tasks.as_path(), tempdir.path().join("tasks"));
}
#[test]
fn test_dynamic_id_manual_access() {
let tempdir = TempDir::new().unwrap();
let root = TestRoot::new(tempdir.path()).unwrap();
root.create_all().unwrap();
let task1 = root.tasks().id("task-1");
task1.create_all().unwrap();
assert!(task1.exists());
assert_eq!(task1.as_path(), tempdir.path().join("tasks/task-1"));
}
#[test]
fn test_dynamic_id_non_string_key() {
let tempdir = TempDir::new().unwrap();
let root = TestRoot::new(tempdir.path()).unwrap();
root.create_all().unwrap();
let key_alpha = root.tasks().key(Key("key-alpha".to_string()));
key_alpha.create_all().unwrap();
assert!(key_alpha.exists());
assert_eq!(key_alpha.as_path(), tempdir.path().join("tasks/key-alpha"));
}