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

Update syn requirement from 1.0.95 to 2.0.63 #67

Merged
merged 5 commits into from
May 16, 2024
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]

resolver = "2"
members = [
"positional",
"positional_derive",
Expand Down
2 changes: 1 addition & 1 deletion positional_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ proc-macro = true
proc-macro-error = "1.0.4"
proc-macro2 = "1.0.39"
quote = "1.0"
syn = {version = "1.0.95", features = ["extra-traits", "full"]}
syn = {version = "2.0.63", features = ["extra-traits", "full"]}
trybuild = "1.0.61"
119 changes: 26 additions & 93 deletions positional_derive/src/analyze/field.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use proc_macro_error::abort;
use std::collections::HashMap;
use syn::{Lit, LitChar, LitInt, LitStr, Meta, NestedMeta};
use syn::{LitChar, LitInt, LitStr};

const FIELD_ATTRIBUTE: &str = "field";

Expand All @@ -26,106 +25,40 @@ fn parse_field_attributes(field: &syn::Field) -> Option<(LitInt, Option<LitChar>
field
.attrs
.iter()
.find(|attribute| attribute.path.is_ident(FIELD_ATTRIBUTE))
.find(|attribute| attribute.path().is_ident(FIELD_ATTRIBUTE))
.map(parse_field_attribute_meta)
}

fn parse_field_attribute_meta(
attribute: &syn::Attribute,
) -> (LitInt, Option<LitChar>, Option<LitStr>) {
match attribute.parse_meta() {
Ok(meta) => {
let mut attrs = HashMap::new();
parse_meta(&meta, &mut attrs);
let mut size: Option<LitInt> = None;
let mut align: Option<LitStr> = None;
let mut filler: Option<LitChar> = None;

let size = match attrs.get("size") {
None => {
abort!(
attribute,
"wrong field configuration";
help = "you need to provide at least a size configuration to the field"
)
}
Some(size_lit) => match size_lit {
Lit::Int(lit_int) => lit_int,
_ => {
abort!(
attribute,
"wrong field configuration";
help = "the size configuration should be a number"
)
}
},
};

let filler = match attrs.get("filler") {
None => None,
Some(filler_lit) => match filler_lit {
Lit::Char(lit_char) => Some(lit_char),
_ => {
abort!(
attribute,
"wrong field configuration";
help = "the filler configuration should be a char"
)
}
},
};

let align = match attrs.get("align") {
None => None,
Some(filler_align) => match filler_align {
Lit::Str(lit_str) => Some(lit_str),
_ => {
abort!(
attribute,
"wrong field configuration";
help = "the align configuration should be a string"
)
}
},
};

(size.clone(), filler.cloned(), align.cloned())
}
Err(_) => {
abort!(
attribute,
"wrong field configuration";
help = "unable to parse field configuration"
)
let parse_result = attribute.parse_nested_meta(|meta| {
if meta.path.is_ident("size") {
size = Some(meta.value()?.parse()?);
} else if meta.path.is_ident("align") {
align = Some(meta.value()?.parse()?);
} else if meta.path.is_ident("filler") {
filler = Some(meta.value()?.parse()?);
} else {
return Err(meta.error("unsupported attribute"));
}
Ok(())
});

if let Err(err) = parse_result {
abort!(err.span(), "failed to parse field attribute"; note = err.to_string());
}
}

fn parse_meta(meta: &syn::Meta, attrs: &mut HashMap<String, syn::Lit>) {
match meta {
Meta::Path(path) => {
abort!(
path,
"wrong field configuration";
help = "there should only be name = value couple inside the field configuration"
)
}
Meta::List(meta_list) => {
for nested_meta in &meta_list.nested {
match nested_meta {
NestedMeta::Meta(name_value) => parse_meta(name_value, attrs),
NestedMeta::Lit(lit) => {
abort!(
lit,
"wrong field configuration";
help = "there should only be name = value couple inside the field configuration"
)
}
}
}
}
Meta::NameValue(name_value) => {
attrs.insert(
name_value.path.get_ident().unwrap().to_string(),
name_value.lit.clone(),
);
}
match size {
Some(size) => (size, filler, align),
None => abort!(
attribute,
"wrong field configuration";
help = "you need to provide at least a size configuration to the field"
),
}
}
35 changes: 11 additions & 24 deletions positional_derive/src/analyze/variant.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use proc_macro_error::abort;
use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned;
use syn::{parenthesized, Expr, Fields};
use syn::{Expr, Fields};

const MATCHER_ATTRIBUTE: &str = "matcher";

Expand All @@ -16,17 +14,6 @@ pub struct Matcher {
pub expr: Expr,
}

impl Parse for Matcher {
fn parse(input: ParseStream) -> syn::Result<Self> {
let content;
let _parenthesis = parenthesized!(content in input);

Ok(Self {
expr: content.parse()?,
})
}
}

impl Variant {
pub fn new(variant: syn::Variant) -> Option<Self> {
match variant.clone().fields {
Expand Down Expand Up @@ -70,19 +57,19 @@ fn parse_variant_attributes(variant: &syn::Variant) -> Option<Matcher> {
variant
.attrs
.iter()
.find(|attribute| attribute.path.is_ident(MATCHER_ATTRIBUTE))
.find(|attribute| attribute.path().is_ident(MATCHER_ATTRIBUTE))
.map(parse_matcher_expression)
}

fn parse_matcher_expression(attribute: &syn::Attribute) -> Matcher {
let span = attribute.tokens.span();
if let Ok(matcher) = syn::parse2::<Matcher>(attribute.tokens.clone()) {
matcher
} else {
abort!(
span,
"expected an expression as matcher";
help = "example syntax: `#[matcher(row[0..2] == \"00\")]`"
)
match attribute.parse_args::<Expr>() {
Ok(expr) => Matcher { expr },
Err(err) => {
abort!(
err.span(),
"expected an expression as matcher";
help = "example syntax: `#[matcher(row[0..2] == \"00\")]`"
)
}
}
}
8 changes: 4 additions & 4 deletions positional_derive/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ use syn::{Data, DeriveInput};
pub type Ast = DeriveInput;

pub fn parse(tokens: TokenStream) -> Ast {
match syn::parse2::<syn::DeriveInput>(tokens) {
// the derive is applied to a struct
match syn::parse2::<DeriveInput>(tokens) {
// the derivation is applied to a struct
Ok(
item @ DeriveInput {
data: Data::Struct(_),
..
},
) => item,
// the derive is applied to an enum
// the derivation is applied to an enum
Ok(
item @ DeriveInput {
data: Data::Enum(_),
..
},
) => item,
// the derive is applied to a union
// the derivation is applied to a union
Ok(
item @ DeriveInput {
data: Data::Union(_),
Expand Down
4 changes: 2 additions & 2 deletions positional_derive/tests/ui/enums-with-wrong-matcher.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error: expected an expression as matcher

= help: example syntax: `#[matcher(row[0..2] == "00")]`

--> tests/ui/enums-with-wrong-matcher.rs:5:14
--> tests/ui/enums-with-wrong-matcher.rs:5:15
|
5 | #[matcher(struct)]
| ^^^^^^^^
| ^^^^^^
4 changes: 2 additions & 2 deletions positional_derive/tests/ui/list-attributes.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
error: wrong field configuration
error: failed to parse field attribute

= help: there should only be name = value couple inside the field configuration
= note: unsupported attribute

--> tests/ui/list-attributes.rs:5:13
|
Expand Down
4 changes: 2 additions & 2 deletions positional_derive/tests/ui/path-attributes.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
error: wrong field configuration
error: failed to parse field attribute

= help: there should only be name = value couple inside the field configuration
= note: expected attribute arguments in parentheses: #[field(...)]

--> tests/ui/path-attributes.rs:5:7
|
Expand Down
8 changes: 4 additions & 4 deletions positional_derive/tests/ui/wrong-filler-type.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: wrong field configuration
error: failed to parse field attribute

= help: the filler configuration should be a char
= note: expected character literal

--> tests/ui/wrong-filler-type.rs:5:5
--> tests/ui/wrong-filler-type.rs:5:33
|
5 | #[field(size = 10, filler = "a")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^
Loading