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