sheave_core/messages/
set_playlist.rs1use std::io::Result as IOResult;
2use crate::{
3 ByteBuffer,
4 Decoder,
5 Encoder,
6 messages::{
7 Channel,
8 ChunkData,
9 Command,
10 amf::v0::{
11 EcmaArray,
12 Null
13 },
14 headers::MessageType
15 },
16};
17
18#[derive(Debug, Clone, Default, PartialEq, Eq)]
20pub struct SetPlaylist(EcmaArray);
21
22impl SetPlaylist {
23 pub fn new(playpaths: EcmaArray) -> Self {
25 Self(playpaths)
26 }
27
28 pub fn get_playpaths(&self) -> &EcmaArray {
30 &self.0
31 }
32}
33
34impl From<SetPlaylist> for EcmaArray {
35 fn from(set_playlist: SetPlaylist) -> Self {
36 set_playlist.0
37 }
38}
39
40impl ChunkData for SetPlaylist {
41 const CHANNEL: Channel = Channel::Video;
42 const MESSAGE_TYPE: MessageType = MessageType::Command;
43}
44
45impl Command for SetPlaylist {}
46
47impl Decoder<SetPlaylist> for ByteBuffer {
48 fn decode(&mut self) -> IOResult<SetPlaylist> {
88 Decoder::<Null>::decode(self)?;
89 let playpaths: EcmaArray = self.decode()?;
90 Ok(SetPlaylist(playpaths))
91 }
92}
93
94impl Encoder<SetPlaylist> for ByteBuffer {
95 fn encode(&mut self, set_playlist: &SetPlaylist) {
97 self.encode(&Null);
98 self.encode(set_playlist.get_playpaths());
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use crate::{
105 ecma_array,
106 messages::amf::v0::AmfString,
107 };
108 use super::*;
109
110 #[test]
111 fn decode_set_playlist() {
112 let mut buffer = ByteBuffer::default();
113 buffer.encode(&Null);
114 buffer.encode(
115 &ecma_array!(
116 "0" => AmfString::default()
117 )
118 );
119 let result: IOResult<SetPlaylist> = buffer.decode();
120 assert!(result.is_ok());
121 let actual = result.unwrap();
122 let expected = SetPlaylist::new(
123 ecma_array!(
124 "0" => AmfString::default()
125 )
126 );
127 assert_eq!(expected, actual)
128 }
129
130 #[test]
131 fn encode_set_playlist() {
132 let mut buffer = ByteBuffer::default();
133 let expected_playpaths = ecma_array!(
134 "0" => AmfString::default()
135 );
136 let expected = SetPlaylist::new(expected_playpaths.clone());
137 buffer.encode(&expected);
138 Decoder::<Null>::decode(&mut buffer).unwrap();
139 let actual_playpaths: EcmaArray = buffer.decode().unwrap();
140 assert_eq!(expected_playpaths, actual_playpaths)
141 }
142}