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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (c) 2020 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
// Use of this source is governed by Apache-2.0 License that can be found
// in the LICENSE file.
use std::io;
use super::byte_array::ByteArrayError;
use super::topic::TopicError;
use super::utils::StringError;
use super::var_int::VarIntError;
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
pub enum DecodeError {
/// ClientId is empty or its length exceeds 23.
/// Or contains invalid characters.
InvalidClientId,
InvalidPacketType,
/// Protocol level is not in `3.1`, `3.1.1` or `5.0`.
InvalidProtocolLevel,
/// Protocol name must be "MQTT".
InvalidProtocolName,
/// Resrved bit in connect packet is not zero.
InvalidConnectFlags,
/// QoS not 0, 1, 2
InvalidQoS,
/// Invalid flag value in fixed header.
///
/// Does not contain InvalidQoS.
InvalidPacketFlags,
/// PacketId should be present but not set.
/// Or PacketId is none where it is required.
InvalidPacketId,
/// Failed to parse variable byte integer.
InvalidVarInt,
InvalidBoolData,
/// Length of buffer - offset < remaining length.
// TODO(Shaohua): Replace with InvalidVarInt
InvalidRemainingLength,
/// Invalid UTF-8 string.
InvalidString(StringError),
/// Violate topic filter rules.
/// Topic name might contain wildcard characters.
InvalidTopic(TopicError),
/// Unknown property type bit.
InvalidPropertyType,
/// Failed to parse property value.
InvalidPropertyValue,
/// Used in v5 protocol.
InvalidReasonCode,
/// Byte array index ouf of range.
OutOfRangeError,
/// Length of data exceeds its limitation
TooManyData,
/// No topic is speicified in Subscribe packet.
EmptyTopicFilter,
/// General errors
OtherErrors,
}
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
pub enum EncodeError {
InvalidData,
/// ClientId is empty or its length exceeds 23.
/// Or contains invalid characters.
InvalidClientId,
IoError(io::Error),
InvalidPacketType,
InvalidPacketLevel,
/// PacketId should be present but not set.
/// Or PacketId is none where it is required.
InvalidPacketId,
/// Length of data exceeds its limitation
TooManyData,
InvalidString(StringError),
/// Violate topic filter rules.
/// No topic is speicified in Subscribe packet.
/// Topic name might contain wildcard characters.
InvalidTopic(TopicError),
InvalidVarInt,
/// Used in v5 protocol.
InvalidReasonCode,
}
impl From<io::Error> for EncodeError {
fn from(err: io::Error) -> Self {
Self::IoError(err)
}
}
impl From<StringError> for EncodeError {
fn from(err: StringError) -> Self {
Self::InvalidString(err)
}
}
impl From<StringError> for DecodeError {
fn from(err: StringError) -> Self {
Self::InvalidString(err)
}
}
impl From<TopicError> for EncodeError {
fn from(err: TopicError) -> Self {
Self::InvalidTopic(err)
}
}
impl From<VarIntError> for EncodeError {
fn from(_err: VarIntError) -> Self {
Self::InvalidVarInt
}
}
impl From<TopicError> for DecodeError {
fn from(err: TopicError) -> Self {
Self::InvalidTopic(err)
}
}
impl From<ByteArrayError> for DecodeError {
fn from(err: ByteArrayError) -> Self {
match err {
ByteArrayError::OutOfRangeError => Self::OutOfRangeError,
ByteArrayError::InvalidString(err) => Self::InvalidString(err),
}
}
}
impl From<VarIntError> for DecodeError {
fn from(_err: VarIntError) -> Self {
// TODO(Shaohua): Add description
Self::InvalidVarInt
}
}