Skip to content

Commit

Permalink
One less hack
Browse files Browse the repository at this point in the history
  • Loading branch information
CreepySkeleton committed Jan 20, 2020
1 parent b636ac8 commit 3bbf4a6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 33 deletions.
31 changes: 2 additions & 29 deletions structopt-derive/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StructOptAttr, Token![,]>,
}

impl Parse for StructOptAttributes {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
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),
Expand Down Expand Up @@ -291,19 +275,8 @@ pub fn parse_structopt_attributes(all_attrs: &[Attribute]) -> Vec<StructOptAttr>
.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::<StructOptAttr, Token![,]>::parse_terminated)
.unwrap_or_abort()
})
.collect()
}
6 changes: 3 additions & 3 deletions tests/ui/structopt_empty_attr.stderr
Original file line number Diff line number Diff line change
@@ -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]
| ^^^^^^^^^
| ^
2 changes: 1 addition & 1 deletion tests/ui/structopt_name_value_attr.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: expected parentheses
error: expected parentheses: #[structopt(...)]
--> $DIR/structopt_name_value_attr.rs:14:17
|
14 | #[structopt = "short"]
Expand Down

0 comments on commit 3bbf4a6

Please sign in to comment.