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, TimeZone, 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        Utc.datetime_from_str(&s, FORMAT)
42            .map_err(serde::de::Error::custom)
43    }
44
45    #[cfg(test)]
46    mod tests {
47        use super::*;
48        use chrono::TimeZone;
49        use serde_json::json;
50        #[test]
51        fn test_ar_date_format() {
52            let date = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0);
53            let json = json!("2020-01-01T00:00:00Z");
54            assert_eq!(serde_json::to_value(date.unwrap()).unwrap(), json);
55        }
56    }
57}