From 3bbf4a6678cacf7b4fa7880194f40a93d403fa64 Mon Sep 17 00:00:00 2001 From: CreepySkeleton Date: Tue, 21 Jan 2020 00:19:37 +0300 Subject: [PATCH] One less hack --- structopt-derive/src/parse.rs | 31 ++--------------------- tests/ui/structopt_empty_attr.stderr | 6 ++--- tests/ui/structopt_name_value_attr.stderr | 2 +- 3 files changed, 6 insertions(+), 33 deletions(-) diff --git a/structopt-derive/src/parse.rs b/structopt-derive/src/parse.rs index fc422035..118c568c 100644 --- a/structopt-derive/src/parse.rs +++ b/structopt-derive/src/parse.rs @@ -5,27 +5,11 @@ use quote::ToTokens; use syn::{ self, parenthesized, parse::{Parse, ParseBuffer, ParseStream}, - parse2, punctuated::Punctuated, spanned::Spanned, Attribute, Expr, ExprLit, Ident, Lit, LitBool, LitStr, Token, }; -pub struct StructOptAttributes { - pub paren_token: syn::token::Paren, - pub attrs: Punctuated, -} - -impl Parse for StructOptAttributes { - fn parse(input: ParseStream<'_>) -> syn::Result { - let content; - let paren_token = parenthesized!(content in input); - let attrs = content.parse_terminated(StructOptAttr::parse)?; - - Ok(StructOptAttributes { paren_token, attrs }) - } -} - pub enum StructOptAttr { // single-identifier attributes Short(Ident), @@ -291,19 +275,8 @@ pub fn parse_structopt_attributes(all_attrs: &[Attribute]) -> Vec .iter() .filter(|attr| attr.path.is_ident("structopt")) .flat_map(|attr| { - let attrs: StructOptAttributes = parse2(attr.tokens.clone()) - .map_err(|e| match &*e.to_string() { - // this error message is misleading and points to Span::call_site() - // so we patch it with something meaningful - "unexpected end of input, expected parentheses" => { - let span = attr.path.span(); - let patch_msg = "expected parentheses after `structopt`"; - syn::Error::new(span, patch_msg) - } - _ => e, - }) - .unwrap_or_abort(); - attrs.attrs + attr.parse_args_with(Punctuated::::parse_terminated) + .unwrap_or_abort() }) .collect() } diff --git a/tests/ui/structopt_empty_attr.stderr b/tests/ui/structopt_empty_attr.stderr index dde36309..181e88c0 100644 --- a/tests/ui/structopt_empty_attr.stderr +++ b/tests/ui/structopt_empty_attr.stderr @@ -1,5 +1,5 @@ -error: expected parentheses after `structopt` - --> $DIR/structopt_empty_attr.rs:14:7 +error: expected attribute arguments in parentheses: #[structopt(...)] + --> $DIR/structopt_empty_attr.rs:14:5 | 14 | #[structopt] - | ^^^^^^^^^ + | ^ diff --git a/tests/ui/structopt_name_value_attr.stderr b/tests/ui/structopt_name_value_attr.stderr index f681978d..373a3b8e 100644 --- a/tests/ui/structopt_name_value_attr.stderr +++ b/tests/ui/structopt_name_value_attr.stderr @@ -1,4 +1,4 @@ -error: expected parentheses +error: expected parentheses: #[structopt(...)] --> $DIR/structopt_name_value_attr.rs:14:17 | 14 | #[structopt = "short"]