1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use super::*;

pub(crate) trait Tally {
  fn tally(self, count: usize) -> Tallied;
}

impl Tally for &'static str {
  fn tally(self, count: usize) -> Tallied {
    Tallied { noun: self, count }
  }
}

pub(crate) struct Tallied {
  count: usize,
  noun: &'static str,
}

impl Display for Tallied {
  fn fmt(&self, f: &mut Formatter) -> fmt::Result {
    if self.count == 1 {
      write!(f, "{} {}", self.count, self.noun)
    } else {
      write!(f, "{} {}s", self.count, self.noun)
    }
  }
}

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

  #[test]
  fn zero() {
    assert_eq!("foo".tally(0).to_string(), "0 foos")
  }

  #[test]
  fn one() {
    assert_eq!("foo".tally(1).to_string(), "1 foo")
  }

  #[test]
  fn two() {
    assert_eq!("foo".tally(2).to_string(), "2 foos")
  }
}