diff --git a/parsers/src/pubsub/pubsub.rs b/parsers/src/pubsub/pubsub.rs index 2a8a45641f58fb8181293557b31236f01618a9dd..406fe98980792b4ebe16174a80288de9c78585c9 100644 --- a/parsers/src/pubsub/pubsub.rs +++ b/parsers/src/pubsub/pubsub.rs @@ -5,9 +5,8 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. use xso::{ - error::{Error, FromElementError, FromEventsError}, - exports::rxml, - minidom_compat, AsXml, FromXml, + error::{Error, FromElementError}, + AsXml, FromXml, }; use crate::data_forms::DataForm; @@ -204,70 +203,14 @@ pub struct Retract { } /// Indicate that the subscription can be configured. -#[derive(Debug, Clone, PartialEq)] +#[derive(FromXml, AsXml, Debug, Clone, PartialEq)] +#[xml(namespace = ns::PUBSUB, name = "subscribe-options")] pub struct SubscribeOptions { /// If `true`, the configuration is actually required. + #[xml(flag)] required: bool, } -impl TryFrom for SubscribeOptions { - type Error = FromElementError; - - fn try_from(elem: Element) -> Result { - check_self!(elem, "subscribe-options", PUBSUB); - check_no_attributes!(elem, "subscribe-options"); - let mut required = false; - for child in elem.children() { - if child.is("required", ns::PUBSUB) { - if required { - return Err(Error::Other( - "More than one required element in subscribe-options.", - ) - .into()); - } - required = true; - } else { - return Err(Error::Other("Unknown child in subscribe-options element.").into()); - } - } - Ok(SubscribeOptions { required }) - } -} - -impl FromXml for SubscribeOptions { - type Builder = minidom_compat::FromEventsViaElement; - - fn from_events( - qname: rxml::QName, - attrs: rxml::AttrMap, - ) -> Result { - if qname.0 != crate::ns::PUBSUB || qname.1 != "subscribe-options" { - return Err(FromEventsError::Mismatch { name: qname, attrs }); - } - Self::Builder::new(qname, attrs) - } -} - -impl From for Element { - fn from(subscribe_options: SubscribeOptions) -> Element { - Element::builder("subscribe-options", ns::PUBSUB) - .append_all(if subscribe_options.required { - Some(Element::builder("required", ns::PUBSUB)) - } else { - None - }) - .build() - } -} - -impl AsXml for SubscribeOptions { - type ItemIter<'x> = minidom_compat::AsItemsViaElement<'x>; - - fn as_xml_iter(&self) -> Result, Error> { - minidom_compat::AsItemsViaElement::new(self.clone()) - } -} - /// A request to subscribe a JID to a node. #[derive(FromXml, AsXml, Debug, PartialEq, Clone)] #[xml(namespace = ns::PUBSUB, name = "subscribe")]