jingle_grouping.rs

 1// Copyright (c) 2020 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::jingle::ContentId;
 8
 9generate_attribute!(
10    /// The semantics of the grouping.
11    Semantics, "semantics", {
12        /// Lip synchronsation.
13        Ls => "LS",
14
15        /// Bundle.
16        Bundle => "BUNDLE",
17    }
18);
19
20generate_element!(
21    /// Describes a content that should be grouped with other ones.
22    Content, "content", JINGLE_GROUPING,
23    attributes: [
24        /// The name of the matching [`Content`](crate::jingle::Content).
25        name: Required<ContentId> = "name",
26    ]
27);
28
29impl Content {
30    /// Creates a new \<content/\> element.
31    pub fn new(name: &str) -> Content {
32        Content {
33            name: ContentId(name.to_string()),
34        }
35    }
36}
37
38generate_element!(
39    /// A semantic group of contents.
40    Group, "group", JINGLE_GROUPING,
41    attributes: [
42        /// Semantics of the grouping.
43        semantics: Required<Semantics> = "semantics",
44    ],
45    children: [
46        /// List of contents that should be grouped with each other.
47        contents: Vec<Content> = ("content", JINGLE_GROUPING) => Content
48    ]
49);
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54    use crate::Element;
55    use std::convert::TryFrom;
56
57    #[cfg(target_pointer_width = "32")]
58    #[test]
59    fn test_size() {
60        assert_size!(Semantics, 1);
61        assert_size!(Content, 12);
62        assert_size!(Group, 16);
63    }
64
65    #[cfg(target_pointer_width = "64")]
66    #[test]
67    fn test_size() {
68        assert_size!(Semantics, 1);
69        assert_size!(Content, 24);
70        assert_size!(Group, 32);
71    }
72
73    #[test]
74    fn parse_group() {
75        let elem: Element = "<group xmlns='urn:xmpp:jingle:apps:grouping:0' semantics='BUNDLE'>
76            <content name='voice'/>
77            <content name='webcam'/>
78        </group>"
79            .parse()
80            .unwrap();
81        let group = Group::try_from(elem).unwrap();
82        assert_eq!(group.semantics, Semantics::Bundle);
83        assert_eq!(group.contents.len(), 2);
84        assert_eq!(
85            group.contents,
86            &[Content::new("voice"), Content::new("webcam")]
87        );
88    }
89}