mqtt/control/variable_header/
protocol_name.rs

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