From 298bf006bffc9db7714ef5f0a9ce8af0a25ab41a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Sch=C3=A4fer?= Date: Wed, 26 Jun 2024 18:05:13 +0200 Subject: [PATCH] parsers: use derive macros for simple text-based elements --- parsers/src/jid_prep.rs | 38 +++++++++++++++++++++----------------- parsers/src/reactions.rs | 20 +++++++++++--------- parsers/src/vcard.rs | 20 +++++++++++--------- 3 files changed, 43 insertions(+), 35 deletions(-) diff --git a/parsers/src/jid_prep.rs b/parsers/src/jid_prep.rs index e082a39a8858b6294275e9ede2d40742a431f4f7..97a66dc02bda5f328346f2c55cbd0a1794eb09a3 100644 --- a/parsers/src/jid_prep.rs +++ b/parsers/src/jid_prep.rs @@ -4,17 +4,21 @@ // 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 jid::Jid; + use crate::iq::{IqGetPayload, IqResultPayload}; -use crate::util::text_node_codecs::{Codec, JidCodec, Text}; +use crate::ns; -generate_element!( - /// Request from a client to stringprep/PRECIS a string into a JID. - JidPrepQuery, "jid", JID_PREP, - text: ( - /// The potential JID. - data: Text - ) -); +/// Request from a client to stringprep/PRECIS a string into a JID. +#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)] +#[xml(namespace = ns::JID_PREP, name = "jid")] +pub struct JidPrepQuery { + /// The potential JID. + #[xml(text)] + pub data: String, +} impl IqGetPayload for JidPrepQuery {} @@ -25,14 +29,14 @@ impl JidPrepQuery { } } -generate_element!( - /// Response from the server with the stringprep’d/PRECIS’d JID. - JidPrepResponse, "jid", JID_PREP, - text: ( - /// The JID. - jid: JidCodec - ) -); +/// Response from the server with the stringprep’d/PRECIS’d JID. +#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)] +#[xml(namespace = ns::JID_PREP, name = "jid")] +pub struct JidPrepResponse { + /// The JID. + #[xml(text)] + pub jid: Jid, +} impl IqResultPayload for JidPrepResponse {} diff --git a/parsers/src/reactions.rs b/parsers/src/reactions.rs index aff6932dfafc75c513cc8fe5b2692452b534d978..90806014f67824476ccbf5c933479aa07ad6fdd8 100644 --- a/parsers/src/reactions.rs +++ b/parsers/src/reactions.rs @@ -4,8 +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::util::text_node_codecs::{Codec, Text}; +use crate::ns; generate_element!( /// Container for a set of reactions. @@ -22,14 +24,14 @@ generate_element!( impl MessagePayload for Reactions {} -generate_element!( - /// One emoji reaction. - Reaction, "reaction", REACTIONS, - text: ( - /// The text of this reaction. - emoji: Text - ) -); +/// One emoji reaction. +#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)] +#[xml(namespace = ns::REACTIONS, name = "reaction")] +pub struct Reaction { + /// The text of this reaction. + #[xml(text)] + pub emoji: String, +} #[cfg(test)] mod tests { diff --git a/parsers/src/vcard.rs b/parsers/src/vcard.rs index 96241a52b2a5aab26f4a301857aded8a8fd68624..a11d5d2c21fabf035f0e39ada3f4953fa0505a75 100644 --- a/parsers/src/vcard.rs +++ b/parsers/src/vcard.rs @@ -13,8 +13,10 @@ //! For vCard updates defined in [XEP-0153](https://xmpp.org/extensions/xep-0153.html), //! see [`vcard_update`][crate::vcard_update] module. +use xso::{FromXml, IntoXml}; + use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload}; -use crate::util::text_node_codecs::{Codec, Text, WhitespaceAwareBase64}; +use crate::util::text_node_codecs::{Codec, WhitespaceAwareBase64}; use crate::{ns, Error}; use minidom::Element; @@ -30,14 +32,14 @@ generate_element!( ] ); -generate_element!( - /// The type of the photo. - Type, "TYPE", VCARD, - text: ( - /// The type as a plain text string; at least "image/jpeg", "image/gif" and "image/png" SHOULD be supported. - data: Text - ) -); +/// The type of the photo. +#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)] +#[xml(namespace = ns::VCARD, name = "TYPE")] +pub struct Type { + /// The type as a plain text string; at least "image/jpeg", "image/gif" and "image/png" SHOULD be supported. + #[xml(text)] + pub data: String, +} generate_element!( /// The binary data of the photo.