time_unit/
lib.rs

1pub mod constants;
2pub mod errors;
3
4use std::str::FromStr;
5use std::fmt;
6
7/// The unit of time
8#[derive(Debug, PartialEq)]
9pub enum TimeUnit {
10    /// Nanosecond = 1 Nanosecond
11    Nanoseconds,
12
13    /// 1 Microsecond = 1000 Nanoseconds
14    Microseconds,
15
16    /// 1 Milliseconds = 1e+6 Nanoseconds
17    Milliseconds,
18
19    /// 1 Second = 1e+9 Nanoseconds
20    Seconds,
21
22    /// 1 Minute = 6e+10 Nanoseconds
23    Minutes,
24
25    /// 1 Hour = 3.6e+12 Nanoseconds
26    Hours,
27
28    /// 1 Day = 8.64e+13 Nanoseconds
29    Days,
30
31    /// 1 Week = 6.048e+14 Nanoseconds
32    Weeks,
33
34    /// 1 Month = 2.628e+15 Nanoseconds
35    Months,
36
37    /// 1 Year = 3.154e+16 Nanosecondss
38    Years,
39}
40
41impl TimeUnit {
42    /// Get nanoseconds in `TimeUnit`.
43    pub fn get_unit_nanoseconds(self) -> u64 {
44        match self {
45            TimeUnit::Nanoseconds => constants::u64::NANOSECOND,
46            TimeUnit::Microseconds => constants::u64::MICROSECOND,
47            TimeUnit::Milliseconds => constants::u64::MILLISECOND,
48            TimeUnit::Seconds => constants::u64::SECOND,
49            TimeUnit::Minutes => constants::u64::MINUTE,
50            TimeUnit::Hours => constants::u64::HOUR,
51            TimeUnit::Days => constants::u64::DAY,
52            TimeUnit::Weeks => constants::u64::WEEK,
53            TimeUnit::Months => constants::u64::MONTH,
54            TimeUnit::Years => constants::u64::YEAR,
55        }
56    }
57}
58
59impl FromStr for TimeUnit {
60    type Err = errors::UnrecognizedUnitError;
61
62    /// Get an instance of `TimeUnit` from a string slice.
63    fn from_str(s: &str) -> Result<Self, Self::Err> {
64        let unit_str = s.trim();
65        match unit_str {
66            ""  => Ok(TimeUnit::Nanoseconds),
67            "Nanoseconds"  => Ok(TimeUnit::Nanoseconds),
68            "Microseconds"  => Ok(TimeUnit::Microseconds),
69            "Milliseconds"  => Ok(TimeUnit::Milliseconds),
70            "Seconds"  => Ok(TimeUnit::Seconds),
71            "Minutes"  => Ok(TimeUnit::Minutes),
72            "Hours"  => Ok(TimeUnit::Hours),
73            "Days"  => Ok(TimeUnit::Days),
74            "Weeks"  => Ok(TimeUnit::Weeks),
75            "Months"  => Ok(TimeUnit::Months),
76            "Years"  => Ok(TimeUnit::Years),
77            _ => Err(errors::UnrecognizedUnitError {}),
78        }
79    }
80}
81
82impl fmt::Display for TimeUnit {
83    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
84        write!(f, "{:?}", self)
85    }
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    #[test]
93    fn test_from_str() {
94        assert_eq!(TimeUnit::Nanoseconds, TimeUnit::from_str("").unwrap());
95        assert_eq!(TimeUnit::Nanoseconds, TimeUnit::from_str("Nanoseconds").unwrap());
96        assert_eq!(TimeUnit::Microseconds, TimeUnit::from_str("Microseconds").unwrap());
97        assert_eq!(TimeUnit::Milliseconds, TimeUnit::from_str("Milliseconds").unwrap());
98        assert_eq!(TimeUnit::Seconds, TimeUnit::from_str("Seconds").unwrap());
99        assert_eq!(TimeUnit::Minutes, TimeUnit::from_str("Minutes").unwrap());
100        assert_eq!(TimeUnit::Hours, TimeUnit::from_str("Hours").unwrap());
101        assert_eq!(TimeUnit::Days, TimeUnit::from_str("Days").unwrap());
102        assert_eq!(TimeUnit::Weeks, TimeUnit::from_str("Weeks").unwrap());
103        assert_eq!(TimeUnit::Months, TimeUnit::from_str("Months").unwrap());
104        assert_eq!(TimeUnit::Years, TimeUnit::from_str("Years").unwrap());
105    }
106
107    #[test]
108    fn test_to_string() {
109        assert_eq!(String::from("Nanoseconds"), TimeUnit::Nanoseconds.to_string());
110        assert_eq!(String::from("Microseconds"), TimeUnit::Microseconds.to_string());
111        assert_eq!(String::from("Milliseconds"), TimeUnit::Milliseconds.to_string());
112        assert_eq!(String::from("Seconds"), TimeUnit::Seconds.to_string());
113        assert_eq!(String::from("Minutes"), TimeUnit::Minutes.to_string());
114        assert_eq!(String::from("Hours"), TimeUnit::Hours.to_string());
115        assert_eq!(String::from("Days"), TimeUnit::Days.to_string());
116        assert_eq!(String::from("Weeks"), TimeUnit::Weeks.to_string());
117        assert_eq!(String::from("Months"), TimeUnit::Months.to_string());
118        assert_eq!(String::from("Years"), TimeUnit::Years.to_string());
119    }
120
121    #[test]
122    fn test_get_unit_nanoseconds() {
123        assert_eq!(TimeUnit::Nanoseconds.get_unit_nanoseconds(), 1);
124        assert_eq!(TimeUnit::Microseconds.get_unit_nanoseconds(), 1_000);
125        assert_eq!(TimeUnit::Milliseconds.get_unit_nanoseconds(), 1_000_000);
126        assert_eq!(TimeUnit::Seconds.get_unit_nanoseconds(), 1_000_000_000);
127        assert_eq!(TimeUnit::Minutes.get_unit_nanoseconds(), 60_000_000_000);
128        assert_eq!(TimeUnit::Hours.get_unit_nanoseconds(), 3_600_000_000_000);
129        assert_eq!(TimeUnit::Days.get_unit_nanoseconds(), 86_400_000_000_000);
130        assert_eq!(TimeUnit::Weeks.get_unit_nanoseconds(), 604_800_000_000_000);
131        assert_eq!(TimeUnit::Months.get_unit_nanoseconds(), 2_628_000_000_000_000);
132        assert_eq!(TimeUnit::Years.get_unit_nanoseconds(), 31_540_000_000_000_000);
133    }
134}