1
0
mirror of https://github.com/danog/strum.git synced 2024-11-26 12:04:38 +01:00

Update syn to v2 (#261)

This commit is contained in:
CosmicHorror 2023-06-17 18:57:26 -06:00 committed by GitHub
parent 67f0d1947d
commit 2b71e10120
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 61 deletions

View File

@ -23,7 +23,7 @@ heck = "0.4.1"
proc-macro2 = "1.0"
quote = "1.0"
rustversion = "1.0"
syn = { version = "1.0", features = ["parsing", "extra-traits"] }
syn = { version = "2.0", features = ["parsing", "extra-traits"] }
[dev-dependencies]
strum = "0.24"

View File

@ -1,12 +1,11 @@
use proc_macro2::{Span, TokenStream};
use proc_macro2::TokenStream;
use syn::{
parenthesized,
parse::{Parse, ParseStream},
parse2, parse_str,
punctuated::Punctuated,
spanned::Spanned,
Attribute, DeriveInput, Ident, Lit, LitBool, LitStr, Meta, MetaNameValue, Path, Token, Variant,
Visibility,
Attribute, DeriveInput, Expr, ExprLit, Ident, Lit, LitBool, LitStr, Meta, MetaNameValue, Path,
Token, Variant, Visibility,
};
use super::case_style::CaseStyle;
@ -76,17 +75,6 @@ impl Parse for EnumMeta {
}
}
impl Spanned for EnumMeta {
fn span(&self) -> Span {
match self {
EnumMeta::SerializeAll { kw, .. } => kw.span(),
EnumMeta::AsciiCaseInsensitive(kw) => kw.span(),
EnumMeta::Crate { kw, .. } => kw.span(),
EnumMeta::UsePhf(use_phf) => use_phf.span(),
}
}
}
pub enum EnumDiscriminantsMeta {
Derive { kw: kw::derive, paths: Vec<Path> },
Name { kw: kw::name, name: Ident },
@ -100,7 +88,7 @@ impl Parse for EnumDiscriminantsMeta {
let kw = input.parse()?;
let content;
parenthesized!(content in input);
let paths = content.parse_terminated::<_, Token![,]>(Path::parse)?;
let paths = content.parse_terminated(Path::parse, Token![,])?;
Ok(EnumDiscriminantsMeta::Derive {
kw,
paths: paths.into_iter().collect(),
@ -127,17 +115,6 @@ impl Parse for EnumDiscriminantsMeta {
}
}
impl Spanned for EnumDiscriminantsMeta {
fn span(&self) -> Span {
match self {
EnumDiscriminantsMeta::Derive { kw, .. } => kw.span,
EnumDiscriminantsMeta::Name { kw, .. } => kw.span,
EnumDiscriminantsMeta::Vis { kw, .. } => kw.span,
EnumDiscriminantsMeta::Other { path, .. } => path.span(),
}
}
}
pub trait DeriveInputExt {
/// Get all the strum metadata associated with an enum.
fn get_metadata(&self) -> syn::Result<Vec<EnumMeta>>;
@ -228,7 +205,7 @@ impl Parse for VariantMeta {
let kw = input.parse()?;
let content;
parenthesized!(content in input);
let props = content.parse_terminated::<_, Token![,]>(Prop::parse)?;
let props = content.parse_terminated(Prop::parse, Token![,])?;
Ok(VariantMeta::Props {
kw,
props: props
@ -256,22 +233,6 @@ impl Parse for Prop {
}
}
impl Spanned for VariantMeta {
fn span(&self) -> Span {
match self {
VariantMeta::Message { kw, .. } => kw.span,
VariantMeta::DetailedMessage { kw, .. } => kw.span,
VariantMeta::Documentation { value } => value.span(),
VariantMeta::Serialize { kw, .. } => kw.span,
VariantMeta::ToString { kw, .. } => kw.span,
VariantMeta::Disabled(kw) => kw.span,
VariantMeta::Default(kw) => kw.span,
VariantMeta::AsciiCaseInsensitive { kw, .. } => kw.span,
VariantMeta::Props { kw, .. } => kw.span,
}
}
}
pub trait VariantExt {
/// Get all the metadata associated with an enum variant.
fn get_metadata(&self) -> syn::Result<Vec<VariantMeta>>;
@ -282,26 +243,32 @@ impl VariantExt for Variant {
let result = get_metadata_inner("strum", &self.attrs)?;
self.attrs
.iter()
.filter(|attr| attr.path.is_ident("doc"))
.filter(|attr| attr.path().is_ident("doc"))
.try_fold(result, |mut vec, attr| {
if let Meta::NameValue(MetaNameValue {
lit: Lit::Str(value),
value:
Expr::Lit(ExprLit {
lit: Lit::Str(value),
..
}),
..
}) = attr.parse_meta()?
}) = &attr.meta
{
vec.push(VariantMeta::Documentation { value })
vec.push(VariantMeta::Documentation {
value: value.clone(),
})
}
Ok(vec)
})
}
}
fn get_metadata_inner<'a, T: Parse + Spanned>(
fn get_metadata_inner<'a, T: Parse>(
ident: &str,
it: impl IntoIterator<Item = &'a Attribute>,
) -> syn::Result<Vec<T>> {
it.into_iter()
.filter(|attr| attr.path.is_ident(ident))
.filter(|attr| attr.path().is_ident(ident))
.try_fold(Vec::new(), |mut vec, attr| {
vec.extend(attr.parse_args_with(Punctuated::<T, Token![,]>::parse_terminated)?);
Ok(vec)

View File

@ -53,14 +53,16 @@ pub fn enum_discriminants_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
.filter(|attr| {
ATTRIBUTES_TO_COPY
.iter()
.any(|attr_whitelisted| attr.path.is_ident(attr_whitelisted))
.any(|attr_whitelisted| attr.path().is_ident(attr_whitelisted))
})
.map(|attr| {
if attr.path.is_ident("strum_discriminants") {
let passthrough_group = attr
.tokens
.clone()
.into_iter()
if attr.path().is_ident("strum_discriminants") {
let mut ts = attr.meta.require_list()?.to_token_stream().into_iter();
// Discard strum_discriminants(...)
let _ = ts.next();
let passthrough_group = ts
.next()
.ok_or_else(|| strum_discriminants_passthrough_error(attr))?;
let passthrough_attribute = match passthrough_group {

View File

@ -1,6 +1,6 @@
use heck::ToShoutySnakeCase;
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote};
use quote::{format_ident, quote, ToTokens};
use syn::{Data, DeriveInput, Fields, PathArguments, Type, TypeParen};
use crate::helpers::{non_enum_error, HasStrumVariantProperties};
@ -14,8 +14,13 @@ pub fn from_repr_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
let mut discriminant_type: Type = syn::parse("usize".parse().unwrap()).unwrap();
for attr in attrs {
let path = &attr.path;
let tokens = &attr.tokens;
let path = attr.path();
let mut ts = attr.meta.require_list()?.to_token_stream().into_iter();
// Discard the path
let _ = ts.next();
let tokens: TokenStream = ts.collect();
if path.leading_colon.is_some() {
continue;
}

View File

@ -80,7 +80,7 @@ pub fn as_static_str_inner(
let mut generics = ast.generics.clone();
generics
.params
.push(syn::GenericParam::Lifetime(syn::LifetimeDef::new(
.push(syn::GenericParam::Lifetime(syn::LifetimeParam::new(
parse_quote!('_derivative_strum),
)));
let (impl_generics2, _, _) = generics.split_for_impl();