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: [
/// The entity which delayed this message.
from: Option<Jid> = "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<Jid>,
/// The time at which this message got stored.
stamp: Required<DateTime> = "stamp"
],
text: (
/// The optional reason this message got delayed.
data: OptionalCodec<Text>
)
);
/// 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<String>,
}
impl MessagePayload for Delay {}
impl PresencePayload for Delay {}
@ -79,15 +82,16 @@ mod tests {
#[test]
fn test_invalid_child() {
let elem: Element = "<delay xmlns='urn:xmpp:delay'><coucou/></delay>"
.parse()
.unwrap();
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();
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]

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