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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use std::fmt::{Debug, Display, Formatter};
use std::str::FromStr;
pub struct HumanReadableDuration {
time_in_seconds: u64,
}
impl HumanReadableDuration {
pub fn seconds(&self) -> u64 {
self.time_in_seconds
}
pub fn minutes(&self) -> u64 {
let divisor = self.time_in_seconds as f32;
let result = divisor / 60.0f32;
return result as u64;
}
}
pub struct ParseHumanReadableDurationError;
impl Debug for ParseHumanReadableDurationError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "ParseHumanReadableDurationError")
}
}
impl Display for ParseHumanReadableDurationError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "ParseHumanReadableDurationError")
}
}
impl std::error::Error for ParseHumanReadableDurationError {}
impl FromStr for HumanReadableDuration {
type Err = ParseHumanReadableDurationError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
if value.contains('s') {
let actual_time_value = value.split("s").next().unwrap().trim();
let maybe_parsed_time_value = actual_time_value.parse::<u64>();
if maybe_parsed_time_value.is_ok() {
return Ok(HumanReadableDuration {
time_in_seconds: maybe_parsed_time_value.unwrap(),
});
}
}
Err(ParseHumanReadableDurationError)
}
}
impl From<u64> for HumanReadableDuration {
fn from(value: u64) -> Self {
HumanReadableDuration {
time_in_seconds: value,
}
}
}
impl From<u32> for HumanReadableDuration {
fn from(value: u32) -> Self {
HumanReadableDuration {
time_in_seconds: value as u64,
}
}
}
#[cfg(test)]
mod tests {
use crate::HumanReadableDuration;
use std::str::FromStr;
#[test]
fn from_u32_works() {
let representation = HumanReadableDuration::from(300 as u32);
assert_eq!(300, representation.seconds());
assert_eq!(5, representation.minutes());
}
#[test]
fn from_u64_works() {
let representation = HumanReadableDuration::from(300 as u64);
assert_eq!(300, representation.seconds());
assert_eq!(5, representation.minutes());
}
#[test]
fn from_str_10_s_works() {
let representation = HumanReadableDuration::from_str("10 s");
assert_eq!(true, representation.is_ok());
assert_eq!(10, representation.as_ref().unwrap().seconds());
assert_eq!(0, representation.as_ref().unwrap().minutes());
}
#[test]
fn from_str_10s_works() {
let representation = HumanReadableDuration::from_str("10s");
assert_eq!(true, representation.is_ok());
assert_eq!(10, representation.as_ref().unwrap().seconds());
assert_eq!(0, representation.as_ref().unwrap().minutes());
}
#[test]
fn from_str_60s_works() {
let representation = HumanReadableDuration::from_str("60s");
assert_eq!(true, representation.is_ok());
assert_eq!(60, representation.as_ref().unwrap().seconds());
assert_eq!(1, representation.as_ref().unwrap().minutes());
}
#[test]
fn from_str_61s_works() {
let representation = HumanReadableDuration::from_str("61s");
assert_eq!(true, representation.is_ok());
assert_eq!(61, representation.as_ref().unwrap().seconds());
assert_eq!(1, representation.as_ref().unwrap().minutes());
}
}