parsers: make use of text codecs and derive more things

This commit is contained in:
Jonas Schäfer 2024-06-26 18:29:37 +02:00
parent c83ff286e0
commit 4ec12fab16
2 changed files with 37 additions and 34 deletions

View file

@ -4,27 +4,30 @@
// 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::{text::EmptyAsNone, FromXml, IntoXml};
use crate::date::DateTime; use crate::date::DateTime;
use crate::message::MessagePayload; use crate::message::MessagePayload;
use crate::ns;
use crate::presence::PresencePayload; use crate::presence::PresencePayload;
use crate::util::text_node_codecs::{Codec, OptionalCodec, Text};
use jid::Jid; use jid::Jid;
generate_element!( /// Notes when and by whom a message got stored for later delivery.
/// Notes when and by whom a message got stored for later delivery. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
Delay, "delay", DELAY, #[xml(namespace = ns::DELAY, name = "delay")]
attributes: [ pub struct Delay {
/// The entity which delayed this message. /// The entity which delayed this message.
from: Option<Jid> = "from", #[xml(attribute(default))]
pub from: Option<Jid>,
/// The time at which this message got stored. /// The time at which this message got stored.
stamp: Required<DateTime> = "stamp" #[xml(attribute)]
], pub stamp: DateTime,
text: (
/// The optional reason this message got delayed. /// The optional reason this message got delayed.
data: OptionalCodec<Text> #[xml(text = EmptyAsNone)]
) pub data: Option<String>,
); }
impl MessagePayload for Delay {} impl MessagePayload for Delay {}
impl PresencePayload for Delay {} impl PresencePayload for Delay {}
@ -79,15 +82,16 @@ mod tests {
#[test] #[test]
fn test_invalid_child() { fn test_invalid_child() {
let elem: Element = "<delay xmlns='urn:xmpp:delay'><coucou/></delay>" let elem: Element =
.parse() "<delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:08:25+00:00'><coucou/></delay>"
.unwrap(); .parse()
.unwrap();
let error = Delay::try_from(elem).unwrap_err(); let error = Delay::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 delay element."); assert_eq!(message, "Unknown child in Delay element.");
} }
#[test] #[test]

View file

@ -4,10 +4,9 @@
// 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 xso::{text::EmptyAsNone, FromXml, IntoXml};
use crate::ns; use crate::ns;
use crate::util::text_node_codecs::{Codec, OptionalCodec, Text};
use crate::Element; use crate::Element;
use xso::error::{Error, FromElementError}; use xso::error::{Error, FromElementError};
@ -31,19 +30,19 @@ generate_attribute!(
}, Default = Edit }, Default = Edit
); );
generate_element!( /// Supports the transmission of text, including key presses, and text block inserts.
/// Supports the transmission of text, including key presses, and text block inserts. #[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
Insert, "t", RTT, #[xml(namespace = ns::RTT, name = "t")]
attributes: [ pub struct Insert {
/// Position in the message to start inserting from. If None, this means to start from the /// Position in the message to start inserting from. If None, this means to start from the
/// end of the message. /// end of the message.
pos: Option<u32> = "p", #[xml(attribute(default, name = "p"))]
], pub pos: Option<u32>,
text: (
/// Text to insert. /// Text to insert.
text: OptionalCodec<Text> #[xml(text = EmptyAsNone)]
) pub text: Option<String>,
); }
impl TryFrom<Action> for Insert { impl TryFrom<Action> for Insert {
type Error = Error; type Error = Error;