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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use crate::{
codec, ControlPacketType, Error, PropertiesDecoder, Property, ReasonCode, Result as SageResult,
};
use futures::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use std::marker::Unpin;
#[derive(Debug, PartialEq, Clone)]
pub struct UnSubAck {
pub packet_identifier: u16,
pub reason_string: Option<String>,
pub user_properties: Vec<(String, String)>,
pub reason_codes: Vec<ReasonCode>,
}
impl Default for UnSubAck {
fn default() -> Self {
UnSubAck {
packet_identifier: 0,
reason_string: None,
user_properties: Default::default(),
reason_codes: Default::default(),
}
}
}
impl UnSubAck {
pub async fn write<W: AsyncWrite + Unpin>(self, writer: &mut W) -> SageResult<usize> {
let mut n_bytes = codec::write_two_byte_integer(self.packet_identifier, writer).await?;
let mut properties = Vec::new();
if let Some(reason_string) = self.reason_string {
n_bytes += Property::ReasonString(reason_string)
.encode(&mut properties)
.await?;
}
for (k, v) in self.user_properties {
n_bytes += Property::UserProperty(k, v).encode(&mut properties).await?;
}
n_bytes += codec::write_variable_byte_integer(properties.len() as u32, writer).await?;
writer.write_all(&properties).await?;
for reason_code in self.reason_codes {
n_bytes += codec::write_reason_code(reason_code, writer).await?;
}
Ok(n_bytes)
}
pub async fn read<R: AsyncRead + Unpin>(
reader: &mut R,
remaining_size: usize,
) -> SageResult<Self> {
let mut reader = reader.take(remaining_size as u64);
let packet_identifier = codec::read_two_byte_integer(&mut reader).await?;
let mut user_properties = Vec::new();
let mut properties = PropertiesDecoder::take(&mut reader).await?;
let mut reason_string = None;
while properties.has_properties() {
match properties.read().await? {
Property::ReasonString(v) => reason_string = Some(v),
Property::UserProperty(k, v) => user_properties.push((k, v)),
_ => return Err(Error::ProtocolError),
}
}
let mut reason_codes = Vec::new();
while reader.limit() > 0 {
reason_codes.push(ReasonCode::try_parse(
codec::read_byte(&mut reader).await?,
ControlPacketType::SUBACK,
)?);
}
Ok(UnSubAck {
packet_identifier,
user_properties,
reason_string,
reason_codes,
})
}
}
#[cfg(test)]
mod unit {
use super::*;
use async_std::io::Cursor;
fn encoded() -> Vec<u8> {
vec![
5, 57, 36, 31, 0, 18, 71, 105, 111, 114, 103, 105, 111, 32, 98, 121, 32, 77, 111, 114,
111, 100, 101, 114, 38, 0, 7, 77, 111, 103, 119, 97, 195, 175, 0, 3, 67, 97, 116, 145,
143,
]
}
fn decoded() -> UnSubAck {
UnSubAck {
packet_identifier: 1337,
reason_string: Some("Giorgio by Moroder".into()),
user_properties: vec![("Mogwaï".into(), "Cat".into())],
reason_codes: vec![
ReasonCode::PacketIdentifierInUse,
ReasonCode::TopicFilterInvalid,
],
}
}
#[async_std::test]
async fn encode() {
let test_data = decoded();
let mut tested_result = Vec::new();
let n_bytes = test_data.write(&mut tested_result).await.unwrap();
assert_eq!(tested_result, encoded());
assert_eq!(n_bytes, 41);
}
#[async_std::test]
async fn decode() {
let mut test_data = Cursor::new(encoded());
let tested_result = UnSubAck::read(&mut test_data, 41).await.unwrap();
assert_eq!(tested_result, decoded());
}
}