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