sheave_core/messages/
command_error.rs1use 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#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct CommandError(Object);
18
19impl CommandError {
20 pub fn new(information: Object) -> Self {
22 Self(information)
23 }
24
25 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 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 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}