Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company đŸ‘‡

4.0 KB
Raw Blame
// ©AngelaMos | 2026
// format.rs

const GROUP: usize = 3;
const SECONDS_PER_MINUTE: u64 = 60;
const SECONDS_PER_HOUR: u64 = 3600;
const THOUSAND: f64 = 1000.0;
const MILLION: f64 = 1_000_000.0;

#[must_use]
pub fn count(value: u64) -> String {
    let digits = value.to_string();
    let mut grouped = String::with_capacity(digits.len() + digits.len() / GROUP);
    for (index, digit) in digits.chars().enumerate() {
        if index > 0 && (digits.len() - index).is_multiple_of(GROUP) {
            grouped.push(',');
        }
        grouped.push(digit);
    }
    grouped
}

#[must_use]
pub fn rate(value: f64) -> String {
    if !value.is_finite() || value < 0.0 {
        return "-".to_owned();
    }
    if value >= MILLION {
        return format!("{:.2}M/s", value / MILLION);
    }
    if value >= THOUSAND {
        return format!("{:.1}k/s", value / THOUSAND);
    }
    format!("{value:.0}/s")
}

#[must_use]
pub fn duration(secs: u64) -> String {
    let hours = secs / SECONDS_PER_HOUR;
    let minutes = (secs % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE;
    let seconds = secs % SECONDS_PER_MINUTE;
    if hours > 0 {
        return format!("{hours}h {minutes:02}m {seconds:02}s");
    }
    if minutes > 0 {
        return format!("{minutes}m {seconds:02}s");
    }
    format!("{seconds}s")
}

#[must_use]
pub fn percent(ratio: f64) -> String {
    format!("{:.2}%", ratio * 100.0)
}

#[must_use]
pub fn density(already_a_percentage: f64) -> String {
    format!("{already_a_percentage:.2}%")
}

#[must_use]
pub fn bar_value(value: f64) -> u64 {
    if !value.is_finite() || value <= 0.0 {
        return 0;
    }
    #[expect(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
    let rounded = value.round() as u64;
    rounded
}

#[must_use]
pub fn bytes(value: usize) -> String {
    format!("{} B", count(value as u64))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn counts_are_grouped_from_the_right() {
        assert_eq!(count(0), "0");
        assert_eq!(count(7), "7");
        assert_eq!(count(999), "999");
        assert_eq!(count(1_000), "1,000");
        assert_eq!(count(34_283_406), "34,283,406");
        assert_eq!(count(u64::MAX), "18,446,744,073,709,551,615");
    }

    #[test]
    fn rates_shorten_only_once_they_would_stop_fitting() {
        assert_eq!(rate(0.0), "0/s");
        assert_eq!(rate(999.4), "999/s");
        assert_eq!(rate(1_000.0), "1.0k/s");
        assert_eq!(rate(71_170.98), "71.2k/s");
        assert_eq!(rate(2_500_000.0), "2.50M/s");
    }

    #[test]
    fn a_rate_that_is_not_a_number_renders_as_unknown_rather_than_nan() {
        assert_eq!(rate(f64::NAN), "-");
        assert_eq!(rate(f64::INFINITY), "-");
        assert_eq!(rate(-1.0), "-");
    }

    #[test]
    fn durations_drop_the_units_they_do_not_need() {
        assert_eq!(duration(0), "0s");
        assert_eq!(duration(45), "45s");
        assert_eq!(duration(600), "10m 00s");
        assert_eq!(duration(3_661), "1h 01m 01s");
        assert_eq!(duration(86_400), "24h 00m 00s");
    }

    #[test]
    fn a_ratio_renders_as_the_percentage_the_stats_file_printed() {
        assert_eq!(percent(13_647.0 / 69_140.0), "19.74%");
        assert_eq!(percent(0.0), "0.00%");
        assert_eq!(percent(1.0), "100.00%");
    }

    #[test]
    fn a_value_already_in_percent_is_not_multiplied_by_a_hundred_again() {
        assert_eq!(
            density(19.74),
            "19.74%",
            "fuzzer_stats reports bitmap_cvg as 0..100 while edge_ratio is 0..1; feeding the former to percent() prints 1974.00%"
        );
        assert_eq!(percent(0.1974), "19.74%");
        assert_ne!(density(19.74), percent(19.74));
    }

    #[test]
    fn a_bar_value_that_is_not_a_number_is_zero_rather_than_a_panic() {
        assert_eq!(bar_value(f64::NAN), 0);
        assert_eq!(bar_value(-1.0), 0);
        assert_eq!(bar_value(f64::INFINITY), 0);
        assert_eq!(bar_value(71_170.98), 71_171);
    }

    #[test]
    fn byte_counts_are_grouped_like_every_other_count() {
        assert_eq!(bytes(0), "0 B");
        assert_eq!(bytes(4_096), "4,096 B");
    }
}