sheave_core/messages/
connect_result.rs

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::io::Result as IOResult;
use super::{
    Channel,
    ChunkData,
    Command,
    headers::MessageType
};
use crate::{
    Decoder,
    Encoder,
    ByteBuffer,
    messages::amf::v0::Object,
};

/// The response message for Connect requests.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ConnectResult {
    properties: Object,
    information: Object
}

impl ConnectResult {
    /// Constructs a ConnectResult command.
    pub fn new(properties: Object, information: Object) -> Self {
        Self { properties, information }
    }

    /// Gets the properties object.
    pub fn get_properties(&self) -> &Object {
        &self.properties
    }

    /// Gets the information object.
    pub fn get_information(&self) -> &Object {
        &self.information
    }
}

impl From<ConnectResult> for (Object, Object) {
    fn from(connect_result: ConnectResult) -> Self {
        (connect_result.properties, connect_result.information)
    }
}

impl ChunkData for ConnectResult {
    const CHANNEL: Channel = Channel::System;
    const MESSAGE_TYPE: MessageType = MessageType::Command;
}

impl Command for ConnectResult {}

impl Decoder<ConnectResult> for ByteBuffer {
    /// Decodes bytes into a ConnectResult command.
    ///
    /// # Errors
    ///
    /// * [`InsufficientBufferLength`]
    ///
    /// When some field misses.
    ///
    /// * [`InconsistentMarker`]
    ///
    /// When some value is inconsistent with its marker.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use sheave_core::{
    ///     ByteBuffer,
    ///     Decoder,
    ///     Encoder,
    ///     messages::{
    ///         ConnectResult,
    ///         amf::v0::Object,
    ///     }
    /// };
    ///
    /// let mut buffer = ByteBuffer::default();
    /// buffer.encode(&Object::default());
    /// buffer.encode(&Object::default());
    /// assert!(Decoder::<ConnectResult>::decode(&mut buffer).is_ok());
    ///
    /// let mut buffer = ByteBuffer::default();
    /// assert!(Decoder::<ConnectResult>::decode(&mut buffer).is_err())
    /// ```
    ///
    /// [`InsufficientBufferLength`]: crate::byte_buffer::InsufficientBufferLength
    /// [`InconsistentMarker`]: crate::messages::amf::InconsistentMarker
    fn decode(&mut self) -> IOResult<ConnectResult> {
        let properties: Object = self.decode()?;
        let information: Object = self.decode()?;
        Ok(ConnectResult { properties, information } )
    }
}

impl Encoder<ConnectResult> for ByteBuffer {
    /// Encodes a ConnectResult command into bytes.
    fn encode(&mut self, connect_result: &ConnectResult) {
        self.encode(connect_result.get_properties());
        self.encode(connect_result.get_information());
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        messages::amf::v0::{
            Number,
            AmfString
        },
        object
    };
    use super::*;

    #[test]
    fn decode_connect_result() {
        let mut buffer = ByteBuffer::default();
        buffer.encode(
            &object!(
                "fmsVer" => AmfString::from("FMS/5,0,17"),
                "capabilities" => Number::new(31f64)
            )
        );
        buffer.encode(
            &object!(
                "level" => AmfString::from("status"),
                "code" => AmfString::from("NetConnection.Connect.Success"),
                "description" => AmfString::from("Connection succeeded."),
                "objectEncoding" => Number::new(0f64)
            )
        );
        let result: IOResult<ConnectResult> = buffer.decode();
        assert!(result.is_ok());
        let actual = result.unwrap();
        let expected = ConnectResult::new(
            object!(
                "fmsVer" => AmfString::from("FMS/5,0,17"),
                "capabilities" => Number::new(31f64)
            ),
            object!(
                "level" => AmfString::from("status"),
                "code" => AmfString::from("NetConnection.Connect.Success"),
                "description" => AmfString::from("Connection succeeded."),
                "objectEncoding" => Number::new(0f64)
            )
        );
        assert_eq!(expected, actual)
    }

    #[test]
    fn encode_connect_result() {
        let mut buffer = ByteBuffer::default();
        let expected_properties = object!(
            "fmsVer" => AmfString::from("FMS/5,0,17"),
            "capabilities" => Number::new(31f64)
        );
        let expected_information = object!(
            "level" => AmfString::from("status"),
            "code" => AmfString::from("NetConnection.Connect.Success"),
            "description" => AmfString::from("Connection succeeded."),
            "objectEncoding" => Number::new(0f64)
        );
        buffer.encode(&ConnectResult::new(expected_properties.clone(), expected_information.clone()));
        let actual_properties: Object = buffer.decode().unwrap();
        assert_eq!(expected_properties, actual_properties);
        let actual_information: Object = buffer.decode().unwrap();
        assert_eq!(expected_information, actual_information)
    }
}