From da75d9dea2c13b0781dec043d53cf680b07e4fdb Mon Sep 17 00:00:00 2001 From: Lucas Thelen <89479728+thelenlucas@users.noreply.github.com> Date: Tue, 1 Apr 2025 17:29:05 +0000 Subject: [PATCH 1/2] Derive formats --- Cargo.toml | 4 +++- src/error.rs | 11 +++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 528b9042..7cbd0d43 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,11 +28,13 @@ edition = "2021" [features] default = ["std", "derive"] std = ["alloc", "serde?/std"] -alloc = ["serde?/alloc"] +alloc = ["serde?/alloc", "defmt?/alloc"] derive = ["bincode_derive"] +defmt = ["dep:defmt"] [dependencies] bincode_derive = { path = "derive", version = "2.0.1", optional = true } +defmt = {version = "1.0.1", optional = true} serde = { version = "1.0", default-features = false, optional = true } unty = "0.0.4" diff --git a/src/error.rs b/src/error.rs index 7fb9c5c0..364730f8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,6 +3,7 @@ /// Errors that can be encountered by encoding a type #[non_exhaustive] #[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum EncodeError { /// The writer ran out of storage. UnexpectedEnd, @@ -30,6 +31,7 @@ pub enum EncodeError { #[cfg(feature = "std")] Io { /// The encountered error + #[cfg_attr(feature="defmt", defmt(Debug2Format))] inner: std::io::Error, /// The amount of bytes that were written before the error occurred index: usize, @@ -46,8 +48,10 @@ pub enum EncodeError { #[cfg(feature = "std")] InvalidSystemTime { /// The error that was thrown by the SystemTime + #[cfg_attr(feature="defmt", defmt(Debug2Format))] inner: std::time::SystemTimeError, /// The SystemTime that caused the error + #[cfg_attr(feature="defmt", defmt(Debug2Format))] time: std::boxed::Box, }, @@ -65,6 +69,7 @@ impl core::fmt::Display for EncodeError { /// Errors that can be encountered by decoding a type #[non_exhaustive] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] #[derive(Debug)] pub enum DecodeError { /// The reader reached its end but more bytes were expected. @@ -109,6 +114,7 @@ pub enum DecodeError { /// The decoder tried to decode a `str`, but an utf8 error was encountered. Utf8 { /// The inner error + #[cfg_attr(feature="defmt", defmt(Debug2Format))] inner: core::str::Utf8Error, }, @@ -168,6 +174,7 @@ pub enum DecodeError { #[cfg(feature = "std")] Io { /// The IO error expected + #[cfg_attr(feature="defmt", defmt(Debug2Format))] inner: std::io::Error, /// Gives an estimate of how many extra bytes are needed. @@ -217,7 +224,7 @@ impl DecodeError { /// Indicates which enum variants are allowed #[non_exhaustive] #[derive(Debug, PartialEq, Eq)] -pub enum AllowedEnumVariants { +#[cfg_attr(feature = "defmt", derive(defmt::Format))]pub enum AllowedEnumVariants { /// All values between `min` and `max` (inclusive) are allowed #[allow(missing_docs)] Range { min: u32, max: u32 }, @@ -229,7 +236,7 @@ pub enum AllowedEnumVariants { #[non_exhaustive] #[derive(Debug, PartialEq, Eq)] #[allow(missing_docs)] -pub enum IntegerType { +#[cfg_attr(feature = "defmt", derive(defmt::Format))]pub enum IntegerType { U8, U16, U32, From 927f2b69edabd2a45b348a278c5304dc1f4db8da Mon Sep 17 00:00:00 2001 From: Lucas Thelen <89479728+thelenlucas@users.noreply.github.com> Date: Tue, 1 Apr 2025 17:29:05 +0000 Subject: [PATCH 2/2] Formatting fix --- src/error.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/error.rs b/src/error.rs index 364730f8..bcb43d19 100644 --- a/src/error.rs +++ b/src/error.rs @@ -31,7 +31,7 @@ pub enum EncodeError { #[cfg(feature = "std")] Io { /// The encountered error - #[cfg_attr(feature="defmt", defmt(Debug2Format))] + #[cfg_attr(feature = "defmt", defmt(Debug2Format))] inner: std::io::Error, /// The amount of bytes that were written before the error occurred index: usize, @@ -48,10 +48,10 @@ pub enum EncodeError { #[cfg(feature = "std")] InvalidSystemTime { /// The error that was thrown by the SystemTime - #[cfg_attr(feature="defmt", defmt(Debug2Format))] + #[cfg_attr(feature = "defmt", defmt(Debug2Format))] inner: std::time::SystemTimeError, /// The SystemTime that caused the error - #[cfg_attr(feature="defmt", defmt(Debug2Format))] + #[cfg_attr(feature = "defmt", defmt(Debug2Format))] time: std::boxed::Box, }, @@ -114,7 +114,7 @@ pub enum DecodeError { /// The decoder tried to decode a `str`, but an utf8 error was encountered. Utf8 { /// The inner error - #[cfg_attr(feature="defmt", defmt(Debug2Format))] + #[cfg_attr(feature = "defmt", defmt(Debug2Format))] inner: core::str::Utf8Error, }, @@ -174,7 +174,7 @@ pub enum DecodeError { #[cfg(feature = "std")] Io { /// The IO error expected - #[cfg_attr(feature="defmt", defmt(Debug2Format))] + #[cfg_attr(feature = "defmt", defmt(Debug2Format))] inner: std::io::Error, /// Gives an estimate of how many extra bytes are needed. @@ -224,7 +224,8 @@ impl DecodeError { /// Indicates which enum variants are allowed #[non_exhaustive] #[derive(Debug, PartialEq, Eq)] -#[cfg_attr(feature = "defmt", derive(defmt::Format))]pub enum AllowedEnumVariants { +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +pub enum AllowedEnumVariants { /// All values between `min` and `max` (inclusive) are allowed #[allow(missing_docs)] Range { min: u32, max: u32 }, @@ -236,7 +237,8 @@ impl DecodeError { #[non_exhaustive] #[derive(Debug, PartialEq, Eq)] #[allow(missing_docs)] -#[cfg_attr(feature = "defmt", derive(defmt::Format))]pub enum IntegerType { +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +pub enum IntegerType { U8, U16, U32,