Ensure `xmpp_parsers::util::macros` implements the new traits

This is a necessary transitionary measure to ensure interoperability
between XML elements declared using the old `macro_rules!` based
approach and the new derive macros.
This commit is contained in:
Jonas Schäfer 2024-03-24 09:34:30 +01:00
parent 6054063bd8
commit 4374cabf44
1 changed files with 80 additions and 0 deletions

View File

@ -93,6 +93,18 @@ macro_rules! generate_attribute {
}))
}
}
impl ::xso::FromXmlText for $elem {
fn from_xml_text(s: &str) -> Result<Self, crate::util::error::Error> {
<Self as ::std::str::FromStr>::from_str(s)
}
}
impl ::xso::IntoXmlText for $elem {
fn into_xml_text(self) -> String {
String::from(match self {
$($elem::$a => $b),+
})
}
}
);
($(#[$meta:meta])* $elem:ident, $name:tt, {$($(#[$a_meta:meta])* $a:ident => $b:tt),+$(,)?}, Default = $default:ident) => (
$(#[$meta])*
@ -126,6 +138,16 @@ macro_rules! generate_attribute {
$elem::$default
}
}
impl ::xso::FromXmlText for $elem {
fn from_xml_text(s: &str) -> Result<Self, crate::util::error::Error> {
<Self as ::std::str::FromStr>::from_str(s)
}
}
impl ::xso::IntoOptionalXmlText for $elem {
fn into_optional_xml_text(self) -> Option<String> {
<Self as ::minidom::IntoAttributeValue>::into_attribute_value(self)
}
}
);
($(#[$meta:meta])* $elem:ident, $name:tt, ($(#[$meta_symbol:meta])* $symbol:ident => $value:tt)) => (
$(#[$meta])*
@ -158,6 +180,16 @@ macro_rules! generate_attribute {
$elem::None
}
}
impl ::xso::FromXmlText for $elem {
fn from_xml_text(s: &str) -> Result<Self, crate::util::error::Error> {
<Self as ::std::str::FromStr>::from_str(s)
}
}
impl ::xso::IntoOptionalXmlText for $elem {
fn into_optional_xml_text(self) -> Option<String> {
<Self as ::minidom::IntoAttributeValue>::into_attribute_value(self)
}
}
);
($(#[$meta:meta])* $elem:ident, $name:tt, bool) => (
$(#[$meta])*
@ -191,6 +223,16 @@ macro_rules! generate_attribute {
$elem::False
}
}
impl ::xso::FromXmlText for $elem {
fn from_xml_text(s: &str) -> Result<Self, crate::util::error::Error> {
<Self as ::std::str::FromStr>::from_str(s)
}
}
impl ::xso::IntoOptionalXmlText for $elem {
fn into_optional_xml_text(self) -> Option<String> {
<Self as ::minidom::IntoAttributeValue>::into_attribute_value(self)
}
}
);
($(#[$meta:meta])* $elem:ident, $name:tt, $type:tt, Default = $default:expr) => (
$(#[$meta])*
@ -215,6 +257,16 @@ macro_rules! generate_attribute {
$elem($default)
}
}
impl ::xso::FromXmlText for $elem {
fn from_xml_text(s: &str) -> Result<Self, crate::util::error::Error> {
<Self as ::std::str::FromStr>::from_str(s)
}
}
impl ::xso::IntoOptionalXmlText for $elem {
fn into_optional_xml_text(self) -> Option<String> {
<Self as ::minidom::IntoAttributeValue>::into_attribute_value(self)
}
}
);
}
@ -413,6 +465,18 @@ macro_rules! generate_id {
Some(self.0)
}
}
impl ::xso::FromXmlText for $elem {
fn from_xml_text(s: &str) -> Result<Self, crate::util::error::Error> {
Ok(Self(s.to_owned()))
}
}
impl ::xso::IntoXmlText for $elem {
fn into_xml_text(self) -> String {
self.0
}
}
);
}
@ -676,6 +740,16 @@ macro_rules! generate_element {
)*
}
impl ::xso::FromXml for $elem {
fn from_tree(elem: crate::Element) -> Result<Self, crate::util::error::Error> {
Self::try_from(elem)
}
fn absent() -> Option<Self> {
None
}
}
impl ::std::convert::TryFrom<crate::Element> for $elem {
type Error = crate::util::error::Error;
@ -748,6 +822,12 @@ macro_rules! generate_element {
builder.build()
}
}
impl ::xso::IntoXml for $elem {
fn into_tree(self) -> Option<::minidom::Element> {
Some(::minidom::Element::from(self))
}
}
);
}