use std::{fmt::Display, ops::AddAssign}; #[derive(Debug, Default)] pub struct Stats { pub file_count: u32, pub failed_file_count: u32, pub sentence_count: u32, pub word_count: u32, pub capitalized_count: u32, pub numeric_count: u32, pub forbidden_count: u32, } impl AddAssign for Stats { fn add_assign(&mut self, rhs: Self) { self.file_count += rhs.file_count; self.failed_file_count += rhs.failed_file_count; self.sentence_count += rhs.sentence_count; self.word_count += rhs.word_count; self.capitalized_count += rhs.capitalized_count; self.numeric_count += rhs.numeric_count; self.forbidden_count += rhs.forbidden_count; } } impl Display for Stats { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { writeln!(f, "file count: {}", self.file_count)?; writeln!(f, "failed file count: {}", self.failed_file_count)?; writeln!(f, "sentence count: {}", self.sentence_count)?; writeln!(f, "word count: {}", self.word_count)?; writeln!(f, "capitalized count: {}", self.capitalized_count)?; writeln!(f, "numeric count: {}", self.numeric_count)?; writeln!(f, "forbidden count: {}", self.forbidden_count)?; let word_count = self.word_count as f32; writeln!( f, "words per sentence average: {:.1}", word_count / self.sentence_count as f32 )?; writeln!( f, "forbidden word percentage: {:.2}%", (self.forbidden_count as f32 / word_count) * 100.0, )?; write!( f, "capitalized word percentage: {:.2}%", (self.capitalized_count as f32 / word_count) * 100.0, ) } }