xmpp-parsers: Simplify PubSub Item using xso

Now that we can have a single arbitrary payload, we can derive FromXml
and AsXml on the Item.
This commit is contained in:
Emmanuel Gil Peyrot 2024-12-20 19:05:07 +01:00 committed by Jonas Schäfer
commit f63b780089
7 changed files with 55 additions and 115 deletions

View file

@ -408,73 +408,3 @@ macro_rules! assert_size (
assert_eq!(::core::mem::size_of::<$t>(), $sz);
);
);
// TODO: move that to src/pubsub/mod.rs, once we figure out how to use macros from there.
macro_rules! impl_pubsub_item {
($item:ident, $ns:ident) => {
impl ::core::convert::TryFrom<minidom::Element> for $item {
type Error = FromElementError;
fn try_from(mut elem: minidom::Element) -> Result<$item, FromElementError> {
check_self!(elem, "item", $ns);
check_no_unknown_attributes!(elem, "item", ["id", "publisher"]);
let mut payloads = elem.take_contents_as_children().collect::<Vec<_>>();
let payload = payloads.pop();
if !payloads.is_empty() {
return Err(Error::Other("More than a single payload in item element.").into());
}
Ok($item(crate::pubsub::Item {
id: get_attr!(elem, "id", Option),
publisher: get_attr!(elem, "publisher", Option),
payload,
}))
}
}
impl ::xso::FromXml for $item {
type Builder = ::xso::minidom_compat::FromEventsViaElement<$item>;
fn from_events(
qname: ::xso::exports::rxml::QName,
attrs: ::xso::exports::rxml::AttrMap,
) -> Result<Self::Builder, ::xso::error::FromEventsError> {
if qname.0 != crate::ns::$ns || qname.1 != "item" {
return Err(::xso::error::FromEventsError::Mismatch { name: qname, attrs });
}
Self::Builder::new(qname, attrs)
}
}
impl From<$item> for minidom::Element {
fn from(item: $item) -> minidom::Element {
minidom::Element::builder("item", ns::$ns)
.attr("id", item.0.id)
.attr("publisher", item.0.publisher)
.append_all(item.0.payload)
.build()
}
}
impl ::xso::AsXml for $item {
type ItemIter<'x> = ::xso::minidom_compat::AsItemsViaElement<'x>;
fn as_xml_iter(&self) -> Result<Self::ItemIter<'_>, ::xso::error::Error> {
::xso::minidom_compat::AsItemsViaElement::new(self.clone())
}
}
impl ::core::ops::Deref for $item {
type Target = crate::pubsub::Item;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl ::core::ops::DerefMut for $item {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
};
}