use console::style;
use super::metrics::SizeMetrics;
use crate::fmt::{format_bytes, CHART, SPARKLES};
pub struct ResultFormatter;
impl ResultFormatter {
pub fn print_summary(metrics: &SizeMetrics) {
println!("\n{} {} Build Summary", CHART, style("📈").bold());
println!(
" {} Before: {}",
style("→").dim(),
style(format_bytes(metrics.before_bytes)).yellow()
);
println!(
" {} After: {}",
style("→").dim(),
style(format_bytes(metrics.after_bytes)).green().bold()
);
let reduction = metrics.reduction_bytes();
let reduction_pct = metrics.reduction_percent();
if reduction > 0 {
println!(
" {} Saved: {} ({:.1}% reduction)",
style("→").dim(),
style(format_bytes(reduction as u64)).green().bold(),
reduction_pct
);
} else {
println!(" {} No size reduction", style("→").dim());
}
println!(
"\n{} {} Build complete!",
SPARKLES,
style("Success!").green().bold()
);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_result_formatter_exists() {
ResultFormatter::print_summary(&SizeMetrics {
before_bytes: 1000,
after_bytes: 500,
});
}
#[test]
fn test_format_with_unicode_content() {
let metrics = SizeMetrics {
before_bytes: 500_000,
after_bytes: 250_000,
};
ResultFormatter::print_summary(&metrics);
}
#[test]
fn test_format_with_extremely_large_numbers() {
let metrics = SizeMetrics {
before_bytes: 5_000_000_000, after_bytes: 2_500_000_000, };
ResultFormatter::print_summary(&metrics);
}
#[test]
fn test_format_with_zero_reduction() {
let metrics = SizeMetrics {
before_bytes: 1000,
after_bytes: 1000,
};
ResultFormatter::print_summary(&metrics);
}
#[test]
fn test_format_with_size_increase() {
let metrics = SizeMetrics {
before_bytes: 1000,
after_bytes: 1500,
};
ResultFormatter::print_summary(&metrics);
}
#[test]
fn test_format_with_very_small_sizes() {
let metrics = SizeMetrics {
before_bytes: 100,
after_bytes: 50,
};
ResultFormatter::print_summary(&metrics);
}
#[test]
fn test_format_with_zero_before_size() {
let metrics = SizeMetrics {
before_bytes: 0,
after_bytes: 0,
};
ResultFormatter::print_summary(&metrics);
}
#[test]
fn test_format_with_one_byte() {
let metrics = SizeMetrics {
before_bytes: 1,
after_bytes: 0,
};
ResultFormatter::print_summary(&metrics);
}
#[test]
fn test_format_with_exact_half_reduction() {
let metrics = SizeMetrics {
before_bytes: 2000,
after_bytes: 1000,
};
ResultFormatter::print_summary(&metrics);
assert_eq!(metrics.reduction_percent(), 50.0);
}
#[test]
fn test_format_with_99_percent_reduction() {
let metrics = SizeMetrics {
before_bytes: 100_000,
after_bytes: 1_000,
};
ResultFormatter::print_summary(&metrics);
assert!(metrics.reduction_percent() > 90.0);
}
}