Skip to content

Commit 7290437

Browse files
committed
Optionally derive defmt::Format for some types
This allows them to be referenced in format strings when using the defmt logging framework. Currently this only covers the smaller enums for things like packet headers and error codes.
1 parent 609c3a3 commit 7290437

File tree

8 files changed

+21
-0
lines changed

8 files changed

+21
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ enum_dispatch = "0.3"
6565
foldhash = { version = "0.2.0", default-features = false }
6666

6767
# logging
68+
defmt = { version = "1.0.1", default-features = false, optional = true }
6869
tracing = { version = "0.1", default-features = false, optional = true }
6970

7071
[dev-dependencies]

src/mqtt/connection/event.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use crate::mqtt::result_code::MqttError;
3333
/// This enum defines the different kinds of timers used in MQTT protocol operations.
3434
/// Each timer serves a specific purpose in maintaining connection health and protocol compliance.
3535
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
36+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
3637
pub enum TimerKind {
3738
/// Timer for sending PINGREQ packets
3839
///

src/mqtt/connection/version.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
/// }
5353
/// ```
5454
#[derive(PartialEq, Clone, Copy, Debug)]
55+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
5556
pub enum Version {
5657
/// Version to be determined by incoming CONNECT packet
5758
///

src/mqtt/packet/packet_type.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use serde::{Deserialize, Serialize};
4242
/// assert_eq!(packet_type.as_str(), "connect");
4343
/// ```
4444
#[derive(Deserialize, PartialEq, Eq, Copy, Clone, TryFromPrimitive)]
45+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
4546
#[repr(u8)]
4647
pub enum PacketType {
4748
/// Client connection request packet
@@ -98,6 +99,7 @@ pub enum PacketType {
9899
/// assert_eq!(header.packet_type(), PacketType::Connect);
99100
/// ```
100101
#[derive(Deserialize, PartialEq, Eq, Copy, Clone, TryFromPrimitive)]
102+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
101103
#[repr(u8)]
102104
pub enum FixedHeader {
103105
/// CONNECT packet header (0x10)

src/mqtt/packet/property.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ use std::io::IoSlice;
6060
/// assert_eq!(property_id.as_str(), "message_expiry_interval");
6161
/// ```
6262
#[derive(Deserialize, PartialEq, Eq, Copy, Clone, TryFromPrimitive)]
63+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6364
#[repr(u8)]
6465
pub enum PropertyId {
6566
/// Indicates the format of the payload in PUBLISH packets (0=binary, 1=UTF-8)
@@ -224,6 +225,7 @@ impl fmt::Debug for PropertyId {
224225
/// assert_eq!(format as u8, 1);
225226
/// ```
226227
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, TryFromPrimitive)]
228+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
227229
#[repr(u8)]
228230
pub enum PayloadFormat {
229231
/// Payload is unspecified bytes (binary data)

src/mqtt/packet/qos.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ use serde::{Deserialize, Serialize};
7171
#[derive(
7272
Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, TryFromPrimitive,
7373
)]
74+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
7475
#[repr(u8)]
7576
pub enum Qos {
7677
/// QoS level 0: At most once delivery

src/mqtt/packet/retain_handling.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ use serde::{Deserialize, Serialize};
7070
#[derive(
7171
Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, TryFromPrimitive,
7272
)]
73+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
7374
#[repr(u8)]
7475
pub enum RetainHandling {
7576
/// Send retained messages at the time of the subscribe

src/mqtt/result_code.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use num_enum::TryFromPrimitive;
2626
use serde::{Serialize, Serializer};
2727

2828
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
29+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
2930
#[repr(u16)]
3031
pub enum MqttError {
3132
// MQTT protocol based error
@@ -210,6 +211,7 @@ impl core::convert::TryFrom<u8> for MqttError {
210211

211212
/// MQTT v3.1.1 Connect Return Code
212213
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive)]
214+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
213215
#[repr(u8)]
214216
pub enum ConnectReturnCode {
215217
Accepted = 0, // Connection accepted (not an error)
@@ -254,6 +256,7 @@ impl Serialize for ConnectReturnCode {
254256

255257
/// MQTT v3.1.1 SUBACK Return Code
256258
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive)]
259+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
257260
#[repr(u8)]
258261
pub enum SubackReturnCode {
259262
SuccessMaximumQos0 = 0x00, // Success with QoS0 (not an error)
@@ -297,6 +300,7 @@ impl Serialize for SubackReturnCode {
297300

298301
/// MQTT v5.0 Connect Reason Code (used in CONNACK)
299302
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive)]
303+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
300304
#[repr(u8)]
301305
pub enum ConnectReasonCode {
302306
Success = 0x00, // Success (not an error)
@@ -370,6 +374,7 @@ impl From<ConnectReasonCode> for MqttError {
370374
}
371375

372376
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive)]
377+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
373378
#[repr(u8)]
374379
pub enum DisconnectReasonCode {
375380
NormalDisconnection = 0x00,
@@ -501,6 +506,7 @@ impl From<MqttError> for DisconnectReasonCode {
501506
}
502507

503508
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive)]
509+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
504510
#[repr(u8)]
505511
pub enum SubackReasonCode {
506512
GrantedQos0 = 0x00,
@@ -566,6 +572,7 @@ impl From<SubackReasonCode> for MqttError {
566572
}
567573

568574
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive)]
575+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
569576
#[repr(u8)]
570577
pub enum UnsubackReasonCode {
571578
Success = 0x00,
@@ -618,6 +625,7 @@ impl From<UnsubackReasonCode> for MqttError {
618625
}
619626

620627
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive)]
628+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
621629
#[repr(u8)]
622630
pub enum PubackReasonCode {
623631
Success = 0x00,
@@ -674,6 +682,7 @@ impl From<PubackReasonCode> for MqttError {
674682
}
675683

676684
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive)]
685+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
677686
#[repr(u8)]
678687
pub enum PubrecReasonCode {
679688
Success = 0x00,
@@ -729,6 +738,7 @@ impl From<PubrecReasonCode> for MqttError {
729738
}
730739

731740
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive)]
741+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
732742
#[repr(u8)]
733743
pub enum PubrelReasonCode {
734744
Success = 0x00,
@@ -771,6 +781,7 @@ impl From<PubrelReasonCode> for MqttError {
771781
}
772782

773783
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive)]
784+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
774785
#[repr(u8)]
775786
pub enum PubcompReasonCode {
776787
Success = 0x00,
@@ -813,6 +824,7 @@ impl From<PubcompReasonCode> for MqttError {
813824
}
814825

815826
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive)]
827+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
816828
#[repr(u8)]
817829
pub enum AuthReasonCode {
818830
Success = 0x00,

0 commit comments

Comments
 (0)