Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add visitor to verify_with #57

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packable/packable-derive-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "packable-derive-test"
version = "0.6.0"
version = "0.6.1"
authors = [ "IOTA Stiftung" ]
edition = "2021"
description = "Test suite for the `packable-derive` crate."
Expand All @@ -16,7 +16,7 @@ name = "tests"
path = "tests/lib.rs"

[dev-dependencies]
packable = { version = "=0.6.0", path = "../packable", default-features = false }
packable = { version = "=0.6.1", path = "../packable", default-features = false }

rustversion = { version = "1.0.9", default-features = false }
trybuild = { version = "1.0.64", default-features = false, features = [ "diff" ] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl From<Infallible> for PickyError {
}
}

fn verify_value<const VERIFY: bool>(&value: &u64) -> Result<(), PickyError> {
fn verify_value<const VERIFY: bool>(&value: &u64, _: &()) -> Result<(), PickyError> {
if !VERIFY || value == 42 {
Ok(())
} else {
Expand All @@ -32,10 +32,6 @@ fn verify_value<const VERIFY: bool>(&value: &u64) -> Result<(), PickyError> {

#[derive(Packable)]
#[packable(unpack_error = PickyError)]
pub struct Picky(
#[packable(verify_with = verify_value)]
u8
);
pub struct Picky(#[packable(verify_with = verify_value)] u8);

fn main() {}

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ error[E0308]: mismatched types
note: function defined here
--> tests/fail/invalid_field_type_verify_with.rs:25:4
|
25 | fn verify_value<const VERIFY: bool>(&value: &u64) -> Result<(), PickyError> {
| ^^^^^^^^^^^^ ------------
25 | fn verify_value<const VERIFY: bool>(&value: &u64, _: &()) -> Result<(), PickyError> {
| ^^^^^^^^^^^^ ------------ ------
= note: this error originates in the derive macro `Packable` (in Nightly builds, run with -Z macro-backtrace for more info)
6 changes: 5 additions & 1 deletion packable/packable-derive-test/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ macro_rules! make_test {
#[rustversion::stable]
make_test!();
#[rustversion::not(stable)]
make_test!(duplicated_tag_enum, overlapping_discriminant);
make_test!(
duplicated_tag_enum,
invalid_field_type_verify_with,
overlapping_discriminant
);
7 changes: 2 additions & 5 deletions packable/packable-derive-test/tests/pass/verify_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl From<Infallible> for PickyError {
}
}

fn verify_value<const VERIFY: bool>(&value: &u8) -> Result<(), PickyError> {
fn verify_value<const VERIFY: bool>(&value: &u8, _: &()) -> Result<(), PickyError> {
if !VERIFY || value == 42 {
Ok(())
} else {
Expand All @@ -32,9 +32,6 @@ fn verify_value<const VERIFY: bool>(&value: &u8) -> Result<(), PickyError> {

#[derive(Packable)]
#[packable(unpack_error = PickyError)]
pub struct Picky(
#[packable(verify_with = verify_value)]
u8
);
pub struct Picky(#[packable(verify_with = verify_value)] u8);

fn main() {}
2 changes: 1 addition & 1 deletion packable/packable-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "packable-derive"
version = "0.6.0"
version = "0.6.1"
authors = [ "IOTA Stiftung" ]
edition = "2021"
description = "Derive macro for the `packable` crate."
Expand Down
4 changes: 2 additions & 2 deletions packable/packable-derive/src/fragments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ impl Fragments {
fields_type,
} = info;

let fields_verification = fields_verify_with.into_iter().zip(fields_ident.iter()).map(|(verify_with, field_ident)| match verify_with {
Some(verify_with) => quote!(#verify_with::<VERIFY>(&#field_ident).map_err(#crate_name::error::UnpackError::from_packable)?;),
let fields_verification = fields_verify_with.into_iter().zip(fields_ident.iter()).zip(fields_type.iter()).map(|((verify_with, field_ident), field_type)| match verify_with {
Some(verify_with) => quote!(#verify_with::<VERIFY>(&#field_ident, Borrow::<<#field_type as #crate_name::Packable>::UnpackVisitor>::borrow(visitor)).map_err(#crate_name::error::UnpackError::from_packable)?;),
None => quote!(),
});

Expand Down
6 changes: 6 additions & 0 deletions packable/packable/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Security -->

## 0.6.1 - 2022-08-26

### Added

- `verify_with` attribute functions now also take the `visitor` as parameter;

## 0.6.0 - 2022-08-25

### Added
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.6.0"
version = "0.6.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.1.0", default-features = false }

[dependencies]
packable-derive = { version = "=0.6.0", path = "../packable-derive", default-features = false }
packable-derive = { version = "=0.6.1", path = "../packable-derive", default-features = false }

primitive-types = { version = "0.11.1", default-features = false, optional = true }
serde = { version = "1.0.144", default-features = false, features = [ "derive", "std" ], optional = true }
4 changes: 2 additions & 2 deletions packable/packable/src/packable/bounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ macro_rules! bounded {
self.0
}

fn verify<const VERIFY: bool>(&value: &$ty) -> Result<(), $invalid_error<MIN, MAX>> {
fn verify<const VERIFY: bool>(&value: &$ty, _: &()) -> Result<(), $invalid_error<MIN, MAX>> {
if VERIFY && !(MIN..=MAX).contains(&value) {
Err($invalid_error(value))
} else {
Expand All @@ -70,7 +70,7 @@ macro_rules! bounded {
type Error = $invalid_error<MIN, MAX>;

fn try_from(value: $ty) -> Result<Self, Self::Error> {
Self::verify::<true>(&value)?;
Self::verify::<true>(&value, &())?;
Ok(Self(value))
}
}
Expand Down