parsers: replace some generate_element! usage with derive macros

This commit is contained in:
Jonas Schäfer 2024-06-23 09:52:41 +02:00
commit 0bae5d3346
6 changed files with 91 additions and 68 deletions

View file

@ -4,6 +4,8 @@
// 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::iq::{IqGetPayload, IqResultPayload}; use crate::iq::{IqGetPayload, IqResultPayload};
use crate::ns; use crate::ns;
use crate::Element; use crate::Element;
@ -90,14 +92,14 @@ generate_element!(
] ]
); );
generate_element!( /// Get URL
/// Get URL #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
Get, "get", HTTP_UPLOAD, #[xml(namespace = ns::HTTP_UPLOAD, name = "get")]
attributes: [ pub struct Get {
/// URL /// URL
url: Required<String> = "url", #[xml(attribute)]
] pub url: String,
); }
generate_element!( generate_element!(
/// Requesting a slot /// Requesting a slot

View file

@ -4,17 +4,20 @@
// 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 crate::message::MessagePayload; use xso::{FromXml, IntoXml};
generate_element!( use crate::message::MessagePayload;
/// Defines that the message containing this payload should replace a use crate::ns;
/// previous message, identified by the id.
Replace, "replace", MESSAGE_CORRECT, /// Defines that the message containing this payload should replace a
attributes: [ /// previous message, identified by the id.
/// The 'id' attribute of the message getting corrected. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
id: Required<String> = "id", #[xml(namespace = ns::MESSAGE_CORRECT, name = "replace")]
] pub struct Replace {
); /// The 'id' attribute of the message getting corrected.
#[xml(attribute)]
pub id: String,
}
impl MessagePayload for Replace {} impl MessagePayload for Replace {}
@ -47,7 +50,7 @@ mod tests {
#[cfg(not(feature = "disable-validation"))] #[cfg(not(feature = "disable-validation"))]
#[test] #[test]
fn test_invalid_attribute() { fn test_invalid_attribute() {
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' coucou=''/>" let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou' coucou=''/>"
.parse() .parse()
.unwrap(); .unwrap();
let error = Replace::try_from(elem).unwrap_err(); let error = Replace::try_from(elem).unwrap_err();
@ -55,20 +58,21 @@ mod tests {
FromElementError::Invalid(Error::Other(string)) => string, FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(), _ => panic!(),
}; };
assert_eq!(message, "Unknown attribute in replace element."); assert_eq!(message, "Unknown attribute in Replace element.");
} }
#[test] #[test]
fn test_invalid_child() { fn test_invalid_child() {
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'><coucou/></replace>" let elem: Element =
.parse() "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'><coucou/></replace>"
.unwrap(); .parse()
.unwrap();
let error = Replace::try_from(elem).unwrap_err(); let error = Replace::try_from(elem).unwrap_err();
let message = match error { let message = match error {
FromElementError::Invalid(Error::Other(string)) => string, FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(), _ => panic!(),
}; };
assert_eq!(message, "Unknown child in replace element."); assert_eq!(message, "Unknown child in Replace element.");
} }
#[test] #[test]
@ -81,7 +85,10 @@ mod tests {
FromElementError::Invalid(Error::Other(string)) => string, FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(), _ => panic!(),
}; };
assert_eq!(message, "Required attribute 'id' missing."); assert_eq!(
message,
"Required attribute field 'id' on Replace element missing."
);
} }
#[test] #[test]

View file

@ -4,21 +4,23 @@
// 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::message::MessagePayload; use crate::message::MessagePayload;
use crate::ns;
use crate::presence::PresencePayload; use crate::presence::PresencePayload;
generate_element!( /// Unique identifier given to a MUC participant.
/// Unique identifier given to a MUC participant. ///
/// /// It allows clients to identify a MUC participant across reconnects and
/// It allows clients to identify a MUC participant across reconnects and /// renames. It thus prevents impersonification of anonymous users.
/// renames. It thus prevents impersonification of anonymous users. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
OccupantId, "occupant-id", OID, #[xml(namespace = ns::OID, name = "occupant-id")]
pub struct OccupantId {
attributes: [ /// The id associated to the sending user by the MUC service.
/// The id associated to the sending user by the MUC service. #[xml(attribute)]
id: Required<String> = "id", pub id: String,
] }
);
impl MessagePayload for OccupantId {} impl MessagePayload for OccupantId {}
impl PresencePayload for OccupantId {} impl PresencePayload for OccupantId {}
@ -52,15 +54,16 @@ mod tests {
#[test] #[test]
fn test_invalid_child() { fn test_invalid_child() {
let elem: Element = "<occupant-id xmlns='urn:xmpp:occupant-id:0'><coucou/></occupant-id>" let elem: Element =
.parse() "<occupant-id xmlns='urn:xmpp:occupant-id:0' id='foo'><coucou/></occupant-id>"
.unwrap(); .parse()
.unwrap();
let error = OccupantId::try_from(elem).unwrap_err(); let error = OccupantId::try_from(elem).unwrap_err();
let message = match error { let message = match error {
FromElementError::Invalid(Error::Other(string)) => string, FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(), _ => panic!(),
}; };
assert_eq!(message, "Unknown child in occupant-id element."); assert_eq!(message, "Unknown child in OccupantId element.");
} }
#[test] #[test]
@ -73,7 +76,10 @@ mod tests {
FromElementError::Invalid(Error::Other(string)) => string, FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(), _ => panic!(),
}; };
assert_eq!(message, "Required attribute 'id' missing."); assert_eq!(
message,
"Required attribute field 'id' on OccupantId element missing."
);
} }
#[test] #[test]

View file

@ -5,6 +5,8 @@
// 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::iq::{IqGetPayload, IqResultPayload, IqSetPayload}; use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
use crate::ns; use crate::ns;
@ -73,14 +75,14 @@ generate_element!(
] ]
); );
generate_element!( /// A redirect element.
/// A redirect element. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
Redirect, "redirect", PUBSUB_OWNER, #[xml(namespace = ns::PUBSUB_OWNER, name = "redirect")]
attributes: [ pub struct Redirect {
/// The node this node will be redirected to. /// The node this node will be redirected to.
uri: Required<String> = "uri", #[xml(attribute)]
] pub uri: String,
); }
generate_element!( generate_element!(
/// Request to delete a node. /// Request to delete a node.

View file

@ -17,15 +17,15 @@ pub struct Request;
impl MessagePayload for Request {} impl MessagePayload for Request {}
generate_element!( /// Notes that a previous message has correctly been received, it is
/// Notes that a previous message has correctly been received, it is /// referenced by its 'id' attribute.
/// referenced by its 'id' attribute. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
Received, "received", RECEIPTS, #[xml(namespace = ns::RECEIPTS, name = "received")]
attributes: [ pub struct Received {
/// The 'id' attribute of the received message. /// The 'id' attribute of the received message.
id: Required<String> = "id", #[xml(attribute)]
] pub id: String,
); }
impl MessagePayload for Received {} impl MessagePayload for Received {}
@ -69,7 +69,10 @@ mod tests {
FromElementError::Invalid(Error::Other(string)) => string, FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(), _ => panic!(),
}; };
assert_eq!(message, "Required attribute 'id' missing."); assert_eq!(
message,
"Required attribute field 'id' on Received element missing."
);
} }
#[test] #[test]

View file

@ -4,7 +4,10 @@
// 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::message::MessagePayload; use crate::message::MessagePayload;
use crate::ns;
use jid::Jid; use jid::Jid;
generate_element!( generate_element!(
@ -22,15 +25,15 @@ generate_element!(
impl MessagePayload for StanzaId {} impl MessagePayload for StanzaId {}
generate_element!( /// A hack for MUC before version 1.31 to track a message which may have
/// A hack for MUC before version 1.31 to track a message which may have /// its 'id' attribute changed.
/// its 'id' attribute changed. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
OriginId, "origin-id", SID, #[xml(namespace = ns::SID, name = "origin-id")]
attributes: [ pub struct OriginId {
/// The id this client set for this stanza. /// The id this client set for this stanza.
id: Required<String> = "id", #[xml(attribute)]
] pub id: String,
); }
impl MessagePayload for OriginId {} impl MessagePayload for OriginId {}