Skip to main content

smbcloud_model/
lib.rs

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