From 0bae5d3346c9aebf506f5ee9fcbe44220e98d5f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Sch=C3=A4fer?= Date: Sun, 23 Jun 2024 09:52:41 +0200 Subject: [PATCH] parsers: replace some generate_element! usage with derive macros --- parsers/src/http_upload.rs | 18 ++++++++------- parsers/src/message_correct.rs | 39 +++++++++++++++++++-------------- parsers/src/occupant_id.rs | 40 +++++++++++++++++++--------------- parsers/src/pubsub/owner.rs | 18 ++++++++------- parsers/src/receipts.rs | 23 ++++++++++--------- parsers/src/stanza_id.rs | 21 ++++++++++-------- 6 files changed, 91 insertions(+), 68 deletions(-) diff --git a/parsers/src/http_upload.rs b/parsers/src/http_upload.rs index 839052345df055ca5ea2bced4f3573ae3a538707..eacd08775bc240fc3cd0189aaa51cb71cdd7a4d2 100644 --- a/parsers/src/http_upload.rs +++ b/parsers/src/http_upload.rs @@ -4,6 +4,8 @@ // 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/. +use xso::{FromXml, IntoXml}; + use crate::iq::{IqGetPayload, IqResultPayload}; use crate::ns; use crate::Element; @@ -90,14 +92,14 @@ generate_element!( ] ); -generate_element!( - /// Get URL - Get, "get", HTTP_UPLOAD, - attributes: [ - /// URL - url: Required = "url", - ] -); +/// Get URL +#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)] +#[xml(namespace = ns::HTTP_UPLOAD, name = "get")] +pub struct Get { + /// URL + #[xml(attribute)] + pub url: String, +} generate_element!( /// Requesting a slot diff --git a/parsers/src/message_correct.rs b/parsers/src/message_correct.rs index 7db218b6dcdffedca2f5bded2d32a48586446f35..20d0dd3a310a28658480a97d687acccf1afeed55 100644 --- a/parsers/src/message_correct.rs +++ b/parsers/src/message_correct.rs @@ -4,17 +4,20 @@ // 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/. +use xso::{FromXml, IntoXml}; + use crate::message::MessagePayload; +use crate::ns; -generate_element!( - /// Defines that the message containing this payload should replace a - /// previous message, identified by the id. - Replace, "replace", MESSAGE_CORRECT, - attributes: [ - /// The 'id' attribute of the message getting corrected. - id: Required = "id", - ] -); +/// Defines that the message containing this payload should replace a +/// previous message, identified by the id. +#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)] +#[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 {} @@ -47,7 +50,7 @@ mod tests { #[cfg(not(feature = "disable-validation"))] #[test] fn test_invalid_attribute() { - let elem: Element = "" + let elem: Element = "" .parse() .unwrap(); let error = Replace::try_from(elem).unwrap_err(); @@ -55,20 +58,21 @@ mod tests { FromElementError::Invalid(Error::Other(string)) => string, _ => panic!(), }; - assert_eq!(message, "Unknown attribute in replace element."); + assert_eq!(message, "Unknown attribute in Replace element."); } #[test] fn test_invalid_child() { - let elem: Element = "" - .parse() - .unwrap(); + let elem: Element = + "" + .parse() + .unwrap(); let error = Replace::try_from(elem).unwrap_err(); let message = match error { FromElementError::Invalid(Error::Other(string)) => string, _ => panic!(), }; - assert_eq!(message, "Unknown child in replace element."); + assert_eq!(message, "Unknown child in Replace element."); } #[test] @@ -81,7 +85,10 @@ mod tests { FromElementError::Invalid(Error::Other(string)) => string, _ => panic!(), }; - assert_eq!(message, "Required attribute 'id' missing."); + assert_eq!( + message, + "Required attribute field 'id' on Replace element missing." + ); } #[test] diff --git a/parsers/src/occupant_id.rs b/parsers/src/occupant_id.rs index 07f72a14b03980e97d6c76b20d569b42e0428b03..44780fc6dbc7b5fb2f5fbcb609323b163a37470e 100644 --- a/parsers/src/occupant_id.rs +++ b/parsers/src/occupant_id.rs @@ -4,21 +4,23 @@ // 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/. +use xso::{FromXml, IntoXml}; + use crate::message::MessagePayload; +use crate::ns; use crate::presence::PresencePayload; -generate_element!( - /// Unique identifier given to a MUC participant. - /// - /// It allows clients to identify a MUC participant across reconnects and - /// renames. It thus prevents impersonification of anonymous users. - OccupantId, "occupant-id", OID, - - attributes: [ - /// The id associated to the sending user by the MUC service. - id: Required = "id", - ] -); +/// Unique identifier given to a MUC participant. +/// +/// It allows clients to identify a MUC participant across reconnects and +/// renames. It thus prevents impersonification of anonymous users. +#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)] +#[xml(namespace = ns::OID, name = "occupant-id")] +pub struct OccupantId { + /// The id associated to the sending user by the MUC service. + #[xml(attribute)] + pub id: String, +} impl MessagePayload for OccupantId {} impl PresencePayload for OccupantId {} @@ -52,15 +54,16 @@ mod tests { #[test] fn test_invalid_child() { - let elem: Element = "" - .parse() - .unwrap(); + let elem: Element = + "" + .parse() + .unwrap(); let error = OccupantId::try_from(elem).unwrap_err(); let message = match error { FromElementError::Invalid(Error::Other(string)) => string, _ => panic!(), }; - assert_eq!(message, "Unknown child in occupant-id element."); + assert_eq!(message, "Unknown child in OccupantId element."); } #[test] @@ -73,7 +76,10 @@ mod tests { FromElementError::Invalid(Error::Other(string)) => string, _ => panic!(), }; - assert_eq!(message, "Required attribute 'id' missing."); + assert_eq!( + message, + "Required attribute field 'id' on OccupantId element missing." + ); } #[test] diff --git a/parsers/src/pubsub/owner.rs b/parsers/src/pubsub/owner.rs index 75295235ba0c4aaf2f1ecd0790abdb966f56712c..7f295731d3abb0ab195113ecf9375d383eab44ed 100644 --- a/parsers/src/pubsub/owner.rs +++ b/parsers/src/pubsub/owner.rs @@ -5,6 +5,8 @@ // 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/. +use xso::{FromXml, IntoXml}; + use crate::data_forms::DataForm; use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload}; use crate::ns; @@ -73,14 +75,14 @@ generate_element!( ] ); -generate_element!( - /// A redirect element. - Redirect, "redirect", PUBSUB_OWNER, - attributes: [ - /// The node this node will be redirected to. - uri: Required = "uri", - ] -); +/// A redirect element. +#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)] +#[xml(namespace = ns::PUBSUB_OWNER, name = "redirect")] +pub struct Redirect { + /// The node this node will be redirected to. + #[xml(attribute)] + pub uri: String, +} generate_element!( /// Request to delete a node. diff --git a/parsers/src/receipts.rs b/parsers/src/receipts.rs index 4b21a4d38446569e721feca098f56c4059df60da..83512b9dd22458c09f0da36f3686a73b489d6f8b 100644 --- a/parsers/src/receipts.rs +++ b/parsers/src/receipts.rs @@ -17,15 +17,15 @@ pub struct Request; impl MessagePayload for Request {} -generate_element!( - /// Notes that a previous message has correctly been received, it is - /// referenced by its 'id' attribute. - Received, "received", RECEIPTS, - attributes: [ - /// The 'id' attribute of the received message. - id: Required = "id", - ] -); +/// Notes that a previous message has correctly been received, it is +/// referenced by its 'id' attribute. +#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)] +#[xml(namespace = ns::RECEIPTS, name = "received")] +pub struct Received { + /// The 'id' attribute of the received message. + #[xml(attribute)] + pub id: String, +} impl MessagePayload for Received {} @@ -69,7 +69,10 @@ mod tests { FromElementError::Invalid(Error::Other(string)) => string, _ => panic!(), }; - assert_eq!(message, "Required attribute 'id' missing."); + assert_eq!( + message, + "Required attribute field 'id' on Received element missing." + ); } #[test] diff --git a/parsers/src/stanza_id.rs b/parsers/src/stanza_id.rs index d3911e0444fb7bed8ae48d1de17f0f1f4e58926f..22b6e4e733d275e953524a3241a6c99f1f03a5ff 100644 --- a/parsers/src/stanza_id.rs +++ b/parsers/src/stanza_id.rs @@ -4,7 +4,10 @@ // 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/. +use xso::{FromXml, IntoXml}; + use crate::message::MessagePayload; +use crate::ns; use jid::Jid; generate_element!( @@ -22,15 +25,15 @@ generate_element!( impl MessagePayload for StanzaId {} -generate_element!( - /// A hack for MUC before version 1.31 to track a message which may have - /// its 'id' attribute changed. - OriginId, "origin-id", SID, - attributes: [ - /// The id this client set for this stanza. - id: Required = "id", - ] -); +/// A hack for MUC before version 1.31 to track a message which may have +/// its 'id' attribute changed. +#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)] +#[xml(namespace = ns::SID, name = "origin-id")] +pub struct OriginId { + /// The id this client set for this stanza. + #[xml(attribute)] + pub id: String, +} impl MessagePayload for OriginId {}