forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhumanize.rs
More file actions
44 lines (38 loc) · 1.01 KB
/
Copy pathhumanize.rs
File metadata and controls
44 lines (38 loc) · 1.01 KB
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
/// A trait to derive plural or singular representations from
pub trait Pluralizable {
fn to_plural(&self) -> String;
fn to_singular(&self) -> String;
}
impl Pluralizable for &str {
fn to_plural(&self) -> String {
if self.ends_with('s') {
self.to_string()
} else {
format!("{}s", self)
}
}
fn to_singular(&self) -> String {
if self.ends_with('s') {
self[0..self.len() - 1].to_string()
} else {
self.to_string()
}
}
}
// Impl Pluralizable for (singular, plural)
impl Pluralizable for (&str, &str) {
fn to_plural(&self) -> String {
self.1.to_string()
}
fn to_singular(&self) -> String {
self.0.to_string()
}
}
// Pluralize the given pluralizable if the `count` is greater than one.
pub fn pluralize_conditionally(pluralizable: impl Pluralizable, count: usize) -> String {
if count == 1 {
pluralizable.to_singular()
} else {
pluralizable.to_plural()
}
}