scuffle_rtmp/command_messages/netconnection/
reader.rs

1//! Reading [`NetConnectionCommand`].
2
3use bytes::Bytes;
4use scuffle_amf0::decoder::Amf0Decoder;
5use scuffle_bytes_util::zero_copy::BytesBuf;
6
7use super::NetConnectionCommand;
8use crate::command_messages::error::CommandError;
9
10impl NetConnectionCommand<'_> {
11    /// Reads a [`NetConnectionCommand`] from the given decoder.
12    ///
13    /// Returns `Ok(None)` if the `command_name` is not recognized.
14    pub fn read(command_name: &str, decoder: &mut Amf0Decoder<BytesBuf<Bytes>>) -> Result<Option<Self>, CommandError> {
15        match command_name {
16            "connect" => {
17                let command_object = decoder.deserialize()?;
18                Ok(Some(Self::Connect(command_object)))
19            }
20            "call" => Ok(Some(Self::Call {
21                command_object: decoder.deserialize()?,
22                optional_arguments: decoder.deserialize()?,
23            })),
24            "close" => Ok(Some(Self::Close)),
25            "createStream" => Ok(Some(Self::CreateStream)),
26            _ => Ok(None),
27        }
28    }
29}
30
31#[cfg(test)]
32#[cfg_attr(all(test, coverage_nightly), coverage(off))]
33mod tests {
34    use bytes::Bytes;
35    use scuffle_amf0::Amf0Object;
36    use scuffle_amf0::decoder::Amf0Decoder;
37    use scuffle_amf0::encoder::Amf0Encoder;
38
39    use super::NetConnectionCommand;
40    use crate::command_messages::error::CommandError;
41
42    #[test]
43    fn test_read_no_app() {
44        let mut command_object = Vec::new();
45        let mut encoder = Amf0Encoder::new(&mut command_object);
46        encoder.encode_object(&Amf0Object::new()).unwrap();
47
48        let mut decoder = Amf0Decoder::from_buf(Bytes::from_owner(command_object));
49        let result = NetConnectionCommand::read("connect", &mut decoder).unwrap_err();
50
51        assert!(matches!(result, CommandError::Amf0(scuffle_amf0::Amf0Error::Custom(_))));
52    }
53}