Add implementations for IpAddr

This commit is contained in:
Dirkjan Ochtman 2022-11-29 12:14:58 +01:00
parent 5bd58698d7
commit d5d9c60c53
1 changed files with 36 additions and 0 deletions

View File

@ -1,5 +1,6 @@
use std::borrow::Cow;
use std::fmt;
use std::net::IpAddr;
use std::str::FromStr;
#[cfg(feature = "chrono")]
@ -541,3 +542,38 @@ impl<'xml> FromXml<'xml> for () {
const KIND: Kind<'static> = Kind::Scalar;
}
impl ToXml for IpAddr {
fn serialize<W: fmt::Write + ?Sized>(
&self,
field: Option<Id<'_>>,
serializer: &mut Serializer<W>,
) -> Result<(), Error> {
DisplayToXml(self).serialize(field, serializer)
}
const KIND: Kind<'static> = Kind::Scalar;
}
impl<'xml> FromXml<'xml> for IpAddr {
fn deserialize<'cx>(
deserializer: &mut Deserializer<'cx, 'xml>,
into: &mut Option<Self>,
) -> Result<(), Error> {
if into.is_some() {
return Err(Error::DuplicateValue);
}
let mut value = None;
FromXmlStr::<Self>::deserialize(deserializer, &mut value)?;
match value {
Some(value) => {
*into = Some(value.0);
Ok(())
}
None => Err(Error::MissingValue(&Kind::Scalar)),
}
}
const KIND: Kind<'static> = Kind::Scalar;
}