Skip to main content

just_lsp/
count.rs

1use super::*;
2
3pub struct Count<T: Display>(pub T, pub usize);
4
5impl<T: Display> Display for Count<T> {
6  fn fmt(&self, f: &mut Formatter) -> fmt::Result {
7    if self.1 == 1 {
8      write!(f, "{}", self.0)
9    } else {
10      write!(f, "{}s", self.0)
11    }
12  }
13}
14
15#[cfg(test)]
16mod tests {
17  use super::*;
18
19  #[test]
20  fn count() {
21    assert_eq!(Count("dog", 0).to_string(), "dogs");
22    assert_eq!(Count("dog", 1).to_string(), "dog");
23    assert_eq!(Count("dog", 2).to_string(), "dogs");
24  }
25}