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;

#[allow(dead_code)]
fn default_config(_config: &TestRootConfig) -> Result<String, std::io::Error> {
    Ok("default config content".to_string())
}

tree_type! {
    TestRoot {
        #[default(default_config)]
        config("config.toml")
    }
}

#[test]
fn test_ensure_creates_file_with_default() {
    let temp_dir = TempDir::new().unwrap();
    let root = TestRoot::new(temp_dir.path()).unwrap();

    // File doesn't exist yet
    assert!(!root.config().exists(), "Config should not exist initially");

    // Call sync - should create the file with default content
    let result = root.sync();
    assert!(result.is_ok(), "Sync should succeed: {:?}", result);

    // File should now exist with default content
    assert!(root.config().exists(), "Config should exist after ensure");

    let content = std::fs::read_to_string(root.config().as_path()).unwrap();
    assert_eq!(
        content, "default config content",
        "Should have default content"
    );
}

#[test]
fn test_ensure_skips_existing_file_with_default() {
    let temp_dir = TempDir::new().unwrap();
    let root = TestRoot::new(temp_dir.path()).unwrap();

    // Create file with custom content
    std::fs::write(root.config().as_path(), "custom content").unwrap();

    // Call sync - should NOT overwrite existing file
    let result = root.sync();
    assert!(result.is_ok(), "Sync should succeed: {:?}", result);

    // File should still have custom content
    let content = std::fs::read_to_string(root.config().as_path()).unwrap();
    assert_eq!(
        content, "custom content",
        "Should preserve existing content"
    );
}