sheave_core/messages/
connect_result.rs

1use std::io::Result as IOResult;
2use super::{
3    Channel,
4    ChunkData,
5    Command,
6    headers::MessageType
7};
8use crate::{
9    Decoder,
10    Encoder,
11    ByteBuffer,
12    messages::amf::v0::Object,
13};
14
15/// The response message for Connect requests.
16#[derive(Debug, Clone, Default, PartialEq)]
17pub struct ConnectResult {
18    properties: Object,
19    information: Object
20}
21
22impl ConnectResult {
23    /// Constructs a ConnectResult command.
24    pub fn new(properties: Object, information: Object) -> Self {
25        Self { properties, information }
26    }
27
28    /// Gets the properties object.
29    pub fn get_properties(&self) -> &Object {
30        &self.properties
31    }
32
33    /// Gets the information object.
34    pub fn get_information(&self) -> &Object {
35        &self.information
36    }
37}
38
39impl From<ConnectResult> for (Object, Object) {
40    fn from(connect_result: ConnectResult) -> Self {
41        (connect_result.properties, connect_result.information)
42    }
43}
44
45impl ChunkData for ConnectResult {
46    const CHANNEL: Channel = Channel::System;
47    const MESSAGE_TYPE: MessageType = MessageType::Command;
48}
49
50impl Command for ConnectResult {}
51
52impl Decoder<ConnectResult> for ByteBuffer {
53    /// Decodes bytes into a ConnectResult command.
54    ///
55    /// # Errors
56    ///
57    /// * [`InsufficientBufferLength`]
58    ///
59    /// When some field misses.
60    ///
61    /// * [`InconsistentMarker`]
62    ///
63    /// When some value is inconsistent with its marker.
64    ///
65    /// # Examples
66    ///
67    /// ```rust
68    /// use sheave_core::{
69    ///     ByteBuffer,
70    ///     Decoder,
71    ///     Encoder,
72    ///     messages::{
73    ///         ConnectResult,
74    ///         amf::v0::Object,
75    ///     }
76    /// };
77    ///
78    /// let mut buffer = ByteBuffer::default();
79    /// buffer.encode(&Object::default());
80    /// buffer.encode(&Object::default());
81    /// assert!(Decoder::<ConnectResult>::decode(&mut buffer).is_ok());
82    ///
83    /// let mut buffer = ByteBuffer::default();
84    /// assert!(Decoder::<ConnectResult>::decode(&mut buffer).is_err())
85    /// ```
86    ///
87    /// [`InsufficientBufferLength`]: crate::byte_buffer::InsufficientBufferLength
88    /// [`InconsistentMarker`]: crate::messages::amf::InconsistentMarker
89    fn decode(&mut self) -> IOResult<ConnectResult> {
90        let properties: Object = self.decode()?;
91        let information: Object = self.decode()?;
92        Ok(ConnectResult { properties, information } )
93    }
94}
95
96impl Encoder<ConnectResult> for ByteBuffer {
97    /// Encodes a ConnectResult command into bytes.
98    fn encode(&mut self, connect_result: &ConnectResult) {
99        self.encode(connect_result.get_properties());
100        self.encode(connect_result.get_information());
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use crate::{
107        messages::amf::v0::{
108            Number,
109            AmfString
110        },
111        object
112    };
113    use super::*;
114
115    #[test]
116    fn decode_connect_result() {
117        let mut buffer = ByteBuffer::default();
118        buffer.encode(
119            &object!(
120                "fmsVer" => AmfString::from("FMS/5,0,17"),
121                "capabilities" => Number::new(31f64)
122            )
123        );
124        buffer.encode(
125            &object!(
126                "level" => AmfString::from("status"),
127                "code" => AmfString::from("NetConnection.Connect.Success"),
128                "description" => AmfString::from("Connection succeeded."),
129                "objectEncoding" => Number::new(0f64)
130            )
131        );
132        let result: IOResult<ConnectResult> = buffer.decode();
133        assert!(result.is_ok());
134        let actual = result.unwrap();
135        let expected = ConnectResult::new(
136            object!(
137                "fmsVer" => AmfString::from("FMS/5,0,17"),
138                "capabilities" => Number::new(31f64)
139            ),
140            object!(
141                "level" => AmfString::from("status"),
142                "code" => AmfString::from("NetConnection.Connect.Success"),
143                "description" => AmfString::from("Connection succeeded."),
144                "objectEncoding" => Number::new(0f64)
145            )
146        );
147        assert_eq!(expected, actual)
148    }
149
150    #[test]
151    fn encode_connect_result() {
152        let mut buffer = ByteBuffer::default();
153        let expected_properties = object!(
154            "fmsVer" => AmfString::from("FMS/5,0,17"),
155            "capabilities" => Number::new(31f64)
156        );
157        let expected_information = object!(
158            "level" => AmfString::from("status"),
159            "code" => AmfString::from("NetConnection.Connect.Success"),
160            "description" => AmfString::from("Connection succeeded."),
161            "objectEncoding" => Number::new(0f64)
162        );
163        buffer.encode(&ConnectResult::new(expected_properties.clone(), expected_information.clone()));
164        let actual_properties: Object = buffer.decode().unwrap();
165        assert_eq!(expected_properties, actual_properties);
166        let actual_information: Object = buffer.decode().unwrap();
167        assert_eq!(expected_information, actual_information)
168    }
169}