xmpp-rs/parsers/src/delay.rs

123 lines
3.7 KiB
Rust
Raw Normal View History

2017-04-29 22:14:34 +01:00
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// 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/.
2024-07-09 17:01:42 +02:00
use xso::{text::EmptyAsNone, AsXml, FromXml};
2018-12-18 15:32:05 +01:00
use crate::date::DateTime;
use crate::message::MessagePayload;
use crate::ns;
use crate::presence::PresencePayload;
2017-05-01 01:50:18 +01:00
use jid::Jid;
2017-04-21 03:57:34 +01:00
/// Notes when and by whom a message got stored for later delivery.
2024-07-09 17:01:42 +02:00
#[derive(FromXml, AsXml, 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.
#[xml(attribute)]
pub stamp: DateTime,
2018-08-02 18:33:37 +02:00
/// The optional reason this message got delayed.
#[xml(text = EmptyAsNone)]
pub data: Option<String>,
}
2017-04-29 03:50:02 +01:00
impl MessagePayload for Delay {}
impl PresencePayload for Delay {}
2017-04-21 03:57:34 +01:00
#[cfg(test)]
mod tests {
2017-05-06 20:42:12 +01:00
use super::*;
use core::str::FromStr;
use jid::BareJid;
use minidom::Element;
use xso::error::{Error, FromElementError};
2017-04-21 03:57:34 +01:00
2018-10-28 13:10:48 +01:00
#[cfg(target_pointer_width = "32")]
#[test]
fn test_size() {
assert_size!(Delay, 44);
2018-10-28 13:10:48 +01:00
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_size() {
assert_size!(Delay, 72);
}
2017-04-21 03:57:34 +01:00
#[test]
fn test_simple() {
2018-12-18 15:32:05 +01:00
let elem: Element =
"<delay xmlns='urn:xmpp:delay' from='capulet.com' stamp='2002-09-10T23:08:25Z'/>"
.parse()
.unwrap();
let delay = Delay::try_from(elem).unwrap();
assert_eq!(delay.from.unwrap(), BareJid::new("capulet.com").unwrap());
2018-12-18 15:32:05 +01:00
assert_eq!(
delay.stamp,
DateTime::from_str("2002-09-10T23:08:25Z").unwrap()
);
2017-04-21 03:57:34 +01:00
assert_eq!(delay.data, None);
}
#[test]
fn test_unknown() {
2018-12-18 15:32:05 +01:00
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>"
.parse()
.unwrap();
let error = Delay::try_from(elem.clone()).unwrap_err();
let returned_elem = match error {
FromElementError::Mismatch(elem) => elem,
2017-04-21 03:57:34 +01:00
_ => panic!(),
};
assert_eq!(elem, returned_elem);
2017-04-21 03:57:34 +01:00
}
#[test]
#[cfg_attr(feature = "disable-validation", should_panic = "Result::unwrap_err")]
2017-04-21 03:57:34 +01:00
fn test_invalid_child() {
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();
2017-04-21 03:57:34 +01:00
let message = match error {
FromElementError::Invalid(Error::Other(string)) => string,
2017-04-21 03:57:34 +01:00
_ => panic!(),
};
assert_eq!(message, "Unknown child in Delay element.");
2017-04-21 03:57:34 +01:00
}
2017-04-23 03:21:53 +01:00
#[test]
fn test_serialise() {
2018-12-18 15:32:05 +01:00
let elem: Element = "<delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:08:25+00:00'/>"
.parse()
.unwrap();
2017-05-06 20:42:12 +01:00
let delay = Delay {
2017-04-23 03:21:53 +01:00
from: None,
stamp: DateTime::from_str("2002-09-10T23:08:25Z").unwrap(),
2017-04-23 03:21:53 +01:00
data: None,
};
let elem2 = delay.into();
2017-04-23 03:21:53 +01:00
assert_eq!(elem, elem2);
}
#[test]
fn test_serialise_data() {
2017-05-27 12:29:21 +01:00
let elem: Element = "<delay xmlns='urn:xmpp:delay' from='juliet@example.org' stamp='2002-09-10T23:08:25+00:00'>Reason</delay>".parse().unwrap();
2017-05-06 20:42:12 +01:00
let delay = Delay {
from: Some(Jid::new("juliet@example.org").unwrap()),
stamp: DateTime::from_str("2002-09-10T23:08:25Z").unwrap(),
2017-04-23 03:21:53 +01:00
data: Some(String::from("Reason")),
};
let elem2 = delay.into();
2017-04-23 03:21:53 +01:00
assert_eq!(elem, elem2);
}
2017-04-21 03:57:34 +01:00
}