delay.rs

  1// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
  2//
  3// This Source Code Form is subject to the terms of the Mozilla Public
  4// License, v. 2.0. If a copy of the MPL was not distributed with this
  5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6
  7use xso::{text::EmptyAsNone, AsXml, FromXml};
  8
  9use crate::date::DateTime;
 10use crate::message::MessagePayload;
 11use crate::ns;
 12use crate::presence::PresencePayload;
 13use jid::Jid;
 14
 15/// Notes when and by whom a message got stored for later delivery.
 16#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
 17#[xml(namespace = ns::DELAY, name = "delay")]
 18pub struct Delay {
 19    /// The entity which delayed this message.
 20    #[xml(attribute(default))]
 21    pub from: Option<Jid>,
 22
 23    /// The time at which this message got stored.
 24    #[xml(attribute)]
 25    pub stamp: DateTime,
 26
 27    /// The optional reason this message got delayed.
 28    #[xml(text = EmptyAsNone)]
 29    pub data: Option<String>,
 30}
 31
 32impl MessagePayload for Delay {}
 33impl PresencePayload for Delay {}
 34
 35#[cfg(test)]
 36mod tests {
 37    use super::*;
 38    use core::str::FromStr;
 39    use jid::BareJid;
 40    use minidom::Element;
 41    use xso::error::{Error, FromElementError};
 42
 43    #[cfg(target_pointer_width = "32")]
 44    #[test]
 45    fn test_size() {
 46        assert_size!(Delay, 44);
 47    }
 48
 49    #[cfg(target_pointer_width = "64")]
 50    #[test]
 51    fn test_size() {
 52        assert_size!(Delay, 72);
 53    }
 54
 55    #[test]
 56    fn test_simple() {
 57        let elem: Element =
 58            "<delay xmlns='urn:xmpp:delay' from='capulet.com' stamp='2002-09-10T23:08:25Z'/>"
 59                .parse()
 60                .unwrap();
 61        let delay = Delay::try_from(elem).unwrap();
 62        assert_eq!(delay.from.unwrap(), BareJid::new("capulet.com").unwrap());
 63        assert_eq!(
 64            delay.stamp,
 65            DateTime::from_str("2002-09-10T23:08:25Z").unwrap()
 66        );
 67        assert_eq!(delay.data, None);
 68    }
 69
 70    #[test]
 71    fn test_unknown() {
 72        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>"
 73            .parse()
 74            .unwrap();
 75        let error = Delay::try_from(elem.clone()).unwrap_err();
 76        let returned_elem = match error {
 77            FromElementError::Mismatch(elem) => elem,
 78            _ => panic!(),
 79        };
 80        assert_eq!(elem, returned_elem);
 81    }
 82
 83    #[test]
 84    #[cfg_attr(feature = "disable-validation", should_panic = "Result::unwrap_err")]
 85    fn test_invalid_child() {
 86        let elem: Element =
 87            "<delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:08:25+00:00'><coucou/></delay>"
 88                .parse()
 89                .unwrap();
 90        let error = Delay::try_from(elem).unwrap_err();
 91        let message = match error {
 92            FromElementError::Invalid(Error::Other(string)) => string,
 93            _ => panic!(),
 94        };
 95        assert_eq!(message, "Unknown child in Delay element.");
 96    }
 97
 98    #[test]
 99    fn test_serialise() {
100        let elem: Element = "<delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:08:25+00:00'/>"
101            .parse()
102            .unwrap();
103        let delay = Delay {
104            from: None,
105            stamp: DateTime::from_str("2002-09-10T23:08:25Z").unwrap(),
106            data: None,
107        };
108        let elem2 = delay.into();
109        assert_eq!(elem, elem2);
110    }
111
112    #[test]
113    fn test_serialise_data() {
114        let elem: Element = "<delay xmlns='urn:xmpp:delay' from='juliet@example.org' stamp='2002-09-10T23:08:25+00:00'>Reason</delay>".parse().unwrap();
115        let delay = Delay {
116            from: Some(Jid::new("juliet@example.org").unwrap()),
117            stamp: DateTime::from_str("2002-09-10T23:08:25Z").unwrap(),
118            data: Some(String::from("Reason")),
119        };
120        let elem2 = delay.into();
121        assert_eq!(elem, elem2);
122    }
123}