From 87e19a0fa90b5c18fc55171360ab9391cc5605aa Mon Sep 17 00:00:00 2001 From: Christian Poveda <31802960+pvdrz@users.noreply.github.com> Date: Fri, 11 Feb 2022 11:44:22 -0500 Subject: [PATCH] Minor fixes for the packable crates (#25) * implement `Error` for `UnknownTagError` even if `T` does not * implement `Error` for `UnpackError` * update keywords of `packable-derive` and `packable-derive-test` * fix docs * update `packable` crates version to `0.2.1` * document `serde` and `primitive-types` features --- packable/packable-derive-test/Cargo.toml | 6 +++--- packable/packable-derive/Cargo.toml | 4 ++-- packable/packable/CHANGELOG.md | 9 ++++++++- packable/packable/Cargo.toml | 4 ++-- packable/packable/README.md | 18 +++++++++++++---- packable/packable/src/error.rs | 25 ++++++++++++++++++++++-- packable/packable/src/lib.rs | 20 ++++++++++++++----- packable/packable/src/packable/prefix.rs | 3 +-- 8 files changed, 68 insertions(+), 21 deletions(-) diff --git a/packable/packable-derive-test/Cargo.toml b/packable/packable-derive-test/Cargo.toml index 54a8350..50bf3b5 100644 --- a/packable/packable-derive-test/Cargo.toml +++ b/packable/packable-derive-test/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "packable-derive-test" -version = "0.2.0" +version = "0.2.1" authors = [ "IOTA Stiftung" ] edition = "2021" description = "Test suite for the `packable-derive` crate." @@ -8,7 +8,7 @@ readme = "README.md" repository = "https://github.com/iotaledger/common-rs" license = "Apache-2.0" publish = false -keywords = [ "iota", "serialization", "framework", "packable" ] +keywords = [ "binary", "no_std", "serialization", "packable" ] homepage = "https://www.iota.org" [[test]] @@ -16,7 +16,7 @@ name = "tests" path = "tests/lib.rs" [dev-dependencies] -packable = { version = "=0.2.0", path = "../packable", default-features = false } +packable = { version = "=0.2.1", path = "../packable", default-features = false } rustversion = { version = "1.0.5", default-features = false } trybuild = { version = "1.0.50", default-features = false, features = [ "diff" ] } diff --git a/packable/packable-derive/Cargo.toml b/packable/packable-derive/Cargo.toml index 2279f37..2336b8b 100644 --- a/packable/packable-derive/Cargo.toml +++ b/packable/packable-derive/Cargo.toml @@ -1,13 +1,13 @@ [package] name = "packable-derive" -version = "0.2.0" +version = "0.2.1" authors = [ "IOTA Stiftung" ] edition = "2021" description = "Derive macro for the `packable` crate." readme = "README.md" repository = "https://github.com/iotaledger/common-rs" license = "Apache-2.0" -keywords = [ "iota", "serialization", "framework", "packable" ] +keywords = [ "binary", "no_std", "serialization", "packable" ] homepage = "https://www.iota.org" [lib] diff --git a/packable/packable/CHANGELOG.md b/packable/packable/CHANGELOG.md index 71ffd36..63f6118 100644 --- a/packable/packable/CHANGELOG.md +++ b/packable/packable/CHANGELOG.md @@ -19,11 +19,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Security --> -## 0.X.X - 2022-XX-XX +## 0.2.1 - 2022-02-11 + +### Added + +- Make the `Error` implementation for `UnknownTagError` less restricted; +- Implement `Error` for `UnpackError`; +- Document the `serde` and `primitive-types` features; ### Changed - Make `String` packing more performant by using `Packer::pack_bytes` directly; +- Fix documentation of `UnexpectedEOF` and `UnpackPrefixError`; ## 0.2.0 - 2022-02-09 diff --git a/packable/packable/Cargo.toml b/packable/packable/Cargo.toml index ad87f92..3a05999 100644 --- a/packable/packable/Cargo.toml +++ b/packable/packable/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "packable" -version = "0.2.0" +version = "0.2.1" authors = [ "IOTA Stiftung" ] edition = "2021" description = "A crate for packing and unpacking binary representations." @@ -19,7 +19,7 @@ usize = [ ] autocfg = { version = "1.0.1", default-features = false } [dependencies] -packable-derive = { version = "=0.2.0", path = "../packable-derive", default-features = false } +packable-derive = { version = "=0.2.1", path = "../packable-derive", default-features = false } primitive-types = { version = "0.10.1", default-features = false, optional = true } serde = { version = "1.0.130", default-features = false, features = [ "derive", "std" ], optional = true } diff --git a/packable/packable/README.md b/packable/packable/README.md index 8fa5ba5..c2d9ab8 100644 --- a/packable/packable/README.md +++ b/packable/packable/README.md @@ -39,16 +39,26 @@ Check the `Packable` `impl` section for further information. ## Features -### `std` - -This feature implements `Error` for all the error types provided by this crate. - ### `io` This feature provides the types `IoPacker` and `IoUnpacker` which allow packing and unpacking from values whose types implement `Write` and `Read` respectively. +### `primitive-types` + +This feature implements `Packable` for `U256` encoding its values as arrays of +bytes in little-endian order. + +### `serde` + +This feature derives `Serialize` and `Deserialize` for the types provided in +the `bounded` and `prefix` modules + +### `std` + +This feature implements `Error` for all the error types provided by this crate. + ### `usize` This feature implements `Packable` for `usize`, `isize`, `Vec`, `Box<[T]>` diff --git a/packable/packable/src/error.rs b/packable/packable/src/error.rs index 0548b5e..6cfc9a1 100644 --- a/packable/packable/src/error.rs +++ b/packable/packable/src/error.rs @@ -96,6 +96,27 @@ impl UnpackError { } } +impl fmt::Display for UnpackError +where + T: fmt::Display, + U: fmt::Display, +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Packable(err) => write!(f, "packable error while unpacking: {}", err), + Self::Unpacker(err) => write!(f, "unpacker error while unpacking: {}", err), + } + } +} + +#[cfg(feature = "std")] +impl std::error::Error for UnpackError +where + T: std::error::Error, + U: std::error::Error, +{ +} + /// We cannot provide a [`From`] implementation because [`Infallible`] is not from this crate. #[allow(clippy::from_over_into)] impl Into for UnpackError { @@ -110,7 +131,7 @@ impl Into for UnpackError { pub struct UnknownTagError(pub T); #[cfg(feature = "std")] -impl std::error::Error for UnknownTagError where T: std::error::Error {} +impl std::error::Error for UnknownTagError where T: fmt::Display + fmt::Debug {} impl From for UnknownTagError { fn from(err: Infallible) -> Self { @@ -124,7 +145,7 @@ impl fmt::Display for UnknownTagError { } } -/// Error type to be raised when [`&[u8]`] does not have enough bytes to unpack something or when +/// Error type to be raised when `&[u8]` does not have enough bytes to unpack something or when /// [`SlicePacker`]('crate::packer::SlicePacker') does not have enough space to pack something. #[derive(Debug)] pub struct UnexpectedEOF { diff --git a/packable/packable/src/lib.rs b/packable/packable/src/lib.rs index 07d8acb..23f9c7c 100644 --- a/packable/packable/src/lib.rs +++ b/packable/packable/src/lib.rs @@ -41,17 +41,27 @@ //! //! # Features //! -//! ## `std` -//! -//! This feature implements [`Error`](std::error::Error) for all the error types provided by this -//! crate. -//! //! ## `io` //! //! This feature provides the types [`IoPacker`](packer::IoPacker) and //! [`IoUnpacker`](unpacker::IoUnpacker) which allow packing and unpacking from values whose types //! implement [`Write`](std::io::Write) and [`Read`](std::io::Read) respectively. //! +//! ## `primitive-types` +//! +//! This feature implements [`Packable`] for [`U256`](primitive_types::U256) encoding its values as +//! arrays of bytes in little-endian order. +//! +//! ## `serde` +//! +//! This feature derives [`Serialize`](serde::Serialize) and [`Deserialize`](serde::Deserialize) +//! for the types provided in the [`mod@bounded`] and [`prefix`] modules +//! +//! ## `std` +//! +//! This feature implements [`Error`](std::error::Error) for all the error types provided by this +//! crate. +//! //! ## `usize` //! //! This feature implements [`Packable`] for [`usize`], [`isize`], [`Vec`](std::vec::Vec), diff --git a/packable/packable/src/packable/prefix.rs b/packable/packable/src/packable/prefix.rs index 6ae139a..73e7f1b 100644 --- a/packable/packable/src/packable/prefix.rs +++ b/packable/packable/src/packable/prefix.rs @@ -21,8 +21,7 @@ use core::{ ops::{Deref, DerefMut, Range}, }; -/// Semantic error raised while unpacking a dynamically-sized sequences that use a type different than `usize` for their -/// length-prefix. +/// Semantic error raised while unpacking dynamically-sized sequences. #[derive(Debug)] pub enum UnpackPrefixError { /// Semantic error raised while unpacking an item of the sequence. Typically this is