sheave_core/messages/
fc_unpublish.rs1use 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::{
13 AmfString,
14 Null
15 }
16};
17
18#[derive(Debug, Clone, PartialEq)]
22pub struct FcUnpublish(AmfString);
23
24impl FcUnpublish {
25 pub fn new(topic_path: AmfString) -> Self {
27 Self(topic_path)
28 }
29
30 pub fn get_topic_path(&self) -> &AmfString {
32 &self.0
33 }
34}
35
36impl From<FcUnpublish> for AmfString {
37 fn from(fc_unpublish: FcUnpublish) -> Self {
38 fc_unpublish.0
39 }
40}
41
42impl ChunkData for FcUnpublish {
43 const CHANNEL: Channel = Channel::System;
44 const MESSAGE_TYPE: MessageType = MessageType::Command;
45}
46
47impl Command for FcUnpublish {}
48
49impl Decoder<FcUnpublish> for ByteBuffer {
50 fn decode(&mut self) -> IOResult<FcUnpublish> {
95 Decoder::<Null>::decode(self)?;
96 let topic_path: AmfString = self.decode()?;
97
98 Ok(FcUnpublish(topic_path))
99 }
100}
101
102impl Encoder<FcUnpublish> for ByteBuffer {
103 fn encode(&mut self, fc_unpublish: &FcUnpublish) {
105 self.encode(&Null);
106 self.encode(fc_unpublish.get_topic_path());
107 }
108}
109
110#[cfg(test)]
111mod tests {
112 use super::*;
113
114 #[test]
115 fn decode_fc_unpublish() {
116 let mut buffer = ByteBuffer::default();
117 buffer.encode(&Null);
118 buffer.encode(&AmfString::default());
119
120 let result: IOResult<FcUnpublish> = buffer.decode();
121 assert!(result.is_ok());
122 let actual = result.unwrap();
123 let expected = FcUnpublish::new(AmfString::default());
124 assert_eq!(expected, actual)
125 }
126
127 #[test]
128 fn encode_fc_unpublish() {
129 let mut buffer = ByteBuffer::default();
130 let expected_topic_path = "";
131 let expected = FcUnpublish::new(AmfString::from(expected_topic_path));
132 buffer.encode(&expected);
133 Decoder::<Null>::decode(&mut buffer).unwrap();
134 let actual_topic_path: AmfString = buffer.decode().unwrap();
135 assert_eq!(expected_topic_path, actual_topic_path)
136 }
137}