ferrotype 0.1.1

An opinionated wrapper for insta.rs
Documentation
//! Demonstrates ferrotype's feature-specific functionality
//!
//! This example shows the additional methods available when various features
//! are enabled. Run with:
//! ```
//! cargo run --example features --all-features
//! ```

use ferrotype::Ferrotype;

fn main() {
  println!("=== Ferrotype Features Example ===\n");

  let mut snapshot = Ferrotype::new();

  // Basic functionality (always available)
  snapshot.add("Title", "Feature showcase example".to_string());

  #[derive(Debug)]
  #[allow(unused)]
  struct ExampleData {
    name: String,
    values: Vec<i32>,
  }

  let data = ExampleData {
    name: "test data".to_string(),
    values: vec![1, 2, 3, 4, 5],
  };

  snapshot.add_debug("Debug Data", &data);

  // Feature: hex (binary data hex dumps)
  #[cfg(feature = "hex")]
  {
    println!("Feature 'hex' is enabled:");

    // Add a hex dump of some binary data
    let binary_data = b"Hello, World! \x00\x01\x02\x03\xFF\xFE\xFD";
    snapshot.add_hex("Binary Data", binary_data);

    // Example with larger binary data
    let packet_data: Vec<u8> = (0..64).collect();
    snapshot.add_hex("Network Packet", &packet_data);

    println!("✓ Added hex dumps\n");
  }
  #[cfg(not(feature = "hex"))]
  {
    println!("Feature 'hex' is not enabled");
    println!("  Run with: cargo run --example features --features hex\n");
  }

  // Feature: tokenstream (Rust code formatting)
  #[cfg(feature = "tokenstream")]
  {
    println!("Feature 'tokenstream' is enabled:");

    // Parse and format some Rust code
    let code = r#"
      fn calculate(x: i32, y: i32) -> i32 {
        let result = x + y;
        println!("Result: {}", result);
        result
      }
    "#;

    if let Ok(tokens) = code.parse::<proc_macro2::TokenStream>() {
      snapshot.add_token_stream("Generated Code", &tokens);
      println!("✓ Added formatted Rust code\n");
    }
  }
  #[cfg(not(feature = "tokenstream"))]
  {
    println!("Feature 'tokenstream' is not enabled");
    println!(
      "  Run with: cargo run --example features --features tokenstream\n"
    );
  }

  // Feature: anstream (ANSI color code stripping)
  #[cfg(feature = "anstream")]
  {
    println!("Feature 'anstream' is enabled:");

    // Add content with ANSI color codes
    let colored_output = "\x1b[32mSUCCESS\x1b[0m: All tests \x1b[1;32mpassed\x1b[0m! \x1b[31mErrors: 0\x1b[0m";
    snapshot.add_strip_str("Test Output", colored_output.to_string());

    // Terminal output with various control sequences
    let terminal_output = "Loading\x08\x08\x08\x08\x08\x08\x08Done!   \x1b[2K\x1b[1A\x1b[32m✓\x1b[0m Complete";
    snapshot.add_strip_str("Terminal Progress", terminal_output.to_string());

    println!("✓ Added content with ANSI codes stripped\n");
  }
  #[cfg(not(feature = "anstream"))]
  {
    println!("Feature 'anstream' is not enabled");
    println!("  Run with: cargo run --example features --features anstream\n");
  }

  // Feature: bluegum (tree rendering)
  #[cfg(feature = "bluegum")]
  {
    println!("Feature 'bluegum' is enabled:");

    // Define a simple tree structure
    #[derive(Debug)]
    struct TreeNode {
      name: String,
      children: Vec<TreeNode>,
    }

    impl bluegum::Bluegum for TreeNode {
      fn node(&self, builder: &mut bluegum::Builder) {
        builder.name(&self.name);
        for (i, child) in self.children.iter().enumerate() {
          builder.add_node(&format!("child_{i}"), child);
        }
      }
    }

    let tree = TreeNode {
      name: "Root".to_string(),
      children: vec![
        TreeNode {
          name: "Branch A".to_string(),
          children: vec![
            TreeNode {
              name: "Leaf 1".to_string(),
              children: vec![],
            },
            TreeNode {
              name: "Leaf 2".to_string(),
              children: vec![],
            },
          ],
        },
        TreeNode {
          name: "Branch B".to_string(),
          children: vec![TreeNode {
            name: "Leaf 3".to_string(),
            children: vec![],
          }],
        },
      ],
    };

    snapshot.add_bluegum("Tree Structure", &tree);

    // Using the builder directly
    snapshot.add_bluegum_builder_with("Custom Tree", |b| {
      b.name("CustomRoot")
        .field("version", "1.0")
        .field("status", "active");
    });

    println!("✓ Added tree visualizations\n");
  }
  #[cfg(not(feature = "bluegum"))]
  {
    println!("Feature 'bluegum' is not enabled");
    println!("  Run with: cargo run --example features --features bluegum\n");
  }

  // Print the complete snapshot
  println!("=== Complete Snapshot ===");
  snapshot.print();
  println!("=========================\n");

  // Show feature detection
  println!("Feature Summary:");
  println!(
    "  hex:         {}",
    if cfg!(feature = "hex") { "" } else { "" }
  );
  println!(
    "  tokenstream: {}",
    if cfg!(feature = "tokenstream") {
      ""
    } else {
      ""
    }
  );
  println!(
    "  anstream:    {}",
    if cfg!(feature = "anstream") {
      ""
    } else {
      ""
    }
  );
  println!(
    "  bluegum:     {}",
    if cfg!(feature = "bluegum") {
      ""
    } else {
      ""
    }
  );

  println!("\nTo enable all features, run:");
  println!("  cargo run --example features --all-features");
}