1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use crate::{
    codec::Decoder, error::DecodeError, protocol::commands::COMMAND_METADATA_UPDATE, ResponseCode,
};

use super::Command;

use crate::codec::Encoder;
#[cfg(test)]
use fake::Fake;

#[cfg_attr(test, derive(fake::Dummy))]
#[derive(PartialEq, Eq, Debug)]
pub struct MetadataUpdateCommand {
    code: ResponseCode,
    stream: String,
}

impl Decoder for MetadataUpdateCommand {
    fn decode(input: &[u8]) -> Result<(&[u8], Self), DecodeError> {
        let (input, code) = ResponseCode::decode(input)?;
        let (input, stream) = Option::decode(input)?;

        Ok((
            input,
            MetadataUpdateCommand {
                code,
                stream: stream.unwrap(),
            },
        ))
    }
}

impl Encoder for MetadataUpdateCommand {
    fn encoded_size(&self) -> u32 {
        0
    }

    fn encode(&self, writer: &mut impl std::io::Write) -> Result<(), crate::error::EncodeError> {
        self.code.encode(writer)?;
        self.stream.as_str().encode(writer)?;
        Ok(())
    }
}

impl Command for MetadataUpdateCommand {
    fn key(&self) -> u16 {
        COMMAND_METADATA_UPDATE
    }
}

#[cfg(test)]
mod tests {

    use crate::commands::tests::command_encode_decode_test;

    use super::MetadataUpdateCommand;

    #[test]
    fn metadata_update_test() {
        command_encode_decode_test::<MetadataUpdateCommand>()
    }
}