1// Copyright (c) 2024 Paul Fariello <xmpp-parsers@fariello.eu>
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::{AsXml, FromXml};
8
9use crate::message::MessagePayload;
10use crate::ns;
11
12/// Defines associated out of band url.
13#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
14#[xml(namespace = ns::OOB, name = "x")]
15pub struct Oob {
16 /// The associated URL.
17 #[xml(extract(fields(text)))]
18 pub url: String,
19
20 /// An optional description of the out of band data.
21 #[xml(extract(default, fields(text(type_ = String))))]
22 pub desc: Option<String>,
23}
24
25impl MessagePayload for Oob {}
26
27#[cfg(test)]
28mod tests {
29 use super::*;
30 use minidom::Element;
31 use xso::error::{Error, FromElementError};
32
33 #[cfg(target_pointer_width = "32")]
34 #[test]
35 fn test_size() {
36 assert_size!(Oob, 24);
37 }
38
39 #[cfg(target_pointer_width = "64")]
40 #[test]
41 fn test_size() {
42 assert_size!(Oob, 48);
43 }
44
45 #[test]
46 fn test_simple() {
47 let elem: Element = "<x xmlns='jabber:x:oob'><url>http://example.org</url></x>"
48 .parse()
49 .unwrap();
50 Oob::try_from(elem).unwrap();
51 }
52
53 #[test]
54 fn test_with_desc() {
55 let elem: Element =
56 "<x xmlns='jabber:x:oob'><url>http://example.org</url><desc>Example website</desc></x>"
57 .parse()
58 .unwrap();
59 Oob::try_from(elem).unwrap();
60 }
61
62 #[test]
63 fn test_invalid_child() {
64 let elem: Element = "<x xmlns='jabber:x:oob'></x>".parse().unwrap();
65 let error = Oob::try_from(elem).unwrap_err();
66 let message = match error {
67 FromElementError::Invalid(Error::Other(string)) => string,
68 _ => panic!(),
69 };
70 assert_eq!(message, "Missing child field 'url' in Oob element.");
71 }
72}