smbcloud_model/
lib.rs

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