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