ih_muse_proto/
timestamp_resolution.rs

1use std::fmt;
2use std::str::FromStr;
3use std::time::Duration;
4
5use serde::{Deserialize, Serialize};
6
7#[derive(PartialEq, Eq, Debug, Clone, Copy, Hash, Default, Deserialize, Serialize)]
8pub enum TimestampResolution {
9    Years,
10    Months,
11    Weeks,
12    Days,
13    Hours,
14    Minutes,
15    #[default]
16    Seconds,
17    Milliseconds,
18    Microseconds,
19}
20
21impl FromStr for TimestampResolution {
22    type Err = String;
23
24    fn from_str(s: &str) -> Result<Self, Self::Err> {
25        TimestampResolution::iter_ordered()
26            .find(|&res| res.as_str().eq_ignore_ascii_case(s))
27            .ok_or_else(|| format!("Invalid resolution: {}", s))
28    }
29}
30
31impl fmt::Display for TimestampResolution {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        write!(f, "{}", self.as_str())
34    }
35}
36
37impl TimestampResolution {
38    // Method to return an iterator over TimestampResolution from finest to coarsest
39    pub fn iter_ordered() -> impl Iterator<Item = TimestampResolution> {
40        [
41            TimestampResolution::Microseconds,
42            TimestampResolution::Milliseconds,
43            TimestampResolution::Seconds,
44            TimestampResolution::Minutes,
45            TimestampResolution::Hours,
46            TimestampResolution::Days,
47            TimestampResolution::Weeks,
48            TimestampResolution::Months,
49            TimestampResolution::Years,
50        ]
51        .iter()
52        .copied()
53    }
54
55    /// Method to get the string representation of the enum variant
56    pub fn as_str(&self) -> &'static str {
57        match *self {
58            TimestampResolution::Years => "Years",
59            TimestampResolution::Months => "Months",
60            TimestampResolution::Weeks => "Weeks",
61            TimestampResolution::Days => "Days",
62            TimestampResolution::Hours => "Hours",
63            TimestampResolution::Minutes => "Minutes",
64            TimestampResolution::Seconds => "Seconds",
65            TimestampResolution::Milliseconds => "Milliseconds",
66            TimestampResolution::Microseconds => "Microseconds",
67        }
68    }
69
70    pub fn as_u8(&self) -> u8 {
71        match self {
72            TimestampResolution::Years => 0,
73            TimestampResolution::Months => 1,
74            TimestampResolution::Weeks => 2,
75            TimestampResolution::Days => 3,
76            TimestampResolution::Hours => 4,
77            TimestampResolution::Minutes => 5,
78            TimestampResolution::Seconds => 6,
79            TimestampResolution::Milliseconds => 7,
80            TimestampResolution::Microseconds => 8,
81        }
82    }
83
84    pub fn from_u8(value: u8) -> Self {
85        match value {
86            0 => TimestampResolution::Years,
87            1 => TimestampResolution::Months,
88            2 => TimestampResolution::Weeks,
89            3 => TimestampResolution::Days,
90            4 => TimestampResolution::Hours,
91            5 => TimestampResolution::Minutes,
92            6 => TimestampResolution::Seconds,
93            7 => TimestampResolution::Milliseconds,
94            8 => TimestampResolution::Microseconds,
95            _ => panic!("Unexpected value: {value}"),
96        }
97    }
98
99    /// Converts the `TimestampResolution` to a `Duration`.
100    pub fn to_duration(&self) -> Duration {
101        match *self {
102            TimestampResolution::Years => Duration::from_secs(31_536_000), // 365 days
103            TimestampResolution::Months => Duration::from_secs(2_592_000), // 30 days
104            TimestampResolution::Weeks => Duration::from_secs(604_800),    // 7 days
105            TimestampResolution::Days => Duration::from_secs(86_400),      // 24 hours
106            TimestampResolution::Hours => Duration::from_secs(3_600),      // 1 hour
107            TimestampResolution::Minutes => Duration::from_secs(60),       // 1 minute
108            TimestampResolution::Seconds => Duration::from_secs(1),        // 1 second
109            TimestampResolution::Milliseconds => Duration::from_millis(1), // 1 millisecond
110            TimestampResolution::Microseconds => Duration::from_micros(1), // 1 microsecond
111        }
112    }
113}