Skip to main content

proto_types/duration/
duration_units.rs

1use 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/// A struct representing seconds. Wraps the value and provides extra formatting methods.
22#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
23pub struct Seconds {
24	pub value: u64,
25}
26
27impl Seconds {
28	/// Returns a string with the amount of seconds, but only if the amount is more than 0.
29	#[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/// A struct representing minutes. Wraps the value and provides extra formatting methods.
43#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
44pub struct Minutes {
45	pub value: u64,
46}
47
48/// Returns a string displaying the amount of minutes (e.g. "1 minute", "2 minutes")
49impl Minutes {
50	/// Returns a string with the amount of minutes, but only if the amount is more than 0.
51	#[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/// A struct representing hours. Wraps the value and provides extra formatting methods.
65#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
66pub struct Hours {
67	pub value: u64,
68}
69
70/// Returns a string displaying the amount of hours (e.g. "1 hour", "2 hours")
71impl Hours {
72	/// Returns a string with the amount of hours, but only if the amount is more than 0.
73	#[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/// A struct representing days. Wraps the value and provides extra formatting methods.
87#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
88pub struct Days {
89	pub value: u64,
90}
91
92/// Returns a string displaying the amount of days (e.g. "1 day", "2 days")
93impl Days {
94	/// Returns a string with the amount of days, but only if the amount is more than 0.
95	#[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/// A struct representing weeks. Wraps the value and provides extra formatting methods.
109#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
110pub struct Weeks {
111	pub value: u64,
112}
113
114impl Weeks {
115	/// Returns a string with the amount of weeks, but only if the amount is more than 0.
116	#[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	/// Returns `true` if the value is zero.
125	const fn is_zero(&self) -> bool {
126		self.value == 0
127	}
128}
129
130/// A struct representing months. Wraps the value and provides extra formatting methods.
131#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
132pub struct Months {
133	pub value: u64,
134}
135
136impl Months {
137	/// Returns a string with the amount of months, but only if the amount is more than 0.
138	#[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/// A struct representing years. Wraps the value and provides extra formatting methods.
152#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
153pub struct Years {
154	pub value: u64,
155}
156
157impl Years {
158	/// Returns a string with the amount of years, but only if the amount is more than 0.
159	#[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}