tree-type 0.4.5

Rust macros for creating type-safe filesystem tree structures
Documentation
#![allow(deprecated)]

use tempfile::TempDir;
use tree_type::tree_type;

tree_type! {
    ChildrenTestRoot {
        projects/ {
            [project_id: String]/ as ProjectDir {
                src/
                config("config.toml")
            }
        }
    }
}

#[test]
fn test_children_method() {
    let tempdir = TempDir::new().unwrap();
    let root = ChildrenTestRoot::new(tempdir.path()).unwrap();

    // Create some test directories
    std::fs::create_dir_all(tempdir.path().join("projects/project1")).unwrap();
    std::fs::create_dir_all(tempdir.path().join("projects/project2")).unwrap();

    let projects = root.projects();

    // Both V1 and V2 have identical children() method signatures
    let children: Vec<_> = projects
        .children()
        .unwrap()
        .collect::<Result<Vec<_>, _>>()
        .unwrap();
    assert_eq!(children.len(), 2);
}

#[test]
fn test_children_method_exists() {
    let tempdir = TempDir::new().unwrap();
    let root = ChildrenTestRoot::new(tempdir.path()).unwrap();

    // Create the projects directory
    std::fs::create_dir_all(tempdir.path().join("projects")).unwrap();

    // Test that children method exists and returns an iterator
    let projects = root.projects();
    let children_result = projects.children();
    assert!(children_result.is_ok());
    // Should be empty initially
    let children: Vec<_> = children_result.unwrap().collect();
    assert_eq!(children.len(), 0);
}

#[test]
fn test_children_method_with_directories() {
    let tempdir = TempDir::new().unwrap();
    let root = ChildrenTestRoot::new(tempdir.path()).unwrap();

    // Create the projects directory and some project subdirectories
    let projects_path = tempdir.path().join("projects");
    std::fs::create_dir_all(&projects_path).unwrap();
    std::fs::create_dir_all(projects_path.join("project-1")).unwrap();
    std::fs::create_dir_all(projects_path.join("project-2")).unwrap();
    std::fs::create_dir_all(projects_path.join("project-3")).unwrap();

    // Also create a file (should be ignored)
    std::fs::write(projects_path.join("readme.txt"), "test").unwrap();

    // Test children method
    let projects = root.projects();
    let children_result = projects.children();
    assert!(children_result.is_ok());

    let children: Result<Vec<_>, _> = children_result.unwrap().collect();
    assert!(children.is_ok());

    let children = children.unwrap();
    assert_eq!(children.len(), 3);

    // Verify the children are correctly typed ProjectDir instances
    let mut project_names: Vec<String> =
        children.iter().map(|project| project.file_name()).collect();
    project_names.sort();
    assert_eq!(project_names, vec!["project-1", "project-2", "project-3"]);
}

#[test]
fn test_children_method_error_handling() {
    let tempdir = TempDir::new().unwrap();
    let root = ChildrenTestRoot::new(tempdir.path()).unwrap();

    // Don't create the projects directory - should return error
    let projects = root.projects();
    let children_result = projects.children();
    assert!(children_result.is_err());
}

#[test]
fn test_children_method_with_mixed_entries() {
    let tempdir = TempDir::new().unwrap();
    let root = ChildrenTestRoot::new(tempdir.path()).unwrap();

    // Create the projects directory
    let projects_path = tempdir.path().join("projects");
    std::fs::create_dir_all(&projects_path).unwrap();

    // Create valid project directories
    std::fs::create_dir_all(projects_path.join("valid-project")).unwrap();

    // Create files (should be filtered out)
    std::fs::write(projects_path.join("file1.txt"), "content").unwrap();
    std::fs::write(projects_path.join("file2.md"), "content").unwrap();

    // Test children method
    let projects = root.projects();
    let children_result = projects.children();
    assert!(children_result.is_ok());

    let children: Result<Vec<_>, _> = children_result.unwrap().collect();
    assert!(children.is_ok());

    let children = children.unwrap();
    assert_eq!(children.len(), 1);
    assert_eq!(children[0].file_name(), "valid-project");
}