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
|
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
use std::ops::Deref;
use mqtt_format::v5::packets::MqttPacket as FormatMqttPacket;
use mqtt_format::v5::write::MqttWriteError;
use mqtt_format::v5::write::WriteMqttPacket;
use stable_deref_trait::StableDeref;
use tokio_util::bytes::BufMut;
use tokio_util::bytes::Bytes;
use tokio_util::bytes::BytesMut;
use yoke::CloneableCart;
use yoke::Yoke;
pub mod auth;
pub mod connack;
pub mod connect;
pub mod disconnect;
pub mod pingreq;
pub mod pingresp;
pub mod puback;
pub mod pubcomp;
pub mod publish;
pub mod pubrec;
pub mod pubrel;
pub mod suback;
pub mod subscribe;
pub mod unsuback;
pub mod unsubscribe;
#[derive(Debug, thiserror::Error)]
#[error("Could not convert into the required packet type")]
pub struct InvalidPacketType;
#[derive(Debug, Clone)]
pub(crate) struct StableBytes(pub(crate) Bytes);
impl Deref for StableBytes {
type Target = [u8];
fn deref(&self) -> &Self::Target {
self.0.deref()
}
}
// SAFETY: StableBytes derefs to &[u8] through bytes, which is stable
unsafe impl StableDeref for StableBytes {}
// SAFETY: StableBytes clones the pointer to the inner slice, and as such is stable as well
unsafe impl CloneableCart for StableBytes {}
#[derive(Debug, Clone)]
pub struct MqttPacket {
pub(crate) packet: Yoke<FormatMqttPacket<'static>, StableBytes>,
}
impl PartialEq for MqttPacket {
fn eq(&self, other: &Self) -> bool {
self.packet.get() == other.packet.get()
}
}
impl MqttPacket {
pub fn get(&self) -> &FormatMqttPacket<'_> {
self.packet.get()
}
}
#[derive(Debug, thiserror::Error)]
pub enum MqttWriterError {
#[error("An error occured while writing an MqttPacket: {:?}", .0)]
MqttWrite(mqtt_format::v5::write::MqttWriteError),
}
impl From<mqtt_format::v5::write::MqttWriteError> for MqttWriterError {
fn from(value: mqtt_format::v5::write::MqttWriteError) -> Self {
MqttWriterError::MqttWrite(value)
}
}
pub struct VecWriter<'a>(pub &'a mut Vec<u8>);
impl<'a> WriteMqttPacket for VecWriter<'a> {
type Error = MqttWriteError;
fn write_byte(&mut self, u: u8) -> mqtt_format::v5::write::WResult<Self> {
self.0.push(u);
Ok(())
}
fn write_slice(&mut self, u: &[u8]) -> mqtt_format::v5::write::WResult<Self> {
self.0.extend_from_slice(u);
Ok(())
}
}
pub struct MqttWriter<'a>(pub &'a mut BytesMut);
impl<'a> WriteMqttPacket for MqttWriter<'a> {
type Error = MqttWriterError;
fn write_byte(&mut self, u: u8) -> mqtt_format::v5::write::WResult<Self> {
self.0.put_u8(u);
Ok(())
}
fn write_slice(&mut self, u: &[u8]) -> mqtt_format::v5::write::WResult<Self> {
self.0.put_slice(u);
Ok(())
}
}
|