mqttrust/
lib.rs

1#![cfg_attr(not(test), no_std)]
2
3// This mod MUST go first, so that the others see its macros.
4pub(crate) mod fmt;
5
6pub mod encoding;
7
8pub use encoding::v4::{
9    subscribe::SubscribeTopic, utils::QoS, Packet, Publish, Subscribe, Unsubscribe,
10};
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13#[cfg_attr(feature = "defmt-impl", derive(defmt::Format))]
14pub enum MqttError {
15    Full,
16    Borrow,
17    Overflow,
18}
19
20pub trait Mqtt {
21    fn send(&self, packet: Packet<'_>) -> Result<(), MqttError>;
22
23    fn client_id(&self) -> &str;
24
25    fn publish(&self, topic_name: &str, payload: &[u8], qos: QoS) -> Result<(), MqttError> {
26        let packet = Packet::Publish(Publish {
27            dup: false,
28            qos,
29            pid: None,
30            retain: false,
31            topic_name,
32            payload,
33        });
34
35        self.send(packet)
36    }
37
38    fn subscribe(&self, topics: &[SubscribeTopic<'_>]) -> Result<(), MqttError> {
39        let packet = Packet::Subscribe(Subscribe::new(topics));
40        self.send(packet)
41    }
42
43    fn unsubscribe(&self, topics: &[&str]) -> Result<(), MqttError> {
44        let packet = Packet::Unsubscribe(Unsubscribe::new(topics));
45        self.send(packet)
46    }
47}