1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use crate::core::{FromXml, IntoXml};
use crate::jingle::{Description, SessionId};
use crate::ns::JINGLE_MESSAGE;

/// Defines a protocol for broadcasting Jingle requests to all of the clients
/// of a user.
#[derive(FromXml, IntoXml, Debug, Clone)]
#[xml(namespace = JINGLE_MESSAGE)]
pub enum JingleMI {
    /// Indicates we want to start a Jingle session.
    #[xml(name = "propose")]
    Propose {
        /// The generated session identifier, must be unique between two users.
        #[xml(attribute(name = "id"))]
        sid: SessionId,

        /// The application description of the proposed session.
        #[xml(child)]
        description: Description,
    },

    /// Cancels a previously proposed session.
    #[xml(name = "retract")]
    Retract(#[xml(attribute(name = "id"))] SessionId),

    /// Accepts a session proposed by the other party.
    #[xml(name = "accept")]
    Accept(#[xml(attribute(name = "id"))] SessionId),

    /// Proceed with a previously proposed session.
    #[xml(name = "proceed")]
    Proceed(#[xml(attribute(name = "id"))] SessionId),

    /// Rejects a session proposed by the other party.
    #[xml(name = "reject")]
    Reject(#[xml(attribute(name = "id"))] SessionId),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::error::Error;
    use crate::Element;

    #[cfg(target_pointer_width = "32")]
    #[test]
    fn test_size() {
        assert_size!(JingleMI, 88);
    }

    #[cfg(target_pointer_width = "64")]
    #[test]
    fn test_size() {
        assert_size!(JingleMI, 176);
    }

    #[test]
    fn test_simple() {
        let elem: Element = "<accept xmlns='urn:xmpp:jingle-message:0' id='coucou'/>"
            .parse()
            .unwrap();
        JingleMI::try_from(elem).unwrap();
    }

    #[test]
    fn test_invalid_child() {
        let elem: Element =
            "<propose xmlns='urn:xmpp:jingle-message:0' id='coucou'><coucou/></propose>"
                .parse()
                .unwrap();
        let error = JingleMI::try_from(elem).unwrap_err();
        let message = match error {
            Error::ParseError(string) => string,
            _ => panic!(),
        };
        assert_eq!(message, "Unknown child in JingleMI::Propose element.");
    }
}