smbcloud_model/
lib.rs

1pub mod account;
2pub mod app_auth;
3pub mod forgot;
4pub mod login;
5pub mod project;
6pub mod signup;
7
8pub mod ar_date_format {
9    use chrono::{DateTime, Utc};
10    use serde::{self, Deserialize, Deserializer, Serializer};
11
12    const FORMAT: &str = "%Y-%m-%dT%H:%M:%S%.f%#z";
13
14    // The signature of a serialize_with function must follow the pattern:
15    //
16    //    fn serialize<S>(&T, S) -> Result<S::Ok, S::Error>
17    //    where
18    //        S: Serializer
19    //
20    // although it may also be generic over the input types T.
21    pub fn serialize<S>(date: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error>
22    where
23        S: Serializer,
24    {
25        let s = format!("{}", date.naive_utc());
26        serializer.serialize_str(&s)
27    }
28
29    // The signature of a deserialize_with function must follow the pattern:
30    //
31    //    fn deserialize<'de, D>(D) -> Result<T, D::Error>
32    //    where
33    //        D: Deserializer<'de>
34    //
35    // although it may also be generic over the output types T.
36    pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
37    where
38        D: Deserializer<'de>,
39    {
40        let s = String::deserialize(deserializer)?;
41        DateTime::parse_from_str(&s, FORMAT)
42            .map(|dt| dt.with_timezone(&Utc))
43            .map_err(serde::de::Error::custom)
44    }
45
46    #[cfg(test)]
47    mod tests {
48        use super::*;
49        use chrono::TimeZone;
50        use serde_json::json;
51        #[test]
52        fn test_ar_date_format() {
53            let date = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0);
54            let json = json!("2020-01-01T00:00:00Z");
55            assert_eq!(serde_json::to_value(date.unwrap()).unwrap(), json);
56        }
57    }
58}