Skip to content

Commit

Permalink
Merge pull request #4627 from tmccombs/macro-cleanup
Browse files Browse the repository at this point in the history
refactor(derive): Clean up unnecessary noop parsing
  • Loading branch information
epage authored Jan 11, 2023
2 parents 5345d6c + 65f9e0d commit 37d03bb
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions clap_derive/src/derives/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,6 @@ fn gen_parsers(
let get_one = quote_spanned!(span=> remove_one::<#convert_type>);
let get_many = quote_spanned!(span=> remove_many::<#convert_type>);
let get_occurrences = quote_spanned!(span=> remove_occurrences::<#convert_type>);
let deref = quote!(|s| s);
let parse = quote_spanned!(span=> |s| ::std::result::Result::Ok::<_, clap::Error>(s));

// Give this identifier the same hygiene
// as the `arg_matches` parameter definition. This
Expand All @@ -661,18 +659,13 @@ fn gen_parsers(
Ty::Option => {
quote_spanned! { ty.span()=>
#arg_matches.#get_one(#id)
.map(#deref)
.map(#parse)
.transpose()?
}
}

Ty::OptionOption => quote_spanned! { ty.span()=>
if #arg_matches.contains_id(#id) {
Some(
#arg_matches.#get_one(#id)
.map(#deref)
.map(#parse).transpose()?
)
} else {
None
Expand All @@ -682,8 +675,7 @@ fn gen_parsers(
Ty::OptionVec => quote_spanned! { ty.span()=>
if #arg_matches.contains_id(#id) {
Some(#arg_matches.#get_many(#id)
.map(|v| v.map(#deref).map::<::std::result::Result<#convert_type, clap::Error>, _>(#parse).collect::<::std::result::Result<Vec<_>, clap::Error>>())
.transpose()?
.map(|v| v.collect::<Vec<_>>())
.unwrap_or_else(Vec::new))
} else {
None
Expand All @@ -693,8 +685,7 @@ fn gen_parsers(
Ty::Vec => {
quote_spanned! { ty.span()=>
#arg_matches.#get_many(#id)
.map(|v| v.map(#deref).map::<::std::result::Result<#convert_type, clap::Error>, _>(#parse).collect::<::std::result::Result<Vec<_>, clap::Error>>())
.transpose()?
.map(|v| v.collect::<Vec<_>>())
.unwrap_or_else(Vec::new)
}
}
Expand All @@ -713,9 +704,7 @@ fn gen_parsers(
Ty::Other => {
quote_spanned! { ty.span()=>
#arg_matches.#get_one(#id)
.map(#deref)
.ok_or_else(|| clap::Error::raw(clap::error::ErrorKind::MissingRequiredArgument, format!("The following required argument was not provided: {}", #id)))
.and_then(#parse)?
.ok_or_else(|| clap::Error::raw(clap::error::ErrorKind::MissingRequiredArgument, format!("The following required argument was not provided: {}", #id)))?
}
}
};
Expand Down

0 comments on commit 37d03bb

Please sign in to comment.