Skip to main content

knave_expand/diagnostic/
counted.rs

1pub struct Counted;
2
3impl Counted {
4    pub fn noun(count: usize, singular: &str) -> String {
5        if count == 1 {
6            format!("{count} {singular}")
7        } else {
8            format!("{count} {singular}s")
9        }
10    }
11
12    pub fn was_or_were(count: usize) -> &'static str {
13        if count == 1 { "was" } else { "were" }
14    }
15}
16
17#[cfg(test)]
18mod tests {
19    use super::Counted;
20
21    #[test]
22    fn one_item_is_singular() {
23        assert_eq!(Counted::noun(1, "argument"), "1 argument");
24        assert_eq!(Counted::was_or_were(1), "was");
25    }
26
27    #[test]
28    fn other_counts_are_plural() {
29        assert_eq!(Counted::noun(0, "argument"), "0 arguments");
30        assert_eq!(Counted::was_or_were(2), "were");
31    }
32}