Skip to content

Commit e2e8af8

Browse files
committed
Simplify: make better usage of syn library
1 parent 45990e9 commit e2e8af8

3 files changed

Lines changed: 14 additions & 71 deletions

File tree

lib/src/generics.rs

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use proc_macro2::TokenStream;
99
use quote::{quote, ToTokens, TokenStreamExt};
1010
use syn::parse::{Parse, ParseStream, Result};
1111
use syn::punctuated::{Pair, Punctuated};
12-
use syn::{parenthesized, token};
13-
use syn::{Attribute, ConstParam, LifetimeDef, PredicateLifetime, TraitBound};
12+
use syn::token;
13+
use syn::{Attribute, ConstParam, LifetimeDef, PredicateLifetime};
1414
use syn::{BoundLifetimes, Ident, Lifetime, Token, Type};
1515

1616
/// Lifetimes and type parameters attached an item
@@ -77,16 +77,13 @@ pub struct TypeParam {
7777

7878
/// A trait or lifetime used as a bound on a type parameter.
7979
///
80-
/// This is a custom variant of [`syn::TypeParamBound`]
81-
/// which supports `trait` as a parameter bound.
80+
/// This is a superset of [`syn::TypeParamBound`].
8281
#[derive(Debug)]
8382
pub enum TypeParamBound {
84-
/// A named trait used as a bound
85-
Trait(TraitBound),
8683
/// `trait` used as a bound (substituted for the trait name by [`ToTokensSubst`])
8784
TraitSubst(Token![trait]),
88-
/// A lifetime bound
89-
Lifetime(Lifetime),
85+
/// Everything else
86+
Other(syn::TypeParamBound),
9087
}
9188

9289
/// A `where` clause in a definition: `where T: Deserialize<'de>, D: 'static`.
@@ -239,23 +236,11 @@ mod parsing {
239236

240237
impl Parse for TypeParamBound {
241238
fn parse(input: ParseStream) -> Result<Self> {
242-
if input.peek(Lifetime) {
243-
return input.parse().map(TypeParamBound::Lifetime);
244-
}
245-
246239
if input.peek(Token![trait]) {
247-
return input.parse().map(TypeParamBound::TraitSubst);
248-
}
249-
250-
if input.peek(token::Paren) {
251-
let content;
252-
let paren_token = parenthesized!(content in input);
253-
let mut bound: TraitBound = content.parse()?;
254-
bound.paren_token = Some(paren_token);
255-
return Ok(TypeParamBound::Trait(bound));
240+
input.parse().map(TypeParamBound::TraitSubst)
241+
} else {
242+
syn::TypeParamBound::parse(input).map(TypeParamBound::Other)
256243
}
257-
258-
input.parse().map(TypeParamBound::Trait)
259244
}
260245
}
261246

@@ -470,19 +455,15 @@ mod printing_subst {
470455
impl ToTokensSubst for TypeParamBound {
471456
fn to_tokens_subst(&self, tokens: &mut TokenStream, subst: &TokenStream) {
472457
match self {
473-
TypeParamBound::Trait(t) => t.to_tokens(tokens),
474458
TypeParamBound::TraitSubst(_) => tokens.append_all(quote! { #subst }),
475-
TypeParamBound::Lifetime(lt) => lt.to_tokens(tokens),
459+
TypeParamBound::Other(bound) => bound.to_tokens(tokens),
476460
}
477461
}
478462
}
479463
}
480464

481465
fn map_type_param_bound(bound: &syn::TypeParamBound) -> TypeParamBound {
482-
match bound {
483-
syn::TypeParamBound::Trait(bound) => TypeParamBound::Trait(bound.clone()),
484-
syn::TypeParamBound::Lifetime(bound) => TypeParamBound::Lifetime(bound.clone()),
485-
}
466+
TypeParamBound::Other(bound.clone())
486467
}
487468

488469
fn map_generic_param(param: &syn::GenericParam) -> GenericParam {

lib/src/scope.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ mod parsing {
215215
use crate::fields::parsing::data_struct;
216216
use syn::parse::{Parse, ParseStream};
217217
use syn::spanned::Spanned;
218-
use syn::{braced, bracketed, AttrStyle, Error, Field, Lifetime, Path, TypePath, WhereClause};
218+
use syn::{braced, Error, Field, Lifetime, Path, TypePath, WhereClause};
219219

220220
impl Parse for Scope {
221221
fn parse(input: ParseStream) -> Result<Self> {
@@ -371,7 +371,7 @@ mod parsing {
371371

372372
let content;
373373
let brace_token = braced!(content in input);
374-
parse_attrs_inner(&content, &mut attrs)?;
374+
attrs.extend(Attribute::parse_inner(&content)?);
375375

376376
let mut items = Vec::new();
377377
while !content.is_empty() {
@@ -415,25 +415,6 @@ mod parsing {
415415
let named = content.parse_terminated(Field::parse_named)?;
416416
Ok(FieldsNamed { brace_token, named })
417417
}
418-
419-
fn parse_attrs_inner(input: ParseStream, attrs: &mut Vec<Attribute>) -> Result<()> {
420-
while input.peek(Token![#]) && input.peek2(Token![!]) {
421-
let pound_token = input.parse()?;
422-
let style = AttrStyle::Inner(input.parse()?);
423-
let content;
424-
let bracket_token = bracketed!(content in input);
425-
let path = content.call(Path::parse_mod_style)?;
426-
let tokens = content.parse()?;
427-
attrs.push(Attribute {
428-
pound_token,
429-
style,
430-
bracket_token,
431-
path,
432-
tokens,
433-
});
434-
}
435-
Ok(())
436-
}
437418
}
438419

439420
mod printing {

lib/src/singleton.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -290,26 +290,7 @@ mod parsing {
290290
use super::*;
291291
use proc_macro_error::abort;
292292
use syn::parse::{Error, Parse, ParseStream, Result};
293-
use syn::{braced, bracketed, parenthesized};
294-
295-
fn parse_attrs_inner(input: ParseStream, attrs: &mut Vec<Attribute>) -> Result<()> {
296-
while input.peek(Token![#]) && input.peek2(Token![!]) {
297-
let pound_token = input.parse()?;
298-
let style = syn::AttrStyle::Inner(input.parse()?);
299-
let content;
300-
let bracket_token = bracketed!(content in input);
301-
let path = content.call(syn::Path::parse_mod_style)?;
302-
let tokens = content.parse()?;
303-
attrs.push(Attribute {
304-
pound_token,
305-
style,
306-
bracket_token,
307-
path,
308-
tokens,
309-
});
310-
}
311-
Ok(())
312-
}
293+
use syn::{braced, parenthesized};
313294

314295
fn parse_impl(in_ident: Option<&Ident>, input: ParseStream) -> Result<ItemImpl> {
315296
let mut attrs = input.call(Attribute::parse_outer)?;
@@ -388,7 +369,7 @@ mod parsing {
388369

389370
let content;
390371
let brace_token = braced!(content in input);
391-
parse_attrs_inner(&content, &mut attrs)?;
372+
attrs.extend(Attribute::parse_inner(&content)?);
392373

393374
let mut items = Vec::new();
394375
while !content.is_empty() {

0 commit comments

Comments
 (0)