diff --git a/.github/workflows/quickstart.yml b/.github/workflows/quickstart.yml index 5dc0912..a72d651 100644 --- a/.github/workflows/quickstart.yml +++ b/.github/workflows/quickstart.yml @@ -28,6 +28,7 @@ jobs: - run: cargo test --no-default-features --features=embedded_hal - run: cargo test --no-default-features --features=embedded_hal,alloc - run: cargo test --no-default-features --features=embedded_hal,alloc,std + - run: cargo test --no-default-features --features=embedded_hal,alloc,std,serde build_examples: name: Build Examples diff --git a/CHANGELOG.md b/CHANGELOG.md index b3e3ac7..7e1836e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Embedded examples for the ESP32-C3 (#37) +- Added `serde` feature which implements `Serialize` and `Deserialize` on most error types (#33) ## [0.4.0] - 2024-06-04 diff --git a/Cargo.toml b/Cargo.toml index afe8adf..49d86ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,11 +15,13 @@ default = ["std"] std = ["alloc"] alloc = [] embedded_hal = ["nb", "embedded-hal"] +serde = ["dep:serde"] [dependencies] crc = "3.0.0" embedded-hal = { version = "0.2.7", optional = true } nb = { version = "1.1.0", optional = true } +serde = { version = "1.0", features = ["derive"], optional = true } [dev-dependencies] insta = { version = "1.21.0", features = ["yaml", "glob"] } diff --git a/src/lib.rs b/src/lib.rs index ba50137..5b84836 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ //! - **`alloc`** (default) — Implementations using allocations (`alloc::Vec` et al.). //! - **`embedded_hal`** — Allows using pins implementing `embedded_hal::serial::Read` in [`SmlReader`](SmlReader::from_eh_reader). //! - **`nb`** - Enables non-blocking APIs using the `nb` crate. +//! - **`serde`** - Implements `Serialize` and `Deserialize` on most error types. //! #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(docsrs, feature(doc_auto_cfg))] diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 8f716c4..f99e9c5 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -107,6 +107,9 @@ use core::{ ops::Deref, }; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + use tlf::TypeLengthField; pub mod common; @@ -122,6 +125,7 @@ pub use tlf::TlfParseError; pub use octet_string::OctetStr; /// Error type used by the parser +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone, Debug, PartialEq, Eq)] pub enum ParseError { /// There are additional bytes in the input while the parser expects EOF diff --git a/src/parser/tlf.rs b/src/parser/tlf.rs index 6784a50..0f95a5b 100644 --- a/src/parser/tlf.rs +++ b/src/parser/tlf.rs @@ -4,11 +4,15 @@ use core::fmt; use crate::parser::ParseError; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + use super::{take_byte, SmlParse}; use super::ResTy; /// Error type used when parsing a `TypeLengthField` +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Clone, Debug, PartialEq, Eq)] pub enum TlfParseError { /// The length field of a TLF overflowed diff --git a/src/transport.rs b/src/transport.rs index 1e7d7f5..9f9268f 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -32,6 +32,9 @@ mod decoder_reader; pub use decoder_reader::{DecoderReader, ReadDecodedError}; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + use core::{borrow::Borrow, fmt}; use crate::util::{Buffer, OutOfMemory, CRC_X25}; @@ -265,6 +268,7 @@ pub fn encode_streaming( Encoder::new(iter.into_iter().map(|x| *x.borrow())) } +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, PartialEq, Eq, Clone)] /// An error which can be returned when decoding an sml message. pub enum DecodeErr { diff --git a/src/transport/decoder_reader.rs b/src/transport/decoder_reader.rs index ab55894..ff5f564 100644 --- a/src/transport/decoder_reader.rs +++ b/src/transport/decoder_reader.rs @@ -2,10 +2,14 @@ use core::fmt; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + use super::{DecodeErr, Decoder}; use crate::util::{Buffer, ByteSource, ByteSourceErr}; /// Error type used by the `DecoderReader` +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[derive(Debug, PartialEq)] pub enum ReadDecodedError { /// Error while decoding the data (e.g. checksum mismatch)