parsers: use derive macros for simple text-based elements

This commit is contained in:
Jonas Schäfer 2024-06-26 18:05:13 +02:00
parent b0803f831b
commit 298bf006bf
3 changed files with 44 additions and 36 deletions

View file

@ -4,17 +4,21 @@
// 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::iq::{IqGetPayload, IqResultPayload}; use xso::{FromXml, IntoXml};
use crate::util::text_node_codecs::{Codec, JidCodec, Text};
generate_element!( use jid::Jid;
/// Request from a client to stringprep/PRECIS a string into a JID.
JidPrepQuery, "jid", JID_PREP, use crate::iq::{IqGetPayload, IqResultPayload};
text: ( use crate::ns;
/// 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 {} impl IqGetPayload for JidPrepQuery {}
@ -25,14 +29,14 @@ impl JidPrepQuery {
} }
} }
generate_element!( /// Response from the server with the stringprepd/PRECISd JID.
/// Response from the server with the stringprepd/PRECISd JID. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
JidPrepResponse, "jid", JID_PREP, #[xml(namespace = ns::JID_PREP, name = "jid")]
text: ( pub struct JidPrepResponse {
/// The JID. /// The JID.
jid: JidCodec #[xml(text)]
) pub jid: Jid,
); }
impl IqResultPayload for JidPrepResponse {} impl IqResultPayload for JidPrepResponse {}

View file

@ -4,8 +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::util::text_node_codecs::{Codec, Text}; use crate::ns;
generate_element!( generate_element!(
/// Container for a set of reactions. /// Container for a set of reactions.
@ -22,14 +24,14 @@ generate_element!(
impl MessagePayload for Reactions {} impl MessagePayload for Reactions {}
generate_element!( /// One emoji reaction.
/// One emoji reaction. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
Reaction, "reaction", REACTIONS, #[xml(namespace = ns::REACTIONS, name = "reaction")]
text: ( pub struct Reaction {
/// The text of this reaction. /// The text of this reaction.
emoji: Text #[xml(text)]
) pub emoji: String,
); }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View file

@ -13,8 +13,10 @@
//! For vCard updates defined in [XEP-0153](https://xmpp.org/extensions/xep-0153.html), //! For vCard updates defined in [XEP-0153](https://xmpp.org/extensions/xep-0153.html),
//! see [`vcard_update`][crate::vcard_update] module. //! see [`vcard_update`][crate::vcard_update] module.
use xso::{FromXml, IntoXml};
use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload}; 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 crate::{ns, Error};
use minidom::Element; use minidom::Element;
@ -30,14 +32,14 @@ generate_element!(
] ]
); );
generate_element!( /// The type of the photo.
/// The type of the photo. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
Type, "TYPE", VCARD, #[xml(namespace = ns::VCARD, name = "TYPE")]
text: ( pub struct Type {
/// The type as a plain text string; at least "image/jpeg", "image/gif" and "image/png" SHOULD be supported. /// The type as a plain text string; at least "image/jpeg", "image/gif" and "image/png" SHOULD be supported.
data: Text #[xml(text)]
) pub data: String,
); }
generate_element!( generate_element!(
/// The binary data of the photo. /// The binary data of the photo.