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 crate::delay::Delay;
8use crate::message::Message;
9
10generate_element!(
11 /// Contains a forwarded stanza, either standalone or part of another
12 /// extension (such as carbons).
13 Forwarded, "forwarded", FORWARD,
14 children: [
15 /// When the stanza originally got sent.
16 delay: Option<Delay> = ("delay", DELAY) => Delay,
17
18 // XXX: really? Option?
19 /// The stanza being forwarded.
20 stanza: Option<Message> = ("message", DEFAULT_NS) => Message
21
22 // TODO: also handle the two other stanza possibilities.
23 ]
24);
25
26#[cfg(test)]
27mod tests {
28 use super::*;
29 use crate::util::error::Error;
30 use crate::Element;
31 use std::convert::TryFrom;
32
33 #[cfg(target_pointer_width = "32")]
34 #[test]
35 fn test_size() {
36 assert_size!(Forwarded, 212);
37 }
38
39 #[cfg(target_pointer_width = "64")]
40 #[test]
41 fn test_size() {
42 assert_size!(Forwarded, 408);
43 }
44
45 #[test]
46 fn test_simple() {
47 let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'/>".parse().unwrap();
48 Forwarded::try_from(elem).unwrap();
49 }
50
51 #[test]
52 fn test_invalid_child() {
53 let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'><coucou/></forwarded>"
54 .parse()
55 .unwrap();
56 let error = Forwarded::try_from(elem).unwrap_err();
57 let message = match error {
58 Error::ParseError(string) => string,
59 _ => panic!(),
60 };
61 assert_eq!(message, "Unknown child in forwarded element.");
62 }
63
64 #[test]
65 fn test_serialise() {
66 let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'/>".parse().unwrap();
67 let forwarded = Forwarded {
68 delay: None,
69 stanza: None,
70 };
71 let elem2 = forwarded.into();
72 assert_eq!(elem, elem2);
73 }
74}