Skip to content

Commit

Permalink
refactor(derive): Clean up unnecessary noop parsing
Browse files Browse the repository at this point in the history
My guess is this was left over from switching to using the type based
API. The generated code for extracting values from `ArgMatches` had some
code that didn't actually do anything, like mapping over the identity
function, and wrapping a value in an `Ok` only to immediately use "?" on
it.
  • Loading branch information
tmccombs committed Jan 11, 2023
1 parent 5345d6c commit 65f9e0d
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 65f9e0d

Please sign in to comment.