Skip to content

Commit

Permalink
Minor fixes for the packable crates (#25)
Browse files Browse the repository at this point in the history
* implement `Error` for `UnknownTagError<T>` 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
  • Loading branch information
pvdrz committed Feb 11, 2022
1 parent f77df05 commit 87e19a0
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 21 deletions.
6 changes: 3 additions & 3 deletions packable/packable-derive-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
[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."
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]]
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" ] }
Expand Down
4 changes: 2 additions & 2 deletions packable/packable-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
9 changes: 8 additions & 1 deletion packable/packable/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions packable/packable/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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."
Expand All @@ -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 }
18 changes: 14 additions & 4 deletions packable/packable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>`, `Box<[T]>`
Expand Down
25 changes: 23 additions & 2 deletions packable/packable/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,27 @@ impl<T> UnpackError<T, Infallible> {
}
}

impl<T, U> fmt::Display for UnpackError<T, U>
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<T, U> std::error::Error for UnpackError<T, U>
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<Infallible> for UnpackError<Infallible, Infallible> {
Expand All @@ -110,7 +131,7 @@ impl Into<Infallible> for UnpackError<Infallible, Infallible> {
pub struct UnknownTagError<T>(pub T);

#[cfg(feature = "std")]
impl<T> std::error::Error for UnknownTagError<T> where T: std::error::Error {}
impl<T> std::error::Error for UnknownTagError<T> where T: fmt::Display + fmt::Debug {}

impl<T> From<Infallible> for UnknownTagError<T> {
fn from(err: Infallible) -> Self {
Expand All @@ -124,7 +145,7 @@ impl<T: fmt::Display> fmt::Display for UnknownTagError<T> {
}
}

/// 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 {
Expand Down
20 changes: 15 additions & 5 deletions packable/packable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>`](std::vec::Vec),
Expand Down
3 changes: 1 addition & 2 deletions packable/packable/src/packable/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, E> {
/// Semantic error raised while unpacking an item of the sequence. Typically this is
Expand Down

0 comments on commit 87e19a0

Please sign in to comment.