proto_types/duration/
duration_units.rs

1#![allow(dead_code)]
2
3#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
4pub struct Seconds {
5  pub value: u64,
6}
7
8impl Seconds {
9  /// Returns a string displaying the amount of seconds (e.g. "1 second", "2 seconds")
10  pub fn format(&self) -> String {
11    format!(
12      "{} second{}",
13      self.value,
14      if self.value != 1 { "s" } else { "" }
15    )
16  }
17
18  /// Returns a string with the amount of seconds, but only if the amount is more than 0.
19  pub fn format_if_nonzero(&self) -> Option<String> {
20    if self.is_zero() {
21      return None;
22    }
23    Some(format!(
24      "{} second{}",
25      self.value,
26      if self.value != 1 { "s" } else { "" }
27    ))
28  }
29
30  pub fn is_zero(&self) -> bool {
31    self.value == 0
32  }
33}
34
35#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
36pub struct Minutes {
37  pub value: u64,
38}
39
40/// Returns a string displaying the amount of minutes (e.g. "1 minute", "2 minutes")
41impl Minutes {
42  pub fn format(&self) -> String {
43    format!(
44      "{} minute{}",
45      self.value,
46      if self.value != 1 { "s" } else { "" }
47    )
48  }
49
50  /// Returns a string with the amount of minutes, but only if the amount is more than 0.
51  pub fn format_if_nonzero(&self) -> Option<String> {
52    if self.is_zero() {
53      return None;
54    }
55    Some(format!(
56      "{} minute{}",
57      self.value,
58      if self.value != 1 { "s" } else { "" }
59    ))
60  }
61
62  pub fn is_zero(&self) -> bool {
63    self.value == 0
64  }
65}
66
67#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
68pub struct Hours {
69  pub value: u64,
70}
71
72/// Returns a string displaying the amount of hours (e.g. "1 hour", "2 hours")
73impl Hours {
74  pub fn format(&self) -> String {
75    format!(
76      "{} hour{}",
77      self.value,
78      if self.value != 1 { "s" } else { "" }
79    )
80  }
81
82  /// Returns a string with the amount of hours, but only if the amount is more than 0.
83  pub fn format_if_nonzero(&self) -> Option<String> {
84    if self.is_zero() {
85      return None;
86    }
87    Some(format!(
88      "{} hour{}",
89      self.value,
90      if self.value != 1 { "s" } else { "" }
91    ))
92  }
93
94  pub fn is_zero(&self) -> bool {
95    self.value == 0
96  }
97}
98
99#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
100pub struct Days {
101  pub value: u64,
102}
103
104/// Returns a string displaying the amount of days (e.g. "1 day", "2 days")
105impl Days {
106  pub fn format(&self) -> String {
107    format!(
108      "{} day{}",
109      self.value,
110      if self.value != 1 { "s" } else { "" }
111    )
112  }
113
114  /// Returns a string with the amount of days, but only if the amount is more than 0.
115  pub fn format_if_nonzero(&self) -> Option<String> {
116    if self.is_zero() {
117      return None;
118    }
119    Some(format!(
120      "{} day{}",
121      self.value,
122      if self.value != 1 { "s" } else { "" }
123    ))
124  }
125
126  pub fn is_zero(&self) -> bool {
127    self.value == 0
128  }
129}
130
131#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
132pub struct Weeks {
133  pub value: u64,
134}
135
136impl Weeks {
137  /// Returns a string displaying the amount of weeks (e.g. "1 week", "2 weeks")
138  pub fn format(&self) -> String {
139    format!(
140      "{} week{}",
141      self.value,
142      if self.value != 1 { "s" } else { "" }
143    )
144  }
145
146  /// Returns a string with the amount of weeks, but only if the amount is more than 0.
147  pub fn format_if_nonzero(&self) -> Option<String> {
148    if self.is_zero() {
149      return None;
150    }
151    Some(format!(
152      "{} week{}",
153      self.value,
154      if self.value != 1 { "s" } else { "" }
155    ))
156  }
157
158  pub fn is_zero(&self) -> bool {
159    self.value == 0
160  }
161}
162
163#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
164pub struct Months {
165  pub value: u64,
166}
167
168impl Months {
169  /// Returns a string displaying the amount of months (e.g. "1 month", "2 months")
170  pub fn format(&self) -> String {
171    format!(
172      "{} month{}",
173      self.value,
174      if self.value != 1 { "s" } else { "" }
175    )
176  }
177
178  /// Returns a string with the amount of months, but only if the amount is more than 0.
179  pub fn format_if_nonzero(&self) -> Option<String> {
180    if self.is_zero() {
181      return None;
182    }
183    Some(format!(
184      "{} month{}",
185      self.value,
186      if self.value != 1 { "s" } else { "" }
187    ))
188  }
189
190  fn is_zero(&self) -> bool {
191    self.value == 0
192  }
193}
194
195#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
196pub struct Years {
197  pub value: u64,
198}
199
200impl Years {
201  /// Returns a string displaying the amount of years (e.g. "1 year", "2 years")
202  fn format(&self) -> String {
203    format!(
204      "{} year{}",
205      self.value,
206      if self.value != 1 { "s" } else { "" }
207    )
208  }
209
210  /// Returns a string with the amount of years, but only if the amount is more than 0.
211  fn format_if_nonzero(&self) -> Option<String> {
212    if self.is_zero() {
213      return None;
214    }
215    Some(format!(
216      "{} year{}",
217      self.value,
218      if self.value != 1 { "s" } else { "" }
219    ))
220  }
221
222  fn is_zero(&self) -> bool {
223    self.value == 0
224  }
225}