//! Counting, kept apart from reading so it can be tested. mod parse; /// What a text holds. #[derive(Debug, Default, PartialEq)] pub struct Counts { pub lines: usize, pub words: usize, pub bytes: usize, } /// Counts `text` in one pass. pub fn count(text: &str) -> Counts { let mut counts = Counts { bytes: text.len(), ..Counts::default() }; for line in parse::lines(text) { counts.lines += 1; counts.words += parse::words(line); } counts } #[cfg(test)] mod tests { use super::*; #[test] fn counts() { let c = count("one two\nthree\n"); assert_eq!(c, Counts { lines: 2, words: 3, bytes: 14 }); } #[test] fn a_last_line_without_a_newline() { assert_eq!(count("a\nb").lines, 2); } }