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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use std::fmt::{Debug, Display, Formatter};
use std::str::FromStr;

/// A data structure for parsing and managing a human readable duration representation
pub struct HumanReadableDuration {
    time_in_seconds: u64,
}

impl HumanReadableDuration {
    /// Get the duration time in seconds
    ///
    /// # Example
    /// ```
    /// use std::str::FromStr;
    /// use human_readable_time::HumanReadableDuration;
    ///
    /// let duration = HumanReadableDuration::from_str("10s");
    ///
    /// assert_eq!(10, duration.unwrap().seconds());
    /// ```
    pub fn seconds(&self) -> u64 {
        self.time_in_seconds
    }

    /// Get the duration time in full minutes
    ///
    /// # Example
    /// ```
    /// use std::str::FromStr;
    /// use human_readable_time::HumanReadableDuration;
    ///
    /// let duration = HumanReadableDuration::from_str("65s");
    ///
    /// assert_eq!(1, duration.unwrap().minutes());
    /// ```
    pub fn minutes(&self) -> u64 {
        let divisor = self.time_in_seconds as f32;
        let result = divisor / 60.0f32;
        return result as u64;
    }
}

/// The error which will be returned, if a value could not be parsed into an `HumanReadableDuration`
pub struct ParseHumanReadableDurationError;

/// `?` formatting.
///
/// `Debug` should format the output in a programmer-facing, debugging context.
impl Debug for ParseHumanReadableDurationError {
    /// Formats the value using the given formatter.
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "ParseHumanReadableDurationError")
    }
}

/// Format trait for an empty format, `{}`.
///
/// `Display` is similar to [`Debug`], but `Display` is for user-facing
/// output.
impl Display for ParseHumanReadableDurationError {
    /// Formats the value using the given formatter.
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "ParseHumanReadableDurationError")
    }
}

impl std::error::Error for ParseHumanReadableDurationError {}

/// The internally used time units which are supported.
enum InternalTimeUnit {
    Seconds,
    Minutes,
}

impl FromStr for InternalTimeUnit {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // ensure that the string has to be at least one character long
        if s.len() < 1 {
            return Err(());
        }

        // match the first character to the corresponding unit
        match s.to_lowercase().chars().next().unwrap() {
            's' => Ok(InternalTimeUnit::Seconds),
            'm' => Ok(InternalTimeUnit::Minutes),
            _ => Err(()),
        }
    }
}

/// A tuple of a time unit and the corresponding value (only for internal use).
struct InternalTime(u64, InternalTimeUnit);

/// A method for extracting the containing time information from a string. This method should
/// only be used internally.
fn extract_time_information(value: &str) -> Vec<InternalTime> {
    use lazy_static::lazy_static;
    use regex::Regex;

    // compile the regular expression for extracting the supported timings
    lazy_static! {
        static ref TIME_REGEX: Regex = Regex::from_str(r"([0-9]+)([ms]){1}").unwrap();
    }

    // collect all found matches
    let mut found_matches = vec![];
    for capture in TIME_REGEX.captures_iter(value) {
        if let Ok(time) = u64::from_str(&capture[1]) {
            if let Ok(unit) = InternalTimeUnit::from_str(&capture[2]) {
                found_matches.push(InternalTime(time, unit))
            }
        }
    }

    // return the found matches
    found_matches
}

/// Parse a value from a string
///
/// `FromStr`'s `from_str` method is often used implicitly, through
/// `str`'s `parse` method. See `parse`'s documentation for examples.
impl FromStr for HumanReadableDuration {
    type Err = ParseHumanReadableDurationError;

    /// Parses a string `s` to return a value of the [`HumanReadableDuration`] type.
    ///
    /// If parsing succeeds, return the value inside [`Ok`], otherwise
    /// when the string is ill-formatted return an error specific to the
    /// inside [`Err`].
    ///
    /// # Examples
    /// ```
    /// use std::str::FromStr;
    /// use human_readable_time::HumanReadableDuration;
    ///
    /// let s = "50s";
    /// let x = HumanReadableDuration::from_str(s).unwrap();
    ///
    /// assert_eq!(50, x.seconds());
    /// ```
    fn from_str(value: &str) -> Result<Self, Self::Err> {
        // try to get the time information from the passed string
        let time_information = extract_time_information(value);

        // if we could not extract any information, return an error
        if time_information.is_empty() {
            return Err(ParseHumanReadableDurationError);
        }

        // sum up the seconds and return corresponding object
        let mut seconds = 0;
        for current_time_object in time_information {
            match current_time_object.1 {
                InternalTimeUnit::Seconds => seconds += current_time_object.0,
                InternalTimeUnit::Minutes => seconds += current_time_object.0 * 60,
            }
        }
        return Ok(HumanReadableDuration {
            time_in_seconds: seconds,
        });
    }
}

/// Used to do value-to-value conversions while consuming the input value. It is the reciprocal of
/// [`Into`].
impl From<u64> for HumanReadableDuration {
    /// Create an instance for [`HumanReadableDuration`] from a `u64` field representing the seconds
    ///
    /// # Example
    /// ```
    /// use human_readable_time::HumanReadableDuration;
    ///
    /// let seconds: u64 = 300;
    /// let representation = HumanReadableDuration::from(seconds);
    ///
    /// assert_eq!(300, representation.seconds());
    /// assert_eq!(5, representation.minutes());
    /// ```
    fn from(value: u64) -> Self {
        HumanReadableDuration {
            time_in_seconds: value,
        }
    }
}

/// Used to do value-to-value conversions while consuming the input value. It is the reciprocal of
/// [`Into`].
impl From<u32> for HumanReadableDuration {
    /// Create an instance for [`HumanReadableDuration`] from a `u32` field representing the seconds
    ///
    /// # Example
    /// ```
    /// use human_readable_time::HumanReadableDuration;
    ///
    /// let seconds: u32 = 300;
    /// let representation = HumanReadableDuration::from(seconds);
    ///
    /// assert_eq!(300, representation.seconds());
    /// assert_eq!(5, representation.minutes());
    /// ```
    fn from(value: u32) -> Self {
        HumanReadableDuration {
            time_in_seconds: value as u64,
        }
    }
}

/// Used to do value-to-value conversions while consuming the input value. It is the reciprocal of
/// [`Into`].
impl From<u16> for HumanReadableDuration {
    /// Create an instance for [`HumanReadableDuration`] from a `u16` field representing the seconds
    ///
    /// # Example
    /// ```
    /// use human_readable_time::HumanReadableDuration;
    ///
    /// let seconds: u16 = 300;
    /// let representation = HumanReadableDuration::from(seconds);
    ///
    /// assert_eq!(300, representation.seconds());
    /// assert_eq!(5, representation.minutes());
    /// ```
    fn from(value: u16) -> Self {
        HumanReadableDuration {
            time_in_seconds: value as u64,
        }
    }
}

/// Used to do value-to-value conversions while consuming the input value. It is the reciprocal of
/// [`Into`].
impl From<u8> for HumanReadableDuration {
    /// Create an instance for [`HumanReadableDuration`] from a `u8` field representing the seconds
    ///
    /// # Example
    /// ```
    /// use human_readable_time::HumanReadableDuration;
    ///
    /// let seconds: u8 = 120;
    /// let representation = HumanReadableDuration::from(seconds);
    ///
    /// assert_eq!(120, representation.seconds());
    /// assert_eq!(2, representation.minutes());
    /// ```
    fn from(value: u8) -> 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_will_be_handled_gracefully() {
        let representation = HumanReadableDuration::from_str("10 s");
        assert_eq!(true, representation.is_err());
    }

    #[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());
    }

    #[test]
    fn from_str_5_m_will_be_handled_gracefully() {
        let representation = HumanReadableDuration::from_str("5 m");
        assert_eq!(true, representation.is_err());
    }

    #[test]
    fn from_str_5m_works() {
        let representation = HumanReadableDuration::from_str("5m");
        assert_eq!(true, representation.is_ok());
        assert_eq!(300, representation.as_ref().unwrap().seconds());
        assert_eq!(5, representation.as_ref().unwrap().minutes());
    }

    #[test]
    fn from_str_60m_works() {
        let representation = HumanReadableDuration::from_str("60m");
        assert_eq!(true, representation.is_ok());
        assert_eq!(3600, representation.as_ref().unwrap().seconds());
        assert_eq!(60, representation.as_ref().unwrap().minutes());
    }

    #[test]
    fn from_str_61m_works() {
        let representation = HumanReadableDuration::from_str("61m");
        assert_eq!(true, representation.is_ok());
        assert_eq!(3660, representation.as_ref().unwrap().seconds());
        assert_eq!(61, representation.as_ref().unwrap().minutes());
    }

    #[test]
    fn from_str_4_m_10_s_will_be_handled_gracefully() {
        let representation = HumanReadableDuration::from_str("4 m 10 s");
        assert_eq!(true, representation.is_err());
    }

    #[test]
    fn from_str_4m_10s_works() {
        let representation = HumanReadableDuration::from_str("4m 10s");
        assert_eq!(true, representation.is_ok());
        assert_eq!(250, representation.as_ref().unwrap().seconds());
        assert_eq!(4, representation.as_ref().unwrap().minutes());
    }

    #[test]
    fn from_str_4m10s_works() {
        let representation = HumanReadableDuration::from_str("4m10s");
        assert_eq!(true, representation.is_ok());
        assert_eq!(250, representation.as_ref().unwrap().seconds());
        assert_eq!(4, representation.as_ref().unwrap().minutes());
    }

    #[test]
    fn from_str_3m60s_works() {
        let representation = HumanReadableDuration::from_str("3m60s");
        assert_eq!(true, representation.is_ok());
        assert_eq!(240, representation.as_ref().unwrap().seconds());
        assert_eq!(4, representation.as_ref().unwrap().minutes());
    }

    #[test]
    fn from_str_3m61s_works() {
        let representation = HumanReadableDuration::from_str("3m61s");
        assert_eq!(true, representation.is_ok());
        assert_eq!(241, representation.as_ref().unwrap().seconds());
        assert_eq!(4, representation.as_ref().unwrap().minutes());
    }
}