proto_types/duration/
duration_units.rs1use alloc::string::ToString;
2
3use crate::String;
4
5macro_rules! impl_display {
6 ($($name:ident),*) => {
7 paste::paste! {
8 $(
9 impl core::fmt::Display for [< $name s >] {
10 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
11 write!(f, "{} {}{}", self.value, stringify!([< $name:lower >]), if self.value != 1 { "s" } else { "" })
12 }
13 }
14 )*
15 }
16 };
17}
18
19impl_display!(Second, Minute, Hour, Day, Week, Month, Year);
20
21#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
23pub struct Seconds {
24 pub value: u64,
25}
26
27impl Seconds {
28 #[must_use]
30 pub fn format_if_nonzero(&self) -> Option<String> {
31 if self.is_zero() {
32 return None;
33 }
34 Some(self.to_string())
35 }
36
37 const fn is_zero(&self) -> bool {
38 self.value == 0
39 }
40}
41
42#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
44pub struct Minutes {
45 pub value: u64,
46}
47
48impl Minutes {
50 #[must_use]
52 pub fn format_if_nonzero(&self) -> Option<String> {
53 if self.is_zero() {
54 return None;
55 }
56 Some(self.to_string())
57 }
58
59 const fn is_zero(&self) -> bool {
60 self.value == 0
61 }
62}
63
64#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
66pub struct Hours {
67 pub value: u64,
68}
69
70impl Hours {
72 #[must_use]
74 pub fn format_if_nonzero(&self) -> Option<String> {
75 if self.is_zero() {
76 return None;
77 }
78 Some(self.to_string())
79 }
80
81 const fn is_zero(&self) -> bool {
82 self.value == 0
83 }
84}
85
86#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
88pub struct Days {
89 pub value: u64,
90}
91
92impl Days {
94 #[must_use]
96 pub fn format_if_nonzero(&self) -> Option<String> {
97 if self.is_zero() {
98 return None;
99 }
100 Some(self.to_string())
101 }
102
103 const fn is_zero(&self) -> bool {
104 self.value == 0
105 }
106}
107
108#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
110pub struct Weeks {
111 pub value: u64,
112}
113
114impl Weeks {
115 #[must_use]
117 pub fn format_if_nonzero(&self) -> Option<String> {
118 if self.is_zero() {
119 return None;
120 }
121 Some(self.to_string())
122 }
123
124 const fn is_zero(&self) -> bool {
126 self.value == 0
127 }
128}
129
130#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
132pub struct Months {
133 pub value: u64,
134}
135
136impl Months {
137 #[must_use]
139 pub fn format_if_nonzero(&self) -> Option<String> {
140 if self.is_zero() {
141 return None;
142 }
143 Some(self.to_string())
144 }
145
146 const fn is_zero(&self) -> bool {
147 self.value == 0
148 }
149}
150
151#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
153pub struct Years {
154 pub value: u64,
155}
156
157impl Years {
158 #[must_use]
160 pub fn format_if_nonzero(&self) -> Option<String> {
161 if self.is_zero() {
162 return None;
163 }
164 Some(self.to_string())
165 }
166
167 const fn is_zero(&self) -> bool {
168 self.value == 0
169 }
170}