sheave_core/messages/
command_error.rs

1use std::io::Result as IOResult;
2use crate::{
3    ByteBuffer,
4    Decoder,
5    Encoder,
6    messages::amf::v0::Object
7};
8use super::{
9    Channel,
10    ChunkData,
11    Command,
12    headers::MessageType
13};
14
15/// The response message that some command failed.
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct CommandError(Object);
18
19impl CommandError {
20    /// Constructs a CommandError.
21    pub fn new(information: Object) -> Self {
22        Self(information)
23    }
24
25    /// Gets the information object.
26    pub fn get_information(&self) -> &Object {
27        &self.0
28    }
29}
30
31impl From<CommandError> for Object {
32    fn from(command_error: CommandError) -> Self {
33        command_error.0
34    }
35}
36
37impl ChunkData for CommandError {
38    const CHANNEL: Channel = Channel::Source;
39    const MESSAGE_TYPE: MessageType = MessageType::Command;
40}
41
42impl Command for CommandError {}
43
44impl Decoder<CommandError> for ByteBuffer {
45    /// Decodes bytes into a CommandError command.
46    ///
47    /// # Errors
48    ///
49    /// * [`InsufficientBufferLength`]
50    ///
51    /// When some field misses.
52    ///
53    /// * [`InconsistentMarker`]
54    ///
55    /// When some value is inconsistent with its marker.
56    ///
57    /// # Examples
58    ///
59    /// ```rust
60    /// use sheave_core::{
61    ///     ByteBuffer,
62    ///     Decoder,
63    ///     Encoder,
64    ///     messages::{
65    ///         CommandError,
66    ///         amf::v0::Object,
67    ///     }
68    /// };
69    ///
70    /// let mut buffer = ByteBuffer::default();
71    /// buffer.encode(&Object::default());
72    /// assert!(Decoder::<CommandError>::decode(&mut buffer).is_ok());
73    ///
74    /// let mut buffer = ByteBuffer::default();
75    /// assert!(Decoder::<CommandError>::decode(&mut buffer).is_err())
76    /// ```
77    ///
78    /// [`InsufficientBufferLength`]: crate::byte_buffer::InsufficientBufferLength
79    /// [`InconsistentMarker`]: crate::messages::amf::InconsistentMarker
80    fn decode(&mut self) -> IOResult<CommandError> {
81        let information: Object = self.decode()?;
82        Ok(CommandError(information))
83    }
84}
85
86impl Encoder<CommandError> for ByteBuffer {
87    /// Encodes a CommandError command into bytes.
88    fn encode(&mut self, command_error: &CommandError) {
89        self.encode(command_error.get_information());
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use crate::{
96        messages::amf::v0::AmfString,
97        object
98    };
99    use super::*;
100
101    #[test]
102    fn decode_error_input() {
103        let mut buffer = ByteBuffer::default();
104        buffer.encode(
105            &object!(
106                "level" => AmfString::from("error"),
107                "code" => AmfString::from("NetStream.GetStreamLength.MetadataNotFound"),
108                "description" => AmfString::from("Metadata field didn't find in specified file.")
109            )
110        );
111        let result: IOResult<CommandError> = buffer.decode();
112        assert!(result.is_ok());
113        let actual = result.unwrap();
114        let expected = CommandError::new(
115            object!(
116                "level" => AmfString::from("error"),
117                "code" => AmfString::from("NetStream.GetStreamLength.MetadataNotFound"),
118                "description" => AmfString::from("Metadata field didn't find in specified file.")
119            )
120        );
121        assert_eq!(expected, actual)
122    }
123
124    #[test]
125    fn encode_error_input() {
126        let mut buffer = ByteBuffer::default();
127        let expected_information = object!(
128            "level" => AmfString::from("error"),
129            "code" => AmfString::from("NetStream.GetStreamLength.MetadataNotFound"),
130            "description" => AmfString::from("Metadata field didn't find in specified file.")
131        );
132        let expected = CommandError::new(expected_information.clone());
133        buffer.encode(&expected);
134        let actual_information: Object = buffer.decode();
135        assert_eq!(expected_information, actual_information)
136    }
137}