Skip to main content

smbcloud_model/
lib.rs

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