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! {
    ParentTestRoot {
        config/ as ConfigDir {
            settings("settings.toml") as SettingsFile
        }
        src/ as SrcDir {
            main("main.rs") as MainFile
        }
    }
}

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

    // Test that parent method exists on root (returns Option<GenericDir>)
    let parent_result = root.parent();
    // For a root created from tempdir, parent should exist
    assert!(parent_result.is_some());
}

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

    // Create the config directory and settings file
    std::fs::create_dir_all(tempdir.path().join("config")).unwrap();
    std::fs::write(tempdir.path().join("config/settings.toml"), "test").unwrap();

    // Test file parent method (now returns exact parent type ConfigDir)
    let config = root.config();
    let settings = config.settings();
    let parent = settings.parent();

    // Verify the parent has the correct path - now type-safe!
    assert_eq!(parent.as_path(), config.as_path());
}

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

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

    // Test directory parent method (now returns exact parent type ParentTestRoot)
    let config = root.config();
    let parent = config.parent();

    // Verify the parent has the correct path - now type-safe!
    assert_eq!(parent.as_path(), root.as_path());
}

#[test]
fn test_generic_parent_methods() {
    use tree_type::GenericDir;
    use tree_type::GenericFile;

    let tempdir = TempDir::new().unwrap();

    // Create a test file
    let test_file_path = tempdir.path().join("test.txt");
    std::fs::write(&test_file_path, "content").unwrap();

    // Test GenericFile parent method - files always have a parent directory
    let generic_file = GenericFile::new(&test_file_path).unwrap();
    let file_parent = generic_file.parent();
    assert_eq!(file_parent.as_path(), tempdir.path());

    // Test GenericDir parent method - directories may not have a parent
    let generic_dir = GenericDir::new(tempdir.path()).unwrap();
    let dir_parent = generic_dir.parent();
    // Should have a parent (the parent of tempdir)
    assert!(dir_parent.is_some());
}