use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{serde_as, DeserializeAs, DisplayFromStr, SerializeAs};

#[serde_as]
#[derive(Deserialize, Serialize)]
pub struct Range {
    #[serde_as(as = "DisplayFromStr")]
    start: u32,
    #[serde_as(as = "DisplayFromStr")]
    end: u32,
}

impl<'de> DeserializeAs<'de, std::ops::Range<u32>> for Range {
    fn deserialize_as<D>(deserializer: D) -> Result<std::ops::Range<u32>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let range = Range::deserialize(deserializer)?;
        Ok(std::ops::Range {
            start: range.start,
            end: range.end,
        })
    }
}

impl SerializeAs<std::ops::Range<u32>> for Range {
    fn serialize_as<S>(
        &std::ops::Range { start, end }: &std::ops::Range<u32>,
        serializer: S,
    ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
    where
        S: Serializer,
    {
        Range { start, end }.serialize(serializer)
    }
}
