use serde::de::{Error, SeqAccess, Visitor}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use crate::duration::CalendarDuration; impl Serialize for CalendarDuration { fn serialize(&self, serializer: S) -> Result where S: Serializer, { (self.whole_months()).serialize(serializer) } } impl<'de> Deserialize<'de> for CalendarDuration { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { struct CalendarDurationVisitor; impl<'de> Visitor<'de> for CalendarDurationVisitor { type Value = CalendarDuration; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { formatter.write_str("a `MonthDuration`") } fn visit_seq(self, mut seq: A) -> Result where A: SeqAccess<'de>, { let months = seq.next_element()?; let months = months.ok_or_else(|| Error::custom("expected months"))?; Ok(CalendarDuration::months(months)) } } deserializer.deserialize_tuple(1, CalendarDurationVisitor) } }