Skip to main content

smbcloud_model/
lib.rs

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