xmpp_parsers/
jingle_message.rs1use crate::jingle::SessionId;
8use crate::ns;
9use minidom::Element;
10use xso::error::{Error, FromElementError};
11
12#[derive(Debug, Clone)]
15pub enum JingleMI {
16 Propose {
18 sid: SessionId,
20
21 description: Element,
24 },
25
26 Retract(SessionId),
28
29 Accept(SessionId),
31
32 Proceed(SessionId),
34
35 Reject(SessionId),
37}
38
39fn get_sid(elem: Element) -> Result<SessionId, Error> {
40 check_no_unknown_attributes!(elem, "Jingle message", ["id"]);
41 Ok(SessionId(get_attr!(elem, "id", Required)))
42}
43
44fn check_empty_and_get_sid(elem: Element) -> Result<SessionId, Error> {
45 check_no_children!(elem, "Jingle message");
46 get_sid(elem)
47}
48
49impl TryFrom<Element> for JingleMI {
50 type Error = FromElementError;
51
52 fn try_from(elem: Element) -> Result<JingleMI, FromElementError> {
53 if !elem.has_ns(ns::JINGLE_MESSAGE) {
54 return Err(Error::Other("This is not a Jingle message element.").into());
55 }
56 Ok(match elem.name() {
57 "propose" => {
58 let mut description = None;
59 for child in elem.children() {
60 if child.name() != "description" {
61 return Err(Error::Other("Unknown child in propose element.").into());
62 }
63 if description.is_some() {
64 return Err(Error::Other("Too many children in propose element.").into());
65 }
66 description = Some(child.clone());
67 }
68 JingleMI::Propose {
69 sid: get_sid(elem)?,
70 description: description.ok_or(Error::Other(
71 "Propose element doesn’t contain a description.",
72 ))?,
73 }
74 }
75 "retract" => JingleMI::Retract(check_empty_and_get_sid(elem)?),
76 "accept" => JingleMI::Accept(check_empty_and_get_sid(elem)?),
77 "proceed" => JingleMI::Proceed(check_empty_and_get_sid(elem)?),
78 "reject" => JingleMI::Reject(check_empty_and_get_sid(elem)?),
79 _ => return Err(Error::Other("This is not a Jingle message element.").into()),
80 })
81 }
82}
83
84impl From<JingleMI> for Element {
85 fn from(jingle_mi: JingleMI) -> Element {
86 match jingle_mi {
87 JingleMI::Propose { sid, description } => {
88 Element::builder("propose", ns::JINGLE_MESSAGE)
89 .attr("id", sid)
90 .append(description)
91 }
92 JingleMI::Retract(sid) => {
93 Element::builder("retract", ns::JINGLE_MESSAGE).attr("id", sid)
94 }
95 JingleMI::Accept(sid) => Element::builder("accept", ns::JINGLE_MESSAGE).attr("id", sid),
96 JingleMI::Proceed(sid) => {
97 Element::builder("proceed", ns::JINGLE_MESSAGE).attr("id", sid)
98 }
99 JingleMI::Reject(sid) => Element::builder("reject", ns::JINGLE_MESSAGE).attr("id", sid),
100 }
101 .build()
102 }
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[cfg(target_pointer_width = "32")]
110 #[test]
111 fn test_size() {
112 assert_size!(JingleMI, 72);
113 }
114
115 #[cfg(target_pointer_width = "64")]
116 #[test]
117 fn test_size() {
118 assert_size!(JingleMI, 144);
119 }
120
121 #[test]
122 fn test_simple() {
123 let elem: Element = "<accept xmlns='urn:xmpp:jingle-message:0' id='coucou'/>"
124 .parse()
125 .unwrap();
126 JingleMI::try_from(elem).unwrap();
127 }
128
129 #[test]
130 fn test_invalid_child() {
131 let elem: Element =
132 "<propose xmlns='urn:xmpp:jingle-message:0' id='coucou'><coucou/></propose>"
133 .parse()
134 .unwrap();
135 let error = JingleMI::try_from(elem).unwrap_err();
136 let message = match error {
137 FromElementError::Invalid(Error::Other(string)) => string,
138 _ => panic!(),
139 };
140 assert_eq!(message, "Unknown child in propose element.");
141 }
142}