Skip to content

Commit

Permalink
Merge pull request #627 from james-jra/syn-2
Browse files Browse the repository at this point in the history
Update pnet_macros to use syn v2
  • Loading branch information
mrmonday committed Jul 19, 2023
2 parents f5ab216 + 08622c5 commit d29848e
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 38 deletions.
2 changes: 1 addition & 1 deletion pnet_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ trybuild = "1.0.77"
[dependencies]
proc-macro2 = "1.0.50"
quote = "1.0.23"
syn = { version = "1.0.107", features = ["full"] }
syn = { version = "2.0.25", features = ["full"] }
regex = "1.7.1"
73 changes: 42 additions & 31 deletions pnet_macros/src/decorator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
use crate::util::{
operations, to_little_endian, to_mutator, Endianness, GetOperation, SetOperation,
};
use core::iter::FromIterator;
use proc_macro2::{Group, Span};
use quote::{quote, ToTokens};
use regex::Regex;
use core::iter::FromIterator;
use syn::{spanned::Spanned, Error};

#[derive(Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -161,9 +161,8 @@ fn make_packet(s: &syn::DataStruct, name: String) -> Result<Packet, Error> {
let mut packet_length = None;
let mut struct_length = None;
for attr in &field.attrs {
let node = attr.parse_meta()?;
match node {
syn::Meta::Path(p) => {
match attr.meta {
syn::Meta::Path(ref p) => {
if let Some(ident) = p.get_ident() {
if ident == "payload" {
if payload_span.is_some() {
Expand All @@ -180,7 +179,11 @@ fn make_packet(s: &syn::DataStruct, name: String) -> Result<Packet, Error> {
syn::Meta::NameValue(ref name_value) => {
if let Some(ident) = name_value.path.get_ident() {
if ident == "length_fn" {
if let syn::Lit::Str(ref s) = name_value.lit {
if let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(ref s),
..
}) = name_value.value
{
packet_length = Some(s.value() + "(&_self.to_immutable())");
} else {
return Err(Error::new(
Expand All @@ -191,7 +194,11 @@ fn make_packet(s: &syn::DataStruct, name: String) -> Result<Packet, Error> {
}
} else if ident == "length" {
// get literal
if let syn::Lit::Str(ref s) = name_value.lit {
if let syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(ref s),
..
}) = name_value.value
{
let field_names: Vec<String> = sfields
.iter()
.filter_map(|field| {
Expand All @@ -216,7 +223,7 @@ fn make_packet(s: &syn::DataStruct, name: String) -> Result<Packet, Error> {
packet_length = Some(parsed.to_string());
} else {
return Err(Error::new(
name_value.lit.span(),
name_value.value.span(),
"#[length] should be used as #[length = \
\"field_name and/or arithmetic expression\"]",
));
Expand All @@ -233,33 +240,37 @@ fn make_packet(s: &syn::DataStruct, name: String) -> Result<Packet, Error> {
if let Some(ident) = l.path.get_ident() {
if ident == "construct_with" {
let mut some_construct_with = Vec::new();
if l.nested.is_empty() {
return Err(Error::new(
l.path.span(),
"#[construct_with] must have at least one argument",
));
}

for item in &l.nested {
if let syn::NestedMeta::Meta(ref meta) = item {
let ty_str = meta.to_token_stream().to_string();
l.parse_nested_meta(|meta| {
if let Some(ident) = meta.path.get_ident() {
// #[construct_with(<type>,...)]
let ty_str = ident.to_string();
match make_type(ty_str, false) {
Ok(ty) => some_construct_with.push(ty),
Err(e) => {
return Err(Error::new(
field.ty.span(),
&format!("{}", e),
));
Ok(ty) => {
some_construct_with.push(ty);
Ok(())
}
Err(e) => Err(meta.error(e)),
}
} else {
// literal
return Err(Error::new(
l.nested.span(),
"#[construct_with] should be of the form \
#[construct_with(<types>)]",
));
// Not an ident. Something else, likely a path.
Err(meta.error("expected ident"))
}
})
.map_err(|mut err| {
err.combine(Error::new(
l.span(),
"#[construct_with] should be of the form \
#[construct_with(<primitive types>)]",
));
err
})?;

if some_construct_with.is_empty() {
return Err(Error::new(
l.span(),
"#[construct_with] must have at least one argument",
));
}
construct_with = Some(some_construct_with);
} else {
Expand Down Expand Up @@ -294,9 +305,9 @@ fn make_packet(s: &syn::DataStruct, name: String) -> Result<Packet, Error> {
inner_size += size;
} else {
return Err(Error::new(
field.span(),
"arguments to #[construct_with] must be primitives",
));
field.span(),
"arguments to #[construct_with] must be primitives",
));
}
}
if inner_size % 8 != 0 {
Expand Down
27 changes: 26 additions & 1 deletion pnet_macros/tests/compile-fail/construct-with-errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,35 @@ pub struct PacketU16 {

#[packet]
pub struct PacketU16B {
#[construct_with("test")] //~ ERROR #[construct_with] should be of the form #[construct_with(<types>)]
#[construct_with("test")] //~ ERROR #[construct_with] should be of the form #[construct_with(<primitive types>)]
banana: Toto,
#[payload]
payload: Vec<u8>,
}

#[packet]
pub struct PacketU16C {
#[construct_with(::foo:bar)] //~ ERROR #[construct_with] should be of the form #[construct_with(<primitive types>)]
banana: Toto,
#[payload]
payload: Vec<u8>,
}

#[packet]
pub struct PacketU16D {
#[construct_with(Vec<u8>)] //~ ERROR #[construct_with] should be of the form #[construct_with(<primitive types>)]
banana: Toto,
#[payload]
payload: Vec<u8>,
}

#[packet]
pub struct PacketU16E {
#[construct_with(test)] //~ ERROR arguments to #[construct_with] must be primitives
banana: Toto,
#[payload]
payload: Vec<u8>,
}


fn main() {}
44 changes: 40 additions & 4 deletions pnet_macros/tests/compile-fail/construct-with-errors.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,47 @@
error: #[construct_with] must have at least one argument
--> $DIR/construct-with-errors.rs:20:7
--> tests/compile-fail/construct-with-errors.rs:20:7
|
20 | #[construct_with()] //~ ERROR #[construct_with] must have at least one argument
| ^^^^^^^^^^^^^^

error: #[construct_with] should be of the form #[construct_with(<types>)]
--> $DIR/construct-with-errors.rs:28:22
error: unexpected literal in nested attribute, expected ident
--> tests/compile-fail/construct-with-errors.rs:28:22
|
28 | #[construct_with("test")] //~ ERROR #[construct_with] should be of the form #[construct_with(<types>)]
28 | #[construct_with("test")] //~ ERROR #[construct_with] should be of the form #[construct_with(<primitive types>)]
| ^^^^^^

error: #[construct_with] should be of the form #[construct_with(<primitive types>)]
--> tests/compile-fail/construct-with-errors.rs:28:7
|
28 | #[construct_with("test")] //~ ERROR #[construct_with] should be of the form #[construct_with(<primitive types>)]
| ^^^^^^^^^^^^^^

error: expected ident
--> tests/compile-fail/construct-with-errors.rs:36:24
|
36 | #[construct_with(::foo:bar)] //~ ERROR #[construct_with] should be of the form #[construct_with(<primitive types>)]
| ^^^

error: #[construct_with] should be of the form #[construct_with(<primitive types>)]
--> tests/compile-fail/construct-with-errors.rs:36:7
|
36 | #[construct_with(::foo:bar)] //~ ERROR #[construct_with] should be of the form #[construct_with(<primitive types>)]
| ^^^^^^^^^^^^^^

error: expected `,`
--> tests/compile-fail/construct-with-errors.rs:44:25
|
44 | #[construct_with(Vec<u8>)] //~ ERROR #[construct_with] should be of the form #[construct_with(<primitive types>)]
| ^

error: #[construct_with] should be of the form #[construct_with(<primitive types>)]
--> tests/compile-fail/construct-with-errors.rs:44:7
|
44 | #[construct_with(Vec<u8>)] //~ ERROR #[construct_with] should be of the form #[construct_with(<primitive types>)]
| ^^^^^^^^^^^^^^

error: arguments to #[construct_with] must be primitives
--> tests/compile-fail/construct-with-errors.rs:52:5
|
52 | #[construct_with(test)] //~ ERROR arguments to #[construct_with] must be primitives
| ^
9 changes: 9 additions & 0 deletions pnet_macros/tests/compile-fail/length_expr_parentheses.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ error: this file contains an unclosed delimiter
| ^^^^^^^^^ unclosed delimiter
|
= note: this error originates in the derive macro `::pnet_macros::Packet` (in Nightly builds, run with -Z macro-backtrace for more info)

error: proc-macro derive panicked
--> tests/compile-fail/length_expr_parentheses.rs:15:1
|
15 | #[packet]
| ^^^^^^^^^
|
= help: message: compiler/fallback mismatch
= note: this error originates in the attribute macro `packet` (in Nightly builds, run with -Z macro-backtrace for more info)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: variable length field must have #[length = ""] or #[length_fn = ""] attribute
--> $DIR/variable_length_fields.rs:16:17
--> tests/compile-fail/variable_length_fields.rs:16:17
|
16 | var_length: Vec<u8>, //~ ERROR: variable length field must have #[length = ""] or #[length_fn = ""] attribute
| ^^^

0 comments on commit d29848e

Please sign in to comment.