Add impls for chrono::NaiveDate

This commit is contained in:
Dirkjan Ochtman 2022-12-02 09:53:14 +01:00
parent 0bfd37a9d7
commit 99dab24219
1 changed files with 57 additions and 1 deletions

View File

@ -5,7 +5,7 @@ use std::net::IpAddr;
use std::str::FromStr; use std::str::FromStr;
#[cfg(feature = "chrono")] #[cfg(feature = "chrono")]
use chrono::{DateTime, Utc}; use chrono::{DateTime, NaiveDate, Utc};
use crate::{Deserializer, Error, FromXml, Id, Kind, Serializer, ToXml}; use crate::{Deserializer, Error, FromXml, Id, Kind, Serializer, ToXml};
@ -604,6 +604,62 @@ impl<'xml> FromXml<'xml> for DateTime<Utc> {
const KIND: Kind = Kind::Scalar; const KIND: Kind = Kind::Scalar;
} }
#[cfg(feature = "chrono")]
impl ToXml for NaiveDate {
fn serialize<W: fmt::Write + ?Sized>(
&self,
field: Option<Id<'_>>,
serializer: &mut Serializer<W>,
) -> Result<(), Error> {
let prefix = match field {
Some(id) => {
let prefix = serializer.write_start(id.name, id.ns)?;
serializer.end_start()?;
Some((prefix, id.name))
}
None => None,
};
serializer.write_str(&self)?;
if let Some((prefix, name)) = prefix {
serializer.write_close(prefix, name)?;
}
Ok(())
}
}
#[cfg(feature = "chrono")]
impl<'xml> FromXml<'xml> for NaiveDate {
#[inline]
fn matches(id: Id<'_>, field: Option<Id<'_>>) -> bool {
match field {
Some(field) => id == field,
None => false,
}
}
fn deserialize<'cx>(
deserializer: &mut Deserializer<'cx, 'xml>,
into: &mut Option<Self>,
) -> Result<(), Error> {
if into.is_some() {
return Err(Error::DuplicateValue);
}
let data = deserializer.take_str()?;
match NaiveDate::parse_from_str(data, "%Y-%m-%d") {
Ok(d) => {
*into = Some(d);
Ok(())
}
_ => Err(Error::Other("invalid date/time".into())),
}
}
const KIND: Kind = Kind::Scalar;
}
impl<'xml> FromXml<'xml> for () { impl<'xml> FromXml<'xml> for () {
#[inline] #[inline]
fn matches(id: Id<'_>, field: Option<Id<'_>>) -> bool { fn matches(id: Id<'_>, field: Option<Id<'_>>) -> bool {