From 4e2af36186ca9e4d88c36e024b7e95a2b02dd0d7 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 4 Aug 2024 17:07:58 +0200 Subject: [PATCH] xmpp-parsers: Implement FromXmlText and AsXmlText for default types This allows such default type wrappers to be used in other xso elements. --- parsers/src/util/macros.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/parsers/src/util/macros.rs b/parsers/src/util/macros.rs index d5e03fd2..e589a28a 100644 --- a/parsers/src/util/macros.rs +++ b/parsers/src/util/macros.rs @@ -275,6 +275,23 @@ macro_rules! generate_attribute { $elem($default) } } + impl ::xso::FromXmlText for $elem { + fn from_xml_text(s: String) -> Result<$elem, xso::error::Error> { + s.parse().map_err(xso::error::Error::text_parse_error) + } + } + impl ::xso::AsXmlText for $elem { + fn as_xml_text(&self) -> Result<::std::borrow::Cow<'_, str>, xso::error::Error> { + Ok(::std::borrow::Cow::Owned(format!("{}", self.0))) + } + + fn as_optional_xml_text(&self) -> Result>, xso::error::Error> { + match self.0 { + $default => Ok(None), + _ => Ok(Some(::std::borrow::Cow::Owned(format!("{}", self.0)))), + } + } + } ); }