sigmd 0.1.0

Windows API signature metadata
Documentation
//! Metadata generator entry point.

mod commands;
mod compiler;
mod config;

use std::path::PathBuf;

use anyhow::{Context as _, Error};
use clap::{ArgAction, Parser, Subcommand};
use tracing_subscriber::EnvFilter;

/// CLI argument parsing.
#[derive(Parser, Debug)]
#[command(author, version, about)]
struct Cli {
    /// Path to the YAML config file.
    #[arg(short, long, value_name = "FILE", env = "SIGMD_CONFIG")]
    config: PathBuf,

    /// Increase verbosity (-v, -vv, -vvv).
    #[arg(short, long, action = ArgAction::Count, group = "logging")]
    verbose: u8,

    /// Suppress output below ERROR.
    #[arg(short, long, group = "logging")]
    quiet: bool,

    /// Subcommand to run.
    #[command(subcommand)]
    command: Command,
}

/// Subcommands for the CLI.
#[derive(Subcommand, Debug)]
enum Command {
    /// Run the full multi-TU build and write the metadata bundle.
    Build(commands::build::BuildArgs),

    /// Parse a single translation unit and dump its `ScoredMetadata` as JSON.
    Compile(commands::compile::CompileArgs),
}

/// Main entry point for the CLI.
pub fn run() -> Result<(), Error> {
    let cli = Cli::parse();

    let default_level = match (cli.verbose, cli.quiet) {
        (_, true) => "error",
        (0, _) | (1, _) => "info",
        (2, _) => "debug",
        _ => "trace",
    };

    let filter =
        EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(default_level));

    tracing_subscriber::fmt()
        .with_writer(std::io::stderr)
        .with_env_filter(filter)
        .with_target(false)
        .init();

    let config = config::load(&cli.config)
        .with_context(|| format!("loading config: {}", cli.config.display()))?;

    match cli.command {
        Command::Build(args) => commands::build::run(config, args),
        Command::Compile(args) => commands::compile::run(config, args),
    }
}