pub mod serialize_as_str {
    use serde::{Deserialize, Deserializer, Serialize, Serializer};
    use std::str::FromStr;
    use std::string::ToString;

    #[allow(dead_code)]
    pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
    where
        T: ToString,
        S: Serializer,
    {
        value.to_string().serialize(serializer)
    }

    pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
    where
        T: FromStr,
        D: Deserializer<'de>,
        <T as FromStr>::Err: std::fmt::Debug,
    {
        let s = String::deserialize(deserializer)?;
        s.parse()
            .map_err(|e| serde::de::Error::custom(format!("Parse error: {:?}", e)))
    }
}

pub mod serialize_vec_as_strs {
    use serde::{Deserialize, Deserializer, Serializer};
    use std::str::FromStr;
    use std::string::ToString;

    #[allow(dead_code)]
    pub fn serialize<T, S>(value: &[T], serializer: S) -> Result<S::Ok, S::Error>
    where
        T: ToString,
        S: Serializer,
    {
        serializer.collect_seq(value.iter().map(ToString::to_string))
    }

    pub fn deserialize<'de, T, D>(deserializer: D) -> Result<Vec<T>, D::Error>
    where
        T: FromStr,
        D: Deserializer<'de>,
        <T as FromStr>::Err: std::fmt::Debug,
    {
        let strings: Vec<String> = Vec::deserialize(deserializer)?;
        let mut res = Vec::with_capacity(strings.len());
        for elem in strings.into_iter() {
            let parsed: T = elem
                .parse()
                .map_err(|e| serde::de::Error::custom(format!("Parse error: {:?}", e)))?;
            res.push(parsed)
        }

        Ok(res)
    }
}
