pub mod account;
pub mod app_auth;
pub mod deploy_config;
pub mod deploy_config_update;
pub mod error_codes;
pub mod forgot;
pub mod frontend_app;
pub mod login;
pub mod mail;
pub mod oauth;
pub mod project;
pub mod repository;
pub mod reset_password_response;
pub mod runner;
pub mod signup;

pub mod ar_date_format {
    use chrono::{DateTime, Utc};
    use serde::{self, Deserialize, Deserializer, Serializer};

    const FORMAT: &str = "%Y-%m-%dT%H:%M:%S%.f%#z";

    // The signature of a serialize_with function must follow the pattern:
    //
    //    fn serialize<S>(&T, S) -> Result<S::Ok, S::Error>
    //    where
    //        S: Serializer
    //
    // although it may also be generic over the input types T.
    pub fn serialize<S>(date: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let s = format!("{}", date.naive_utc());
        serializer.serialize_str(&s)
    }

    // The signature of a deserialize_with function must follow the pattern:
    //
    //    fn deserialize<'de, D>(D) -> Result<T, D::Error>
    //    where
    //        D: Deserializer<'de>
    //
    // although it may also be generic over the output types T.
    pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        DateTime::parse_from_str(&s, FORMAT)
            .map(|dt| dt.with_timezone(&Utc))
            .map_err(serde::de::Error::custom)
    }

    #[cfg(test)]
    mod tests {
        use super::*;
        use chrono::TimeZone;
        use serde_json::json;
        #[test]
        fn test_ar_date_format() {
            let date = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0);
            let json = json!("2020-01-01T00:00:00Z");
            assert_eq!(serde_json::to_value(date.unwrap()).unwrap(), json);
        }
    }
}
