Skip to content

Commit

Permalink
add serde feature and implement Serialize and Deserialize on mo…
Browse files Browse the repository at this point in the history
…st error types (#33)

* add serde for ParseError
* Add serde for DecodeErr
* add "serde" as feature
* Add documentation for "serde" feature
* impl Serialize & Deserialize for ReadDecodedError
* test serde feature in CI
* update changelog

---------

Co-authored-by: Felix Wirth <[email protected]>
  • Loading branch information
Arne91 and felixwrt authored Jun 21, 2024
1 parent caca0da commit 5f6aac5
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/quickstart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
4 changes: 4 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ use core::{
ops::Deref,
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use tlf::TypeLengthField;

pub mod common;
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/parser/tlf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/transport/decoder_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IoErr> {
/// Error while decoding the data (e.g. checksum mismatch)
Expand Down

0 comments on commit 5f6aac5

Please sign in to comment.