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
// 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: [
#[derive(FromXml, IntoXml, PartialEq, Debug, Clone)]
#[xml(namespace = ns::DELAY, name = "delay")]
pub struct Delay {
/// 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.
stamp: Required<DateTime> = "stamp"
],
text: (
#[xml(attribute)]
pub stamp: DateTime,
/// The optional reason this message got delayed.
data: OptionalCodec<Text>
)
);
#[xml(text = EmptyAsNone)]
pub data: Option<String>,
}
impl MessagePayload for Delay {}
impl PresencePayload for Delay {}
@ -79,7 +82,8 @@ mod tests {
#[test]
fn test_invalid_child() {
let elem: Element = "<delay xmlns='urn:xmpp:delay'><coucou/></delay>"
let elem: Element =
"<delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:08:25+00:00'><coucou/></delay>"
.parse()
.unwrap();
let error = Delay::try_from(elem).unwrap_err();
@ -87,7 +91,7 @@ mod tests {
FromElementError::Invalid(Error::Other(string)) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown child in delay element.");
assert_eq!(message, "Unknown child in Delay element.");
}
#[test]

View file

@ -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: [
#[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.
pos: Option<u32> = "p",
],
text: (
#[xml(attribute(default, name = "p"))]
pub pos: Option<u32>,
/// Text to insert.
text: OptionalCodec<Text>
)
);
#[xml(text = EmptyAsNone)]
pub text: Option<String>,
}
impl TryFrom<Action> for Insert {
type Error = Error;