waddling-errors 0.7.3

Structured, secure-by-default diagnostic codes for distributed systems with no_std and role-based documentation
Documentation
//! Example: Compile-time enriched error documentation
//!
//! This demonstrates how to capture compile-time metadata when registering errors.

use waddling_errors::{Code, ComponentId, PrimaryId, Severity};

#[cfg(feature = "doc-gen")]
use waddling_errors::doc_generator::DocRegistry;

#[derive(Debug, Copy, Clone)]
enum Component {
    Auth,
}

impl ComponentId for Component {
    fn as_str(&self) -> &'static str {
        "AUTH"
    }
}

#[derive(Debug, Copy, Clone)]
enum Primary {
    Token,
}

impl PrimaryId for Primary {
    fn as_str(&self) -> &'static str {
        "TOKEN"
    }
}

const ERR_TOKEN_MISSING: Code<Component, Primary> =
    Code::new(Severity::Error, Component::Auth, Primary::Token, 1);

/// Macro to capture compile-time metadata when registering errors
#[cfg(feature = "doc-gen")]
macro_rules! register_with_metadata {
    ($registry:expr, $code:expr, $desc:expr, $hints:expr) => {{
        let _ = $registry.register_code_extended(
            &$code,
            $desc,
            $hints,
            &[],
            None, // role
            &[],  // related_codes
            None, // deprecated
            &[],  // see_also
        );

        // Capture compile-time metadata
        let source_info = format!(
            "Defined in {} at line {} (built with rustc {})",
            file!(),
            line!(),
            env!("CARGO_PKG_RUST_VERSION")
        );

        let build_info = format!(
            "Package: {} v{} (git: {})",
            env!("CARGO_PKG_NAME"),
            env!("CARGO_PKG_VERSION"),
            option_env!("GIT_HASH").unwrap_or("unknown")
        );

        println!("📍 {}", source_info);
        println!("🔨 {}", build_info);
    }};
}

fn main() {
    #[cfg(feature = "doc-gen")]
    {
        let mut registry = DocRegistry::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));

        println!("=== Registering Errors with Compile-Time Metadata ===\n");

        register_with_metadata!(
            registry,
            ERR_TOKEN_MISSING,
            "JWT token missing from Authorization header",
            &[
                "Include Authorization header",
                "Check authentication middleware"
            ]
        );

        // Show what metadata we captured
        println!("\n=== Available Compile-Time Constants ===");
        println!("CARGO_PKG_NAME:        {}", env!("CARGO_PKG_NAME"));
        println!("CARGO_PKG_VERSION:     {}", env!("CARGO_PKG_VERSION"));
        println!("CARGO_PKG_AUTHORS:     {}", env!("CARGO_PKG_AUTHORS"));
        println!("CARGO_PKG_DESCRIPTION: {}", env!("CARGO_PKG_DESCRIPTION"));
        println!("CARGO_PKG_HOMEPAGE:    {}", env!("CARGO_PKG_HOMEPAGE"));
        println!("CARGO_PKG_REPOSITORY:  {}", env!("CARGO_PKG_REPOSITORY"));

        #[cfg(debug_assertions)]
        println!("\nBuild Mode:            DEBUG");
        #[cfg(not(debug_assertions))]
        println!("\nBuild Mode:            RELEASE");

        println!("Target OS:             {}", std::env::consts::OS);
        println!("Target Arch:           {}", std::env::consts::ARCH);
        println!("Target Family:         {}", std::env::consts::FAMILY);

        // These would require build.rs to set
        if let Some(git_hash) = option_env!("GIT_HASH") {
            println!("Git Commit:            {}", git_hash);
        }
        if let Some(build_time) = option_env!("BUILD_TIMESTAMP") {
            println!("Build Time:            {}", build_time);
        }

        println!("\n=== Future: Runtime Enrichment ===");
        println!("At error occurrence, we could capture:");
        println!("  • Timestamp and duration since app start");
        println!("  • Thread ID and stack trace");
        println!("  • Memory usage and allocation count");
        println!("  • Request ID / Correlation ID");
        println!("  • User context (if available)");
        println!("  • Previous errors in causal chain");
    }

    #[cfg(not(feature = "doc-gen"))]
    {
        eprintln!("⚠️  Run with --features doc-gen to see compile-time metadata");
    }
}