forge-guard 0.3.6

Pre-deployment smart contract auditing framework for Foundry
Documentation
//! Formatting utilities for output display.

/// Format a duration in seconds to a human-readable string.
pub fn format_duration(secs: f64) -> String {
    if secs < 1.0 {
        format!("{:.0}ms", secs * 1000.0)
    } else if secs < 60.0 {
        format!("{:.1}s", secs)
    } else if secs < 3600.0 {
        let minutes = (secs / 60.0) as u64;
        let seconds = (secs % 60.0) as u64;
        format!("{}m {}s", minutes, seconds)
    } else {
        let hours = (secs / 3600.0) as u64;
        let minutes = ((secs % 3600.0) / 60.0) as u64;
        format!("{}h {}m", hours, minutes)
    }
}

/// Format a number with comma separators.
pub fn format_number(n: u64) -> String {
    let s = n.to_string();
    let mut result = String::new();
    for (i, c) in s.chars().rev().enumerate() {
        if i > 0 && i % 3 == 0 {
            result.insert(0, ',');
        }
        result.insert(0, c);
    }
    result
}

/// Format a percentage value.
pub fn format_pct(value: f64) -> String {
    format!("{:.1}%", value * 100.0)
}

/// Format bytes to a human-readable size.
pub fn format_bytes(bytes: u64) -> String {
    const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];
    let mut size = bytes as f64;
    let mut unit_idx = 0;

    while size >= 1024.0 && unit_idx < UNITS.len() - 1 {
        size /= 1024.0;
        unit_idx += 1;
    }

    if unit_idx == 0 {
        format!("{} {}", bytes, UNITS[unit_idx])
    } else {
        format!("{:.2} {}", size, UNITS[unit_idx])
    }
}