maven_rs/utils/
time.rs

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
macro_rules! define_time_format {
    (   $(#[$docs:meta])*
        $time:ident($format:literal)
    ) => {
        $(#[$docs])*
        pub mod $time {
            use chrono::NaiveDateTime;
            use serde::{Deserialize, Deserializer, Serializer};

            static FORMAT: &str = $format;

            pub fn serialize<S>(
                date: &Option<NaiveDateTime>,
                serializer: S,
            ) -> Result<S::Ok, S::Error>
            where
                S: Serializer,
            {
                if let Some(date) = date {
                    let s = format!("{}", date.format(FORMAT));
                    serializer.serialize_str(&s)
                } else {
                    serializer.serialize_none()
                }
            }

            pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<NaiveDateTime>, D::Error>
            where
                D: Deserializer<'de>,
            {
                let s = String::deserialize(deserializer)?;
                NaiveDateTime::parse_from_str(&s, FORMAT)
                    .map_err(|err| {
                        serde::de::Error::custom(format!("Failed to parse date: {} {} ", s, err))
                    })
                    .map(Some)
            }
        }
    };
}
define_time_format!(
    /// Standard time format
    ///
    /// Format: `%Y%m%d%H%M%S`
    standard_time("%Y%m%d%H%M%S")
);
define_time_format!(
    /// Snapshot time format
    ///
    /// Format: `%Y%m%d.%H%M%S`
    snapshot_time("%Y%m%d.%H%M%S")
);