2022-06-18 21:56:31 -07:00
|
|
|
use super::*;
|
2019-10-09 01:40:40 -07:00
|
|
|
|
|
|
|
pub struct Count<T: Display>(pub T, pub usize);
|
|
|
|
|
|
|
|
impl<T: Display> Display for Count<T> {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
if self.1 == 1 {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
} else {
|
|
|
|
write!(f, "{}s", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn count() {
|
|
|
|
assert_eq!(Count("dog", 0).to_string(), "dogs");
|
|
|
|
assert_eq!(Count("dog", 1).to_string(), "dog");
|
|
|
|
assert_eq!(Count("dog", 2).to_string(), "dogs");
|
|
|
|
}
|
|
|
|
}
|