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

Migrate neli-proc-macros to syn 2.0.x #241

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 neli-proc-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ version = "1"
features = ["derive"]

[dependencies.syn]
version = "1.0.98"
version = "2.0"
features = ["full", "extra-traits"]
8 changes: 5 additions & 3 deletions neli-proc-macros/src/neli_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ use crate::shared::remove_bad_attrs;
fn parse_type_attr(attr: Meta) -> Type {
if let Meta::NameValue(nv) = attr {
if nv.path == parse_str::<Path>("serialized_type").unwrap() {
if let Lit::Str(ls) = nv.lit {
return parse_str::<Type>(&ls.value())
.unwrap_or_else(|_| panic!("Invalid type supplied: {}", ls.value()));
if let Expr::Lit(el) = nv.value {
if let Lit::Str(ls) = el.lit {
return parse_str::<Type>(&ls.value())
.unwrap_or_else(|_| panic!("Invalid type supplied: {}", ls.value()));
}
}
}
}
Expand Down
99 changes: 39 additions & 60 deletions neli-proc-macros/src/shared.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::{any::type_name, collections::HashMap};

use proc_macro2::{Span, TokenStream as TokenStream2};
use proc_macro2::{Span, TokenStream as TokenStream2, TokenTree};
use quote::{quote, ToTokens};
use syn::{
parse::Parse,
parse_str,
punctuated::Punctuated,
token::{Add, Colon2},
token::{PathSep, Plus},
Attribute, Expr, Fields, FieldsNamed, FieldsUnnamed, GenericParam, Generics, Ident, Index,
ItemStruct, Lit, Meta, MetaNameValue, NestedMeta, Path, PathArguments, PathSegment, Token,
ItemStruct, Meta, MetaNameValue, Path, PathArguments, PathSegment, Token,
TraitBound, TraitBoundModifier, Type, TypeParam, TypeParamBound, Variant,
};

Expand Down Expand Up @@ -145,7 +145,7 @@ fn path_from_idents(idents: &[&str]) -> Path {
ident: Ident::new(ident, Span::call_site()),
arguments: PathArguments::None,
})
.collect::<Punctuated<PathSegment, Colon2>>(),
.collect::<Punctuated<PathSegment, PathSep>>(),
}
}

Expand Down Expand Up @@ -223,17 +223,9 @@ pub fn process_impl_generics(
pub fn remove_bad_attrs(attrs: Vec<Attribute>) -> Vec<Attribute> {
attrs
.into_iter()
.filter(|attr| {
if let Ok(meta) = attr.parse_meta() {
match meta {
Meta::NameValue(MetaNameValue { path, .. }) => {
!(path == parse_str::<Path>("doc").expect("doc should be valid path"))
}
_ => true,
}
} else {
panic!("Could not parse provided attribute {}", attr.tokens,)
}
.filter(|attr| match &attr.meta {
Meta::NameValue(MetaNameValue { path, .. }) => !path.is_ident("doc"),
_ => true,
})
.collect()
}
Expand Down Expand Up @@ -277,10 +269,7 @@ where
{
let attrs = remove_bad_attrs(attrs)
.into_iter()
.map(|attr| {
attr.parse_meta()
.unwrap_or_else(|_| panic!("Failed to parse attribute {}", attr.tokens))
})
.map(|attr| attr.meta)
.collect::<Vec<_>>();
let arm = generate_pat_and_expr(
enum_name,
Expand Down Expand Up @@ -372,17 +361,11 @@ pub fn generate_unnamed_fields(fields: FieldsUnnamed, uses_self: bool) -> Vec<Fi
/// Returns [`true`] if the given attribute is present in the list.
fn attr_present(attrs: &[Attribute], attr_name: &str) -> bool {
for attr in attrs {
let meta = attr
.parse_meta()
.unwrap_or_else(|_| panic!("Failed to parse attribute {}", attr.tokens));
if let Meta::List(list) = meta {
if list.path == parse_str::<Path>("neli").expect("neli is valid path") {
for nested in list.nested {
if let NestedMeta::Meta(Meta::Path(path)) = nested {
if path
== parse_str::<Path>(attr_name)
.unwrap_or_else(|_| panic!("{} should be valid path", attr_name))
{
if let Meta::List(list) = &attr.meta {
if list.path.is_ident("neli") {
for token in list.tokens.clone() {
if let TokenTree::Ident(ident) = token {
if ident == attr_name {
return true;
}
}
Expand All @@ -403,37 +386,33 @@ where
{
let mut output = Vec::new();
for attr in attrs {
let meta = attr
.parse_meta()
.unwrap_or_else(|_| panic!("Failed to parse attribute {}", attr.tokens));
if let Meta::List(list) = meta {
if let Meta::List(list) = &attr.meta {
if list.path == parse_str::<Path>("neli").expect("neli is valid path") {
for nested in list.nested {
if let NestedMeta::Meta(Meta::NameValue(MetaNameValue {
path,
lit: Lit::Str(lit),
..
})) = nested
{
if path
== parse_str::<Path>(attr_name)
.unwrap_or_else(|_| panic!("{} should be valid path", attr_name))
{
output.push(Some(parse_str::<T>(&lit.value()).unwrap_or_else(|_| {
panic!(
"{} should be valid tokens of type {}",
&lit.value(),
type_name::<T>()
)
})));
}
} else if let NestedMeta::Meta(Meta::Path(path)) = nested {
if path
== parse_str::<Path>(attr_name)
.unwrap_or_else(|_| panic!("{} should be valid path", attr_name))
{
output.push(None);
let (found_ident, found_literal) = list.tokens.clone().into_iter().fold(
(false, String::new()),
|(found_ident, found_literal), token| {
match &token {
TokenTree::Literal(literal) => {
let literal_str = literal.to_string();
let stripped_literal = &literal_str[1..literal_str.len() - 1]; // removes extra quotes at the ends of the string
(found_ident, String::from(stripped_literal))
}
TokenTree::Ident(ident) if ident == attr_name => (true, found_literal),
_ => (found_ident, found_literal),
}
},
);
if found_ident {
if !found_literal.is_empty() {
output.push(Some(parse_str::<T>(&found_literal).unwrap_or_else(|_| {
panic!(
"{} should be valid tokens of type {}",
&found_literal.to_string().as_str(),
type_name::<T>()
)
})));
} else {
output.push(None);
}
}
}
Expand Down Expand Up @@ -535,7 +514,7 @@ pub fn process_size(attrs: &[Attribute]) -> Option<Expr> {
/// ```
fn override_trait_bounds_on_generics(generics: &mut Generics, trait_bound_overrides: &[TypeParam]) {
let mut overrides = trait_bound_overrides.iter().cloned().fold(
HashMap::<Ident, Punctuated<TypeParamBound, Add>>::new(),
HashMap::<Ident, Punctuated<TypeParamBound, Plus>>::new(),
|mut map, param| {
if let Some(bounds) = map.get_mut(&param.ident) {
bounds.extend(param.bounds);
Expand Down