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
use futures::{AsyncWrite, AsyncWriteExt};
use super::errors::{MPacketHeaderError, MPacketWriteError};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum MQualityOfService {
AtMostOnce,
AtLeastOnce,
ExactlyOnce,
}
impl MQualityOfService {
pub fn to_byte(self) -> u8 {
match self {
MQualityOfService::AtMostOnce => 0x0,
MQualityOfService::AtLeastOnce => 0x1,
MQualityOfService::ExactlyOnce => 0x2,
}
}
}
pub fn mquality_of_service(lower: u8) -> Result<MQualityOfService, MPacketHeaderError> {
match lower {
0b00 => Ok(MQualityOfService::AtMostOnce),
0b01 => Ok(MQualityOfService::AtLeastOnce),
0b10 => Ok(MQualityOfService::ExactlyOnce),
inv_qos => Err(MPacketHeaderError::InvalidQualityOfService(inv_qos)),
}
}
impl MQualityOfService {
pub async fn write_to<W: AsyncWrite>(
&self,
writer: &mut std::pin::Pin<&mut W>,
) -> Result<(), MPacketWriteError> {
writer
.write_all(match self {
MQualityOfService::AtMostOnce => &[0x0],
MQualityOfService::AtLeastOnce => &[0x1],
MQualityOfService::ExactlyOnce => &[0x2],
})
.await?;
Ok(())
}
}