use core::time::Duration;

use crate::{des::Deserializer, ser::Serializer, Deserialize, Serialize};

impl<'de> Deserialize<'de> for Duration {
    fn deserialize<D: Deserializer<'de>>(deserializer: &mut D) -> Result<Self, D::Error> {
        let seconds = deserializer.read_u64()?;
        let nanoseconds = deserializer.read_u32()?;
        Ok(Self::new(seconds, nanoseconds))
    }
}

impl Serialize for Duration {
    fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
        serializer.write_u64(self.as_secs())?;
        serializer.write_u32(self.subsec_nanos())
    }
}
