From 4ec12fab1689e4f4520309a098a37ef09ffe4b3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Sch=C3=A4fer?= Date: Wed, 26 Jun 2024 18:29:37 +0200 Subject: [PATCH] parsers: make use of text codecs and derive more things --- parsers/src/delay.rs | 42 +++++++++++++++++++++++------------------- parsers/src/rtt.rs | 29 ++++++++++++++--------------- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/parsers/src/delay.rs b/parsers/src/delay.rs index 53532c45..869b181a 100644 --- a/parsers/src/delay.rs +++ b/parsers/src/delay.rs @@ -4,27 +4,30 @@ // 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::{text::EmptyAsNone, FromXml, IntoXml}; + use crate::date::DateTime; use crate::message::MessagePayload; +use crate::ns; use crate::presence::PresencePayload; -use crate::util::text_node_codecs::{Codec, OptionalCodec, Text}; use jid::Jid; -generate_element!( - /// Notes when and by whom a message got stored for later delivery. - Delay, "delay", DELAY, - attributes: [ - /// The entity which delayed this message. - from: Option = "from", +/// Notes when and by whom a message got stored for later delivery. +#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)] +#[xml(namespace = ns::DELAY, name = "delay")] +pub struct Delay { + /// The entity which delayed this message. + #[xml(attribute(default))] + pub from: Option, - /// The time at which this message got stored. - stamp: Required = "stamp" - ], - text: ( - /// The optional reason this message got delayed. - data: OptionalCodec - ) -); + /// The time at which this message got stored. + #[xml(attribute)] + pub stamp: DateTime, + + /// The optional reason this message got delayed. + #[xml(text = EmptyAsNone)] + pub data: Option, +} impl MessagePayload for Delay {} impl PresencePayload for Delay {} @@ -79,15 +82,16 @@ mod tests { #[test] fn test_invalid_child() { - let elem: Element = "" - .parse() - .unwrap(); + let elem: Element = + "" + .parse() + .unwrap(); let error = Delay::try_from(elem).unwrap_err(); let message = match error { FromElementError::Invalid(Error::Other(string)) => string, _ => panic!(), }; - assert_eq!(message, "Unknown child in delay element."); + assert_eq!(message, "Unknown child in Delay element."); } #[test] diff --git a/parsers/src/rtt.rs b/parsers/src/rtt.rs index a9d5ac75..9def1aa0 100644 --- a/parsers/src/rtt.rs +++ b/parsers/src/rtt.rs @@ -4,10 +4,9 @@ // 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 xso::{text::EmptyAsNone, FromXml, IntoXml}; use crate::ns; -use crate::util::text_node_codecs::{Codec, OptionalCodec, Text}; use crate::Element; use xso::error::{Error, FromElementError}; @@ -31,19 +30,19 @@ generate_attribute!( }, Default = Edit ); -generate_element!( - /// Supports the transmission of text, including key presses, and text block inserts. - Insert, "t", RTT, - attributes: [ - /// Position in the message to start inserting from. If None, this means to start from the - /// end of the message. - pos: Option = "p", - ], - text: ( - /// Text to insert. - text: OptionalCodec - ) -); +/// Supports the transmission of text, including key presses, and text block inserts. +#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)] +#[xml(namespace = ns::RTT, name = "t")] +pub struct Insert { + /// Position in the message to start inserting from. If None, this means to start from the + /// end of the message. + #[xml(attribute(default, name = "p"))] + pub pos: Option, + + /// Text to insert. + #[xml(text = EmptyAsNone)] + pub text: Option, +} impl TryFrom for Insert { type Error = Error;