mqtt_codec_kit/common/variable_header/protocol_name.rs
1use std::io::{self, Read, Write};
2
3use crate::common::{Decodable, Encodable};
4
5/// Protocol name in variable header
6///
7/// # Example
8///
9/// ```plain
10/// 7 3 0
11/// +--------------------------+--------------------------+
12/// | Length MSB (0) |
13/// | Length LSB (4) |
14/// | 0100 | 1101 | 'M'
15/// | 0101 | 0001 | 'Q'
16/// | 0101 | 0100 | 'T'
17/// | 0101 | 0100 | 'T'
18/// +--------------------------+--------------------------+
19/// ```
20#[derive(Debug, Eq, PartialEq, Clone)]
21pub struct ProtocolName(pub String);
22
23impl Encodable for ProtocolName {
24 fn encode<W: Write>(&self, writer: &mut W) -> Result<(), io::Error> {
25 (&self.0[..]).encode(writer)
26 }
27
28 fn encoded_length(&self) -> u32 {
29 (&self.0[..]).encoded_length()
30 }
31}
32
33impl Decodable for ProtocolName {
34 type Error = io::Error;
35 type Cond = ();
36
37 fn decode_with<R: Read>(reader: &mut R, _rest: ()) -> Result<ProtocolName, io::Error> {
38 Ok(ProtocolName(Decodable::decode(reader)?))
39 }
40}