From d5d9c60c53a264e3190610ad7acfa8c9f66415f0 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 29 Nov 2022 12:14:58 +0100 Subject: [PATCH] Add implementations for IpAddr --- instant-xml/src/impls.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/instant-xml/src/impls.rs b/instant-xml/src/impls.rs index ced4cc3..324f84c 100644 --- a/instant-xml/src/impls.rs +++ b/instant-xml/src/impls.rs @@ -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( + &self, + field: Option>, + serializer: &mut Serializer, + ) -> 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, + ) -> Result<(), Error> { + if into.is_some() { + return Err(Error::DuplicateValue); + } + + let mut value = None; + FromXmlStr::::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; +}