-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
packable: add a struct-level verify_with (#71)
* Add a struct-level verify_with * Bump versions * Update release date * Nits
- Loading branch information
1 parent
eab7b46
commit dd2f346
Showing
14 changed files
with
179 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
[package] | ||
name = "packable-derive-test" | ||
version = "0.7.0" | ||
authors = [ "IOTA Stiftung" ] | ||
version = "0.0.0" | ||
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 = [ "binary", "no_std", "serialization", "packable" ] | ||
keywords = ["binary", "no_std", "serialization", "packable"] | ||
homepage = "https://www.iota.org" | ||
|
||
[[test]] | ||
name = "tests" | ||
path = "tests/lib.rs" | ||
|
||
[dev-dependencies] | ||
packable = { version = "=0.8.3", path = "../packable", default-features = false } | ||
packable = { version = "=0.9.0", path = "../packable", default-features = false } | ||
|
||
rustversion = { version = "1.0.9", default-features = false } | ||
trybuild = { version = "1.0.71", default-features = false, features = [ "diff" ] } | ||
rustversion = { version = "1.0.14", default-features = false } | ||
trybuild = { version = "1.0.85", default-features = false, features = ["diff"] } | ||
|
||
[package.metadata.cargo-udeps.ignore] | ||
development = [ "packable" ] | ||
development = ["packable"] |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packable/packable-derive-test/tests/fail/invalid_field_type_verify_with_struct.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2023 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#![allow(unused_imports)] | ||
|
||
use core::convert::Infallible; | ||
|
||
use packable::{ | ||
error::{UnknownTagError, UnpackError, UnpackErrorExt}, | ||
packer::Packer, | ||
unpacker::Unpacker, | ||
Packable, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub struct PickyError(u8); | ||
|
||
impl From<Infallible> for PickyError { | ||
fn from(err: Infallible) -> Self { | ||
match err {} | ||
} | ||
} | ||
|
||
fn verify<const VERIFY: bool>(&value: &u64, _: &()) -> Result<(), PickyError> { | ||
if !VERIFY || value == 42 { | ||
Ok(()) | ||
} else { | ||
Err(PickyError(value as u8)) | ||
} | ||
} | ||
|
||
#[derive(Packable)] | ||
#[packable(unpack_error = PickyError)] | ||
#[packable(verify_with = verify)] | ||
pub struct Picky(u8); | ||
|
||
fn main() {} |
17 changes: 17 additions & 0 deletions
17
packable/packable-derive-test/tests/fail/invalid_field_type_verify_with_struct.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
error[E0308]: mismatched types | ||
--> tests/fail/invalid_field_type_verify_with_struct.rs:32:10 | ||
| | ||
32 | #[derive(Packable)] | ||
| ^^^^^^^^ | ||
| | | ||
| expected `&u64`, found `&Picky` | ||
| arguments to this function are incorrect | ||
| | ||
= note: expected reference `&u64` | ||
found reference `&Picky` | ||
note: function defined here | ||
--> tests/fail/invalid_field_type_verify_with_struct.rs:24:4 | ||
| | ||
24 | fn verify<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) |
File renamed without changes.
37 changes: 37 additions & 0 deletions
37
packable/packable-derive-test/tests/pass/verify_with_struct.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2023 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#![allow(unused_imports)] | ||
|
||
use core::convert::Infallible; | ||
|
||
use packable::{ | ||
error::{UnknownTagError, UnpackError, UnpackErrorExt}, | ||
packer::Packer, | ||
unpacker::Unpacker, | ||
Packable, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub struct PickyError(u8); | ||
|
||
impl From<Infallible> for PickyError { | ||
fn from(err: Infallible) -> Self { | ||
match err {} | ||
} | ||
} | ||
|
||
fn verify<const VERIFY: bool>(value: &Picky, _: &()) -> Result<(), PickyError> { | ||
if !VERIFY || value.0 == 42 { | ||
Ok(()) | ||
} else { | ||
Err(PickyError(value.0)) | ||
} | ||
} | ||
|
||
#[derive(Packable)] | ||
#[packable(unpack_error = PickyError)] | ||
#[packable(verify_with = verify)] | ||
pub struct Picky(u8); | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,31 @@ | ||
[package] | ||
name = "packable-derive" | ||
version = "0.7.0" | ||
authors = [ "IOTA Stiftung" ] | ||
version = "0.8.0" | ||
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 = [ "binary", "no_std", "serialization", "packable" ] | ||
keywords = ["binary", "no_std", "serialization", "packable"] | ||
homepage = "https://www.iota.org" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro-crate = { version = "1.2.1", default-features = false } | ||
proc-macro-error = { version = "1.0.4", default-features = false, features = [ "syn-error" ] } | ||
proc-macro2 = { version = "1.0.46", default-features = false } | ||
quote = { version = "1.0.21", default-features = false } | ||
syn = { version = "1.0.102", default-features = false, features = [ "full", "extra-traits", "parsing", "printing", "derive", "proc-macro", "clone-impls" ] } | ||
proc-macro-crate = { version = "1.3.1", default-features = false } | ||
proc-macro-error = { version = "1.0.4", default-features = false, features = [ | ||
"syn-error", | ||
] } | ||
proc-macro2 = { version = "1.0.69", default-features = false } | ||
quote = { version = "1.0.33", default-features = false } | ||
syn = { version = "1.0.109", default-features = false, features = [ | ||
"full", | ||
"extra-traits", | ||
"parsing", | ||
"printing", | ||
"derive", | ||
"proc-macro", | ||
"clone-impls", | ||
] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
[package] | ||
name = "packable" | ||
version = "0.8.3" | ||
authors = [ "IOTA Stiftung" ] | ||
version = "0.9.0" | ||
authors = ["IOTA Stiftung"] | ||
edition = "2021" | ||
description = "A crate for packing and unpacking binary representations." | ||
readme = "README.md" | ||
repository = "https://github.com/iotaledger/common-rs" | ||
license = "Apache-2.0" | ||
keywords = [ "binary", "no_std", "serialization", "packable" ] | ||
keywords = ["binary", "no_std", "serialization", "packable"] | ||
homepage = "https://www.iota.org" | ||
|
||
[features] | ||
io = [ "std" ] | ||
std = [ "serde?/std", "primitive-types?/std" ] | ||
usize = [ ] | ||
io = ["std"] | ||
std = ["serde?/std", "primitive-types?/std"] | ||
usize = [] | ||
|
||
[build-dependencies] | ||
autocfg = { version = "1.1.0", default-features = false } | ||
|
||
[dependencies] | ||
packable-derive = { version = "=0.7.0", path = "../packable-derive", default-features = false } | ||
packable-derive = { version = "=0.8.0", path = "../packable-derive", default-features = false } | ||
|
||
primitive-types = { version = "0.12.0", default-features = false, optional = true } | ||
serde = { version = "1.0.145", default-features = false, features = [ "derive" ], optional = true } | ||
primitive-types = { version = "0.12.2", default-features = false, optional = true } | ||
serde = { version = "1.0.192", default-features = false, features = [ | ||
"derive", | ||
], optional = true } |