parsers: port more generate_element! usages to derive macros

This commit is contained in:
Jonas Schäfer 2024-06-26 16:07:06 +02:00
parent 0c57be3e61
commit e439e9991e
6 changed files with 79 additions and 68 deletions

View file

@ -8,9 +8,9 @@ use std::fmt::{Display, Formatter};
use std::str::FromStr; use std::str::FromStr;
use minidom::{Element, IntoAttributeValue}; use minidom::{Element, IntoAttributeValue};
use xso::error::FromElementError; use xso::{error::FromElementError, FromXml, IntoXml};
use crate::ns::XDATA_VALIDATE; use crate::ns::{self, XDATA_VALIDATE};
use crate::Error; use crate::Error;
/// Validation Method /// Validation Method
@ -66,16 +66,17 @@ pub enum Method {
Regex(String), Regex(String),
} }
generate_element!( /// Selection Ranges in "list-multi"
/// Selection Ranges in "list-multi" #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
ListRange, "list-range", XDATA_VALIDATE, #[xml(namespace = ns::XDATA_VALIDATE, name = "list-range")]
attributes: [ pub struct ListRange {
/// The 'min' attribute specifies the minimum allowable number of selected/entered values. /// The 'min' attribute specifies the minimum allowable number of selected/entered values.
min: Option<u32> = "min", #[xml(attribute(default))]
/// The 'max' attribute specifies the maximum allowable number of selected/entered values. pub min: Option<u32>,
max: Option<u32> = "max", /// The 'max' attribute specifies the maximum allowable number of selected/entered values.
] #[xml(attribute(default))]
); pub max: Option<u32>,
}
/// Enum representing errors that can occur while parsing a `Datatype`. /// Enum representing errors that can occur while parsing a `Datatype`.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]

View file

@ -4,9 +4,12 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use xso::{FromXml, IntoXml};
use crate::data_forms::DataForm; use crate::data_forms::DataForm;
use crate::date::DateTime; use crate::date::DateTime;
use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload}; use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
use crate::ns;
generate_attribute!( generate_attribute!(
/// When sending a push update, the action value indicates if the service is being added or /// When sending a push update, the action value indicates if the service is being added or
@ -97,14 +100,14 @@ generate_element!(
impl IqGetPayload for Service {} impl IqGetPayload for Service {}
generate_element!( /// Structure representing a `<services xmlns='urn:xmpp:extdisco:2'/>` element.
/// Structure representing a `<services xmlns='urn:xmpp:extdisco:2'/>` element. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
ServicesQuery, "services", EXT_DISCO, #[xml(namespace = ns::EXT_DISCO, name = "services")]
attributes: [ pub struct ServicesQuery {
/// TODO /// TODO
type_: Option<Type> = "type", #[xml(attribute(default, name = "type"))]
] pub type_: Option<Type>,
); }
impl IqGetPayload for ServicesQuery {} impl IqGetPayload for ServicesQuery {}

View file

@ -203,15 +203,14 @@ impl Mix {
} }
} }
generate_element!( /// Create a new MIX channel.
/// Create a new MIX channel. #[derive(FromXml, IntoXml, PartialEq, Clone, Debug, Default)]
#[derive(Default)] #[xml(namespace = ns::MIX_CORE, name = "create")]
Create, "create", MIX_CORE, pub struct Create {
attributes: [ /// The requested channel identifier.
/// The requested channel identifier. #[xml(attribute(default))]
channel: Option<ChannelId> = "channel", pub channel: Option<ChannelId>,
] }
);
impl IqSetPayload for Create {} impl IqSetPayload for Create {}
impl IqResultPayload for Create {} impl IqResultPayload for Create {}

View file

@ -5,27 +5,32 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use xso::{FromXml, IntoXml};
use crate::date::DateTime; use crate::date::DateTime;
use crate::ns;
use crate::presence::PresencePayload; use crate::presence::PresencePayload;
generate_element!( /// Represents the query for messages before our join.
/// Represents the query for messages before our join. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone, Default)]
#[derive(Default)] #[xml(namespace = ns::MUC, name = "history")]
History, "history", MUC, pub struct History {
attributes: [ /// How many characters of history to send, in XML characters.
/// How many characters of history to send, in XML characters. #[xml(attribute(default))]
maxchars: Option<u32> = "maxchars", pub maxchars: Option<u32>,
/// How many messages to send. /// How many messages to send.
maxstanzas: Option<u32> = "maxstanzas", #[xml(attribute(default))]
pub maxstanzas: Option<u32>,
/// Only send messages received in these last seconds. /// Only send messages received in these last seconds.
seconds: Option<u32> = "seconds", #[xml(attribute(default))]
pub seconds: Option<u32>,
/// Only send messages after this date. /// Only send messages after this date.
since: Option<DateTime> = "since", #[xml(attribute(default))]
] pub since: Option<DateTime>,
); }
impl History { impl History {
/// Create a new empty history element. /// Create a new empty history element.

View file

@ -5,11 +5,15 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use xso::{
error::{Error, FromElementError},
FromXml, IntoXml,
};
use crate::message::MessagePayload; use crate::message::MessagePayload;
use crate::ns; use crate::ns;
use crate::presence::PresencePayload; use crate::presence::PresencePayload;
use crate::Element; use crate::Element;
use xso::error::{Error, FromElementError};
use jid::FullJid; use jid::FullJid;
@ -125,15 +129,15 @@ impl From<Actor> for Element {
} }
} }
generate_element!( /// Used to continue a one-to-one discussion in a room, with more than one
/// Used to continue a one-to-one discussion in a room, with more than one /// participant.
/// participant. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
Continue, "continue", MUC_USER, #[xml(namespace = ns::MUC_USER, name = "continue")]
attributes: [ pub struct Continue {
/// The thread to continue in this room. /// The thread to continue in this room.
thread: Option<String> = "thread", #[xml(attribute(default))]
] pub thread: Option<String>,
); }
generate_elem_id!( generate_elem_id!(
/// A reason for inviting, declining, etc. a request. /// A reason for inviting, declining, etc. a request.
@ -527,17 +531,16 @@ mod tests {
#[test] #[test]
fn test_continue_invalid() { fn test_continue_invalid() {
let elem: Element = "<continue xmlns='http://jabber.org/protocol/muc#user'> let elem: Element =
<foobar/> "<continue xmlns='http://jabber.org/protocol/muc#user'><foobar/></continue>"
</continue>" .parse()
.parse() .unwrap();
.unwrap();
let continue_ = Continue::try_from(elem).unwrap_err(); let continue_ = Continue::try_from(elem).unwrap_err();
let message = match continue_ { let message = match continue_ {
FromElementError::Invalid(Error::Other(string)) => string, FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(), _ => panic!(),
}; };
assert_eq!(message, "Unknown child in continue element.".to_owned()); assert_eq!(message, "Unknown child in Continue element.".to_owned());
} }
#[test] #[test]

View file

@ -54,14 +54,14 @@ generate_element!(
] ]
); );
generate_element!( /// Request to create a new node.
/// Request to create a new node. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
Create, "create", PUBSUB, #[xml(namespace = ns::PUBSUB, name = "create")]
attributes: [ pub struct Create {
/// The node name to create, if `None` the service will generate one. /// The node name to create, if `None` the service will generate one.
node: Option<NodeName> = "node", #[xml(attribute(default))]
] pub node: Option<NodeName>,
); }
generate_element!( generate_element!(
/// Request for a default node configuration. /// Request for a default node configuration.