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> {
84 Decoder::<Null>::decode(self)?;
85 let playpaths: EcmaArray = self.decode()?;
86 Ok(SetPlaylist(playpaths))
87 }
88}
89
90impl Encoder<SetPlaylist> for ByteBuffer {
91 fn encode(&mut self, set_playlist: &SetPlaylist) {
93 self.encode(&Null);
94 self.encode(set_playlist.get_playpaths());
95 }
96}
97
98#[cfg(test)]
99mod tests {
100 use crate::{
101 ecma_array,
102 messages::amf::v0::AmfString,
103 };
104 use super::*;
105
106 #[test]
107 fn decode_set_playlist() {
108 let mut buffer = ByteBuffer::default();
109 buffer.encode(
110 &ecma_array!(
111 "0" => AmfString::default()
112 )
113 );
114 let result: IOResult<SetPlaylist> = buffer.decode();
115 assert!(result.is_ok());
116 let actual = result.unwrap();
117 let expected = SetPlaylist::new(
118 ecma_array!(
119 "0" => AmfString::default()
120 )
121 );
122 assert_eq!(expected, actual)
123 }
124
125 #[test]
126 fn encode_set_playlist() {
127 let mut buffer = ByteBuffer::default();
128 let expected_playpaths = ecma_array!(
129 "0" => AmfString::default()
130 );
131 let expected = SetPlaylist::new(expected_playpaths.clone());
132 buffer.encode(&expected);
133 Decoder::<Null>::decode(&mut buffer).unwrap();
134 let actual_playpaths: EcmaArray = buffer.decode().unwrap();
135 assert_eq!(expected_playpaths, actual_playpaths)
136 }
137}