Skip to content

Commit

Permalink
Update to syn 2.0, darling 0.20 (#386)
Browse files Browse the repository at this point in the history
* Update to syn 2.0, darling 0.20

- Change `type` for enums into `id_type`, as this is no longer
accepted through the syn parser library

* Update to rstest 0.18
  • Loading branch information
wcampbell0x2a committed Dec 28, 2023
1 parent 920b3cf commit 44554c5
Show file tree
Hide file tree
Showing 28 changed files with 118 additions and 119 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ This also disallows using tuples for storing the id:
old:
```rust
#[derive(PartialEq, Debug, DekuRead, DekuWrite)]
#[deku(type = "u8")]
#[deku(id_type = "u8")]
enum DekuTest {
#[deku(id_pat = "_")]
VariantC((u8, u8)),
Expand All @@ -121,7 +121,7 @@ enum DekuTest {
new:
```rust
#[derive(PartialEq, Debug, DekuRead, DekuWrite)]
#[deku(type = "u8")]
#[deku(id_type = "u8")]
enum DekuTest {
#[deku(id_pat = "_")]
VariantC {
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ log = { version = "0.4.17", optional = true }
no_std_io = { version = "0.5.0", default-features = false, features = ["alloc"] }

[dev-dependencies]
rstest = "0.16.0"
rstest = "0.18.0"
hexlit = "0.5.5"
criterion = "0.4.0"
alloc_counter = "0.0.4"
Expand Down
2 changes: 1 addition & 1 deletion benches/deku.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct DekuBytes {
}

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(type = "u8")]
#[deku(id_type = "u8")]
enum DekuEnum {
#[deku(id = "0x01")]
VariantA(u8),
Expand Down
6 changes: 3 additions & 3 deletions deku-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ logging = []

[dependencies]
quote = "1.0"
syn = "1.0"
syn = "2.0"
# extra-traits gives us Debug
# syn = {version = "1.0", features = ["extra-traits"]}
proc-macro2 = "1.0"
darling = "0.14"
darling = "0.20"
proc-macro-crate = { version = "1.3.0", optional = true }

[dev-dependencies]
rstest = "0.16"
rstest = "0.18"
30 changes: 14 additions & 16 deletions deku-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::AttributeArgs;

use crate::macros::deku_read::emit_deku_read;
use crate::macros::deku_write::emit_deku_write;
Expand Down Expand Up @@ -205,7 +204,10 @@ impl DekuData {
ast::Data::Struct(_) => {
// Validate id_* attributes are being used on an enum
if data.id_type.is_some() {
Err(cerror(data.id_type.span(), "`type` only supported on enum"))
Err(cerror(
data.id_type.span(),
"`id_type` only supported on enum",
))
} else if data.id.is_some() {
Err(cerror(data.id.span(), "`id` only supported on enum"))
} else if data.bytes.is_some() {
Expand All @@ -217,19 +219,19 @@ impl DekuData {
}
}
ast::Data::Enum(_) => {
// Validate `type` or `id` is specified
// Validate `id_type` or `id` is specified
if data.id_type.is_none() && data.id.is_none() {
return Err(cerror(
data.ident.span(),
"`type` or `id` must be specified on enum",
"`id_type` or `id` must be specified on enum",
));
}

// Validate either `type` or `id` is specified
// Validate either `id_type` or `id` is specified
if data.id_type.is_some() && data.id.is_some() {
return Err(cerror(
data.ident.span(),
"conflicting: both `type` and `id` specified on enum",
"conflicting: both `id_type` and `id` specified on enum",
));
}

Expand Down Expand Up @@ -635,11 +637,7 @@ struct DekuReceiver {
id: Option<Id>,

/// enum only: type of the enum `id`
#[darling(
rename = "type",
default = "default_res_opt",
map = "map_litstr_as_tokenstream"
)]
#[darling(default = "default_res_opt", map = "map_litstr_as_tokenstream")]
id_type: Result<Option<TokenStream>, ReplacementError>,

/// enum only: bit size of the enum `id`
Expand Down Expand Up @@ -875,7 +873,7 @@ pub fn proc_deku_write(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
}

fn is_not_deku(attr: &syn::Attribute) -> bool {
attr.path
attr.path()
.get_ident()
.map(|ident| ident != "deku" && ident != "deku_derive")
.unwrap_or(true)
Expand Down Expand Up @@ -939,8 +937,8 @@ pub fn deku_derive(
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
// Parse `deku_derive` attribute
let attr_args = syn::parse_macro_input!(attr as AttributeArgs);
let args = match DekuDerive::from_list(&attr_args) {
let nested_meta = darling::ast::NestedMeta::parse_meta_list(attr.into()).unwrap();
let args = match DekuDerive::from_list(&nested_meta) {
Ok(v) => v,
Err(e) => {
return proc_macro::TokenStream::from(e.write_errors());
Expand Down Expand Up @@ -1039,9 +1037,9 @@ mod tests {
}"#),
// Valid Enum
case::enum_empty(r#"#[deku(type = "u8")] enum Test {}"#),
case::enum_empty(r#"#[deku(id_type = "u8")] enum Test {}"#),
case::enum_all(r#"
#[deku(type = "u8")]
#[deku(id_type = "u8")]
enum Test {
#[deku(id = "1")]
A,
Expand Down
2 changes: 1 addition & 1 deletion examples/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use deku::{prelude::*, reader::Reader};
use hexlit::hex;

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(type = "u8")]
#[deku(id_type = "u8")]
enum DekuTest {
#[deku(id = "0")]
Var1,
Expand Down
2 changes: 1 addition & 1 deletion examples/enums_catch_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use deku::prelude::*;
use hexlit::hex;

#[derive(Clone, Copy, PartialEq, Eq, Debug, DekuWrite, DekuRead)]
#[deku(type = "u8")]
#[deku(id_type = "u8")]
#[non_exhaustive]
#[repr(u8)]
pub enum DekuTest {
Expand Down
10 changes: 5 additions & 5 deletions src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ Example:
# use std::io::Cursor;
# use std::convert::{TryInto, TryFrom};
# #[derive(PartialEq, Debug, DekuRead, DekuWrite)]
#[deku(type = "u8")]
#[deku(id_type = "u8")]
enum DekuTest {
#[deku(id = "0x01")]
VariantA(u8),
Expand Down Expand Up @@ -1057,7 +1057,7 @@ Example discriminant
# use std::io::Cursor;
# use std::convert::{TryInto, TryFrom};
# #[derive(PartialEq, Debug, DekuRead, DekuWrite)]
#[deku(type = "u8")]
#[deku(id_type = "u8")]
enum DekuTest {
VariantA = 0x01,
VariantB,
Expand Down Expand Up @@ -1099,7 +1099,7 @@ Example:
# use std::io::Cursor;
# use std::convert::{TryInto, TryFrom};
# #[derive(PartialEq, Debug, DekuRead, DekuWrite)]
#[deku(type = "u8")]
#[deku(id_type = "u8")]
enum DekuTest {
#[deku(id = "0x01")]
VariantA(u8),
Expand Down Expand Up @@ -1151,7 +1151,7 @@ Example:
# use std::io::Cursor;
# use std::convert::{TryInto, TryFrom};
# #[derive(PartialEq, Debug, DekuRead, DekuWrite)]
#[deku(type = "u8", bits = "4")]
#[deku(id_type = "u8", bits = "4")]
enum DekuTest {
#[deku(id = "0b1001")]
VariantA( #[deku(bits = "4")] u8, u8),
Expand Down Expand Up @@ -1182,7 +1182,7 @@ Example:
# use deku::prelude::*;
# use std::convert::{TryInto, TryFrom};
# #[derive(PartialEq, Debug, DekuRead, DekuWrite)]
#[deku(type = "u32", bytes = "2")]
#[deku(id_type = "u32", bytes = "2")]
enum DekuTest {
#[deku(id = "0xBEEF")]
VariantA(u8),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Example:
use deku::prelude::*;
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(type = "u8")]
#[deku(id_type = "u8")]
enum DekuTest {
#[deku(id = "0x01")]
VariantA,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ struct NestedStruct {
}

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(type = "u8", ctx = "_endian: Endian")]
#[deku(id_type = "u8", ctx = "_endian: Endian")]
enum NestedEnum {
#[deku(id = "0x01")]
VarA(u8),
}

#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
#[deku(type = "u32", bytes = "2", ctx = "_endian: Endian")]
#[deku(id_type = "u32", bytes = "2", ctx = "_endian: Endian")]
enum NestedEnum2 {
#[deku(id = "0x01")]
VarA(u8),
Expand Down
6 changes: 3 additions & 3 deletions tests/test_attributes/test_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn test_ctx_struct() {
#[test]
fn test_top_level_ctx_enum() {
#[derive(PartialEq, Debug, DekuRead, DekuWrite)]
#[deku(type = "u8", ctx = "a: u8, b: u8")]
#[deku(id_type = "u8", ctx = "a: u8, b: u8")]
enum TopLevelCtxEnum {
#[deku(id = "1")]
VariantA(
Expand Down Expand Up @@ -80,7 +80,7 @@ fn test_top_level_ctx_enum() {
#[test]
fn test_top_level_ctx_enum_default() {
#[derive(PartialEq, Debug, DekuRead, DekuWrite)]
#[deku(type = "u8", ctx = "a: u8, b: u8", ctx_default = "1,2")]
#[deku(id_type = "u8", ctx = "a: u8, b: u8", ctx_default = "1,2")]
enum TopLevelCtxEnumDefault {
#[deku(id = "1")]
VariantA(
Expand Down Expand Up @@ -240,7 +240,7 @@ fn test_ctx_default_struct() {
#[test]
fn test_enum_endian_ctx() {
#[derive(PartialEq, Debug, DekuRead, DekuWrite)]
#[deku(type = "u32", endian = "endian", ctx = "endian: deku::ctx::Endian")]
#[deku(id_type = "u32", endian = "endian", ctx = "endian: deku::ctx::Endian")]
enum EnumTypeEndianCtx {
#[deku(id = "0xDEADBEEF")]
VarA(u8),
Expand Down
4 changes: 2 additions & 2 deletions tests/test_attributes/test_temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn test_temp_field_unnamed_write() {
fn test_temp_enum_field() {
#[deku_derive(DekuRead, DekuWrite)]
#[derive(PartialEq, Debug)]
#[deku(type = "u8")]
#[deku(id_type = "u8")]
enum TestEnum {
#[deku(id = "0xAB")]
VarA {
Expand Down Expand Up @@ -133,7 +133,7 @@ fn test_temp_enum_field() {
fn test_temp_enum_field_write() {
#[deku_derive(DekuRead, DekuWrite)]
#[derive(PartialEq, Debug)]
#[deku(type = "u8")]
#[deku(id_type = "u8")]
enum TestEnum {
#[deku(id = "0xAB")]
VarA {
Expand Down
4 changes: 2 additions & 2 deletions tests/test_catch_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod test {

/// Basic test struct
#[derive(Clone, Copy, PartialEq, Eq, Debug, DekuWrite, DekuRead)]
#[deku(type = "u8")]
#[deku(id_type = "u8")]
#[non_exhaustive]
#[repr(u8)]
pub enum BasicMapping {
Expand All @@ -21,7 +21,7 @@ mod test {

/// Advanced test struct
#[derive(Clone, Copy, PartialEq, Eq, Debug, DekuWrite, DekuRead)]
#[deku(type = "u8")]
#[deku(id_type = "u8")]
#[non_exhaustive]
#[repr(u8)]
pub enum AdvancedRemapping {
Expand Down
4 changes: 2 additions & 2 deletions tests/test_compile/cases/bits_bytes_conflict.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use deku::prelude::*;

#[derive(DekuRead)]
#[deku(type = "u8", bits = "1", bytes = "2")]
#[deku(id_type = "u8", bits = "1", bytes = "2")]
enum Test1 {}

#[derive(DekuRead)]
#[deku(type = "u8")]
#[deku(id_type = "u8")]
enum Test2 {
A(#[deku(bits = "1", bytes = "2")] u8),
B {
Expand Down
6 changes: 3 additions & 3 deletions tests/test_compile/cases/bits_bytes_conflict.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: conflicting: both `bits` and `bytes` specified on enum
--> $DIR/bits_bytes_conflict.rs:4:28
--> $DIR/bits_bytes_conflict.rs:4:31
|
4 | #[deku(type = "u8", bits = "1", bytes = "2")]
| ^^^
4 | #[deku(id_type = "u8", bits = "1", bytes = "2")]
| ^^^

error: conflicting: both `bits` and `bytes` specified on field
--> $DIR/bits_bytes_conflict.rs:10:21
Expand Down
2 changes: 1 addition & 1 deletion tests/test_compile/cases/catch_all_multiple.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use deku::prelude::*;

#[derive(DekuRead)]
#[deku(type = "u8")]
#[deku(id_type = "u8")]
enum Test1 {
#[deku(default)]
A = 1,
Expand Down
Loading

0 comments on commit 44554c5

Please sign in to comment.