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 jid::BareJid;
 39    use minidom::Element;
 40    use std::str::FromStr;
 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    fn test_invalid_child() {
 85        let elem: Element =
 86            "<delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:08:25+00:00'><coucou/></delay>"
 87                .parse()
 88                .unwrap();
 89        let error = Delay::try_from(elem).unwrap_err();
 90        let message = match error {
 91            FromElementError::Invalid(Error::Other(string)) => string,
 92            _ => panic!(),
 93        };
 94        assert_eq!(message, "Unknown child in Delay element.");
 95    }
 96
 97    #[test]
 98    fn test_serialise() {
 99        let elem: Element = "<delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:08:25+00:00'/>"
100            .parse()
101            .unwrap();
102        let delay = Delay {
103            from: None,
104            stamp: DateTime::from_str("2002-09-10T23:08:25Z").unwrap(),
105            data: None,
106        };
107        let elem2 = delay.into();
108        assert_eq!(elem, elem2);
109    }
110
111    #[test]
112    fn test_serialise_data() {
113        let elem: Element = "<delay xmlns='urn:xmpp:delay' from='juliet@example.org' stamp='2002-09-10T23:08:25+00:00'>Reason</delay>".parse().unwrap();
114        let delay = Delay {
115            from: Some(Jid::new("juliet@example.org").unwrap()),
116            stamp: DateTime::from_str("2002-09-10T23:08:25Z").unwrap(),
117            data: Some(String::from("Reason")),
118        };
119        let elem2 = delay.into();
120        assert_eq!(elem, elem2);
121    }
122}