derive-defs 0.0.8

Library for generating derive preset macros from TOML configuration
Documentation
//! Inheritance example: Demonstrates `extends` functionality.
//!
//! This example shows how definitions can inherit from other definitions
//! using the `extends` keyword.
//!
//! # Running the example
//!
//! ```bash
//! cargo run --example inheritance
//! ```

use std::fs;

fn main() {
    println!("Inheritance Example");
    println!("===================");
    println!();

    // Create a TOML configuration with inheritance chain
    let config = r#"
# Base definitions
[defs.debug]
traits = ["Debug"]

[defs.cloneable]
extends = "debug"
traits = ["Clone"]

[defs.comparable]
extends = "cloneable"
traits = ["PartialEq", "Eq"]

[defs.hashable]
extends = "comparable"
traits = ["Hash"]

[defs.value_object]
extends = "hashable"
traits = ["Copy"]
"#;

    let temp_dir = std::env::temp_dir();
    let config_path = temp_dir.join("derive_defs_inheritance_example.toml");
    fs::write(&config_path, config).expect("Failed to write config");

    println!("Configuration:");
    println!("{config}");
    println!();

    // Parse and resolve
    let parsed = derive_defs::parser::parse_file(&config_path).expect("Failed to parse");
    let resolved = derive_defs::resolver::resolve(&parsed).expect("Failed to resolve");

    println!("Inheritance chain results:");
    println!();

    // Show the inheritance chain
    let chain = [
        "debug",
        "cloneable",
        "comparable",
        "hashable",
        "value_object",
    ];
    for name in &chain {
        if let Some(def) = resolved.defs.get(*name) {
            println!("[defs.{name}]");
            println!("  Final traits: {:?}", def.traits);
            println!("  Trait count: {}", def.traits.len());
            println!();
        }
    }

    // Verify the chain
    let value_object = resolved.defs.get("value_object").unwrap();
    assert_eq!(
        value_object.traits,
        vec!["Debug", "Clone", "PartialEq", "Eq", "Hash", "Copy"]
    );

    println!("Verification passed!");
    println!("'value_object' correctly inherits all traits from the chain.");

    // Cleanup
    let _ = fs::remove_file(&config_path);
}