forge-guard 0.1.1

Pre-deployment smart contract auditing framework for Foundry
Documentation
//! `forge gas` — analyze gas usage.

use super::GasArgs;
use anyhow::{Context, Result};
use colored::*;

/// Analyze gas usage of smart contracts.
pub fn run(args: &GasArgs) -> Result<()> {
    eprintln!("{}", "⛽ Forge Guard — Gas Analysis".bold());

    // Run forge snapshot for gas report
    let mut cmd = std::process::Command::new("forge");
    cmd.arg("snapshot");

    if let Some(contract) = &args.contract {
        cmd.arg("--match-contract").arg(contract);
    }

    if args.all {
        cmd.arg("--all");
    }

    eprintln!("\n🚀 Running gas analysis...\n");
    let output = cmd.output().context("Failed to run forge snapshot")?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("Gas analysis failed: {}", stderr);
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    println!("{}", stdout);

    // Parse and highlight high gas usage
    eprintln!("{}", "\n── Gas Summary ──".bold());
    eprintln!(
        "  ⚠️  Functions exceeding {} gas threshold will need optimization",
        args.warn_threshold
    );

    for line in stdout.lines() {
        if let Some(gas_str) = line.split('|').nth(2) {
            if let Ok(gas) = gas_str.trim().replace(',', "").parse::<u64>() {
                if gas > args.warn_threshold {
                    println!(
                        "  {} {}{} gas",
                        "⚠️".yellow(),
                        line.split('|').next().unwrap_or("?").trim(),
                        gas
                    );
                }
            }
        }
    }

    if let Some(diff_path) = &args.diff {
        eprintln!("\n📊 Gas diff against {} requested", diff_path);
        // Future: implement diff comparison
    }

    eprintln!("\n{}", "✅ Gas analysis complete.".green().bold());
    Ok(())
}