mqtt_codec_kit/common/variable_header/
protocol_level.rs

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
//! Protocol level header

use std::io::{self, Read, Write};

use byteorder::{ReadBytesExt, WriteBytesExt};

use crate::common::{Decodable, Encodable};

pub const SPEC_3_1_0: u8 = 0x03;
pub const SPEC_3_1_1: u8 = 0x04;
pub const SPEC_5_0: u8 = 0x05;

/// Protocol level in MQTT (`0x04` in v3.1.1)
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
#[repr(u8)]
pub enum ProtocolLevel {
    Version310 = SPEC_3_1_0,
    Version311 = SPEC_3_1_1,
    Version50 = SPEC_5_0,
}

impl ProtocolLevel {
    pub fn from_u8(n: u8) -> Result<ProtocolLevel, ProtocolLevelError> {
        match n {
            SPEC_3_1_0 => Ok(ProtocolLevel::Version310),
            SPEC_3_1_1 => Ok(ProtocolLevel::Version311),
            SPEC_5_0 => Ok(ProtocolLevel::Version50),
            lvl => Err(ProtocolLevelError::InvalidProtocolLevel(lvl)),
        }
    }
}

impl Encodable for ProtocolLevel {
    fn encode<W: Write>(&self, writer: &mut W) -> Result<(), io::Error> {
        writer.write_u8(*self as u8)
    }

    fn encoded_length(&self) -> u32 {
        1
    }
}

impl Decodable for ProtocolLevel {
    type Error = ProtocolLevelError;
    type Cond = ();

    fn decode_with<R: Read>(
        reader: &mut R,
        _rest: (),
    ) -> Result<ProtocolLevel, ProtocolLevelError> {
        reader.read_u8().map(ProtocolLevel::from_u8)?
    }
}

#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub enum ProtocolLevelError {
    IoError(#[from] io::Error),
    #[error("invalid protocol level ({0})")]
    InvalidProtocolLevel(u8),
}