diff options
author | Jeff Vander Stoep <jeffv@google.com> | 2023-03-04 06:40:23 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-03-04 06:40:23 +0000 |
commit | 4fc7e895d7a45371ce52fe26f8a1a3adf8cd3c37 (patch) | |
tree | c7515a8727ea464106b69a62c5707b0c4fb67201 | |
parent | b90d41a754fc7c21cf9ce9866f246adc4366a799 (diff) | |
parent | 22025c897e5b404cd4d54d962b6ed670808fc1ef (diff) | |
download | clap_derive-4fc7e895d7a45371ce52fe26f8a1a3adf8cd3c37.tar.gz |
Upgrade clap_derive to 4.1.8 am: c8731374ee am: 5978a27f64 am: 0dc9663657 am: 22025c897e
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/clap_derive/+/2465913
Change-Id: Ia1048eb0a123f7abc6bccf5fe43324f8d1fbb914
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | Android.bp | 2 | ||||
-rw-r--r-- | Cargo.toml | 4 | ||||
-rw-r--r-- | Cargo.toml.orig | 18 | ||||
-rw-r--r-- | METADATA | 10 | ||||
-rw-r--r-- | src/attr.rs | 2 | ||||
-rw-r--r-- | src/derives/args.rs | 76 | ||||
-rw-r--r-- | src/derives/into_app.rs | 6 | ||||
-rw-r--r-- | src/derives/subcommand.rs | 18 | ||||
-rw-r--r-- | src/derives/value_enum.rs | 2 | ||||
-rw-r--r-- | src/item.rs | 2 | ||||
-rw-r--r-- | src/utils/ty.rs | 34 |
12 files changed, 118 insertions, 58 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index faf15b2..447f5f0 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,6 +1,6 @@ { "git": { - "sha1": "bc457b179f4e4145d1bd4c61c1fddd871f8839b8" + "sha1": "f931de694a5478d7e4bd424613e6699f7d4f4e9f" }, "path_in_vcs": "clap_derive" }
\ No newline at end of file @@ -41,7 +41,7 @@ rust_proc_macro { name: "libclap_derive", crate_name: "clap_derive", cargo_env_compat: true, - cargo_pkg_version: "4.0.21", + cargo_pkg_version: "4.1.8", srcs: ["src/lib.rs"], edition: "2021", features: ["default"], @@ -11,9 +11,9 @@ [package] edition = "2021" -rust-version = "1.60.0" +rust-version = "1.64.0" name = "clap_derive" -version = "4.0.21" +version = "4.1.8" include = [ "build.rs", "src/**/*", diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 27bf1e5..a32d823 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "clap_derive" -version = "4.0.21" +version = "4.1.8" description = "Parse command line argument by defining a struct, derive crate." repository = "https://github.com/clap-rs/clap/tree/master/clap_derive" categories = ["command-line-interface", "development-tools::procedural-macro-helpers"] @@ -11,18 +11,10 @@ keywords = [ "derive", "proc_macro" ] -license = "MIT OR Apache-2.0" -edition = "2021" -rust-version = "1.60.0" # MSRV -include = [ - "build.rs", - "src/**/*", - "Cargo.toml", - "LICENSE*", - "README.md", - "benches/**/*", - "examples/**/*" -] +license.workspace = true +edition.workspace = true +rust-version.workspace = true +include.workspace = true [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] @@ -11,13 +11,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/clap_derive/clap_derive-4.0.21.crate" + value: "https://static.crates.io/crates/clap_derive/clap_derive-4.1.8.crate" } - version: "4.0.21" + version: "4.1.8" license_type: NOTICE last_upgrade_date { - year: 2022 - month: 11 - day: 30 + year: 2023 + month: 3 + day: 2 } } diff --git a/src/attr.rs b/src/attr.rs index 5ea49a1..e282a8f 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -70,7 +70,7 @@ impl ClapAttr { AttrValue::Expr(_) | AttrValue::Call(_) => { abort!( self.name, - "attribute `{}` can only accept string litersl", + "attribute `{}` can only accept string literals", self.name ) } diff --git a/src/derives/args.rs b/src/derives/args.rs index ad2a646..d3b3692 100644 --- a/src/derives/args.rs +++ b/src/derives/args.rs @@ -109,8 +109,8 @@ pub fn gen_for_struct( clippy::nursery, clippy::cargo, clippy::suspicious_else_formatting, + clippy::almost_swapped, )] - #[deny(clippy::correctness)] impl #impl_generics clap::FromArgMatches for #item_name #ty_generics #where_clause { fn from_arg_matches(__clap_arg_matches: &clap::ArgMatches) -> ::std::result::Result<Self, clap::Error> { Self::from_arg_matches_mut(&mut __clap_arg_matches.clone()) @@ -144,8 +144,8 @@ pub fn gen_for_struct( clippy::nursery, clippy::cargo, clippy::suspicious_else_formatting, + clippy::almost_swapped, )] - #[deny(clippy::correctness)] impl #impl_generics clap::Args for #item_name #ty_generics #where_clause { fn group_id() -> Option<clap::Id> { #group_id @@ -187,7 +187,7 @@ pub fn gen_augment( (Ty::Option, Some(sub_type)) => sub_type, _ => &field.ty, }; - let implicit_methods = if **ty == Ty::Option || override_required { + let implicit_methods = if **ty == Ty::Option { quote!() } else { quote_spanned! { kind.span()=> @@ -196,10 +196,20 @@ pub fn gen_augment( } }; + let override_methods = if override_required { + quote_spanned! { kind.span()=> + .subcommand_required(false) + .arg_required_else_help(false) + } + } else { + quote!() + }; + Some(quote! { let #app_var = <#subcmd_type as clap::Subcommand>::augment_subcommands( #app_var ); let #app_var = #app_var - #implicit_methods; + #implicit_methods + #override_methods; }) } Kind::Flatten(ty) => { @@ -288,8 +298,16 @@ pub fn gen_augment( } } + Ty::VecVec | Ty::OptionVecVec => { + quote_spanned! { ty.span() => + .value_name(#value_name) + #value_parser + #action + } + } + Ty::Other => { - let required = item.find_default_method().is_none() && !override_required; + let required = item.find_default_method().is_none(); // `ArgAction::takes_values` is assuming `ArgAction::default_value` will be // set though that won't always be true but this should be good enough, // otherwise we'll report an "arg required" error when unwrapping. @@ -310,6 +328,13 @@ pub fn gen_augment( } else { quote!() }; + let override_methods = if override_required { + quote_spanned! { kind.span()=> + .required(false) + } + } else { + quote!() + }; Some(quote_spanned! { field.span()=> let #app_var = #app_var.arg({ @@ -321,6 +346,10 @@ pub fn gen_augment( let arg = arg #explicit_methods; + + let arg = arg + #override_methods; + arg }); }) @@ -431,7 +460,9 @@ pub fn gen_constructor(fields: &[(&Field, Item)]) -> TokenStream { Ty::Unit | Ty::Vec | Ty::OptionOption | - Ty::OptionVec => { + Ty::OptionVec | + Ty::VecVec | + Ty::OptionVecVec => { abort!( ty.span(), "{} types are not supported for subcommand", @@ -470,7 +501,9 @@ pub fn gen_constructor(fields: &[(&Field, Item)]) -> TokenStream { Ty::Unit | Ty::Vec | Ty::OptionOption | - Ty::OptionVec => { + Ty::OptionVec | + Ty::VecVec | + Ty::OptionVecVec => { abort!( ty.span(), "{} types are not supported for flatten", @@ -609,8 +642,7 @@ fn gen_parsers( let id = item.id(); let get_one = quote_spanned!(span=> remove_one::<#convert_type>); let get_many = quote_spanned!(span=> remove_many::<#convert_type>); - let deref = quote!(|s| s); - let parse = quote_spanned!(span=> |s| ::std::result::Result::Ok::<_, clap::Error>(s)); + let get_occurrences = quote_spanned!(span=> remove_occurrences::<#convert_type>); // Give this identifier the same hygiene // as the `arg_matches` parameter definition. This @@ -627,9 +659,6 @@ fn gen_parsers( Ty::Option => { quote_spanned! { ty.span()=> #arg_matches.#get_one(#id) - .map(#deref) - .map(#parse) - .transpose()? } } @@ -637,8 +666,6 @@ fn gen_parsers( if #arg_matches.contains_id(#id) { Some( #arg_matches.#get_one(#id) - .map(#deref) - .map(#parse).transpose()? ) } else { None @@ -648,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 @@ -659,18 +685,26 @@ 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) } } + Ty::VecVec => quote_spanned! { ty.span()=> + #arg_matches.#get_occurrences(#id) + .map(|g| g.map(::std::iter::Iterator::collect).collect::<Vec<Vec<_>>>()) + .unwrap_or_else(Vec::new) + }, + + Ty::OptionVecVec => quote_spanned! { ty.span()=> + #arg_matches.#get_occurrences(#id) + .map(|g| g.map(::std::iter::Iterator::collect).collect::<Vec<Vec<_>>>()) + }, + 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)))? } } }; diff --git a/src/derives/into_app.rs b/src/derives/into_app.rs index eabdfa3..dfb676a 100644 --- a/src/derives/into_app.rs +++ b/src/derives/into_app.rs @@ -36,8 +36,8 @@ pub fn gen_for_struct(item: &Item, item_name: &Ident, generics: &Generics) -> To clippy::nursery, clippy::cargo, clippy::suspicious_else_formatting, + clippy::almost_swapped, )] - #[deny(clippy::correctness)] impl #impl_generics clap::CommandFactory for #item_name #ty_generics #where_clause { fn command<'b>() -> clap::Command { let #app_var = clap::Command::new(#name); @@ -72,8 +72,8 @@ pub fn gen_for_enum(item: &Item, item_name: &Ident, generics: &Generics) -> Toke clippy::nursery, clippy::cargo, clippy::suspicious_else_formatting, + clippy::almost_swapped, )] - #[deny(clippy::correctness)] impl #impl_generics clap::CommandFactory for #item_name #ty_generics #where_clause { fn command<'b>() -> clap::Command { let #app_var = clap::Command::new(#name) @@ -85,6 +85,8 @@ pub fn gen_for_enum(item: &Item, item_name: &Ident, generics: &Generics) -> Toke fn command_for_update<'b>() -> clap::Command { let #app_var = clap::Command::new(#name); <Self as clap::Subcommand>::augment_subcommands_for_update(#app_var) + .subcommand_required(false) + .arg_required_else_help(false) } } } diff --git a/src/derives/subcommand.rs b/src/derives/subcommand.rs index 009684b..ffe22ec 100644 --- a/src/derives/subcommand.rs +++ b/src/derives/subcommand.rs @@ -80,8 +80,8 @@ pub fn gen_for_enum( clippy::nursery, clippy::cargo, clippy::suspicious_else_formatting, + clippy::almost_swapped, )] - #[deny(clippy::correctness)] impl #impl_generics clap::FromArgMatches for #item_name #ty_generics #where_clause { fn from_arg_matches(__clap_arg_matches: &clap::ArgMatches) -> ::std::result::Result<Self, clap::Error> { Self::from_arg_matches_mut(&mut __clap_arg_matches.clone()) @@ -106,8 +106,8 @@ pub fn gen_for_enum( clippy::nursery, clippy::cargo, clippy::suspicious_else_formatting, + clippy::almost_swapped, )] - #[deny(clippy::correctness)] impl #impl_generics clap::Subcommand for #item_name #ty_generics #where_clause { fn augment_subcommands <'b>(__clap_app: clap::Command) -> clap::Command { #augmentation @@ -244,16 +244,24 @@ fn gen_augment( }; let initial_app_methods = item.initial_top_level_methods(); let final_from_attrs = item.final_top_level_methods(); + let override_methods = if override_required { + quote_spanned! { kind.span()=> + .subcommand_required(false) + .arg_required_else_help(false) + } + } else { + quote!() + }; let subcommand = quote! { let #app_var = #app_var.subcommand({ #deprecations; let #subcommand_var = clap::Command::new(#name); - let #subcommand_var = #subcommand_var #initial_app_methods; - let #subcommand_var = #arg_block; let #subcommand_var = #subcommand_var .subcommand_required(true) .arg_required_else_help(true); - #subcommand_var #final_from_attrs + let #subcommand_var = #subcommand_var #initial_app_methods; + let #subcommand_var = #arg_block; + #subcommand_var #final_from_attrs #override_methods }); }; Some(subcommand) diff --git a/src/derives/value_enum.rs b/src/derives/value_enum.rs index a1411d0..7a9d870 100644 --- a/src/derives/value_enum.rs +++ b/src/derives/value_enum.rs @@ -65,8 +65,8 @@ pub fn gen_for_enum(item: &Item, item_name: &Ident, variants: &[(&Variant, Item) clippy::nursery, clippy::cargo, clippy::suspicious_else_formatting, + clippy::almost_swapped, )] - #[deny(clippy::correctness)] impl clap::ValueEnum for #item_name { #value_variants #to_possible_value diff --git a/src/item.rs b/src/item.rs index a068e36..5e8272a 100644 --- a/src/item.rs +++ b/src/item.rs @@ -1121,7 +1121,7 @@ impl Action { fn default_action(field_type: &Type, span: Span) -> Method { let ty = Ty::from_syn_ty(field_type); let args = match *ty { - Ty::Vec | Ty::OptionVec => { + Ty::Vec | Ty::OptionVec | Ty::VecVec | Ty::OptionVecVec => { quote_spanned! { span=> clap::ArgAction::Append } diff --git a/src/utils/ty.rs b/src/utils/ty.rs index 1cf0514..9349bc2 100644 --- a/src/utils/ty.rs +++ b/src/utils/ty.rs @@ -11,9 +11,11 @@ use syn::{ pub enum Ty { Unit, Vec, + VecVec, Option, OptionOption, OptionVec, + OptionVecVec, Other, } @@ -24,13 +26,13 @@ impl Ty { if is_unit_ty(ty) { t(Unit) - } else if is_generic_ty(ty, "Vec") { - t(Vec) + } else if let Some(vt) = get_vec_ty(ty, Vec, VecVec) { + t(vt) } else if let Some(subty) = subty_if_name(ty, "Option") { if is_generic_ty(subty, "Option") { t(OptionOption) - } else if is_generic_ty(subty, "Vec") { - t(OptionVec) + } else if let Some(vt) = get_vec_ty(subty, OptionVec, OptionVecVec) { + t(vt) } else { t(Option) } @@ -46,6 +48,8 @@ impl Ty { Self::Option => "Option<T>", Self::OptionOption => "Option<Option<T>>", Self::OptionVec => "Option<Vec<T>>", + Self::VecVec => "Vec<Vec<T>>", + Self::OptionVecVec => "Option<Vec<Vec<T>>>", Self::Other => "...other...", } } @@ -55,9 +59,13 @@ pub fn inner_type(field_ty: &syn::Type) -> &syn::Type { let ty = Ty::from_syn_ty(field_ty); match *ty { Ty::Vec | Ty::Option => sub_type(field_ty).unwrap_or(field_ty), - Ty::OptionOption | Ty::OptionVec => { + Ty::OptionOption | Ty::OptionVec | Ty::VecVec => { sub_type(field_ty).and_then(sub_type).unwrap_or(field_ty) } + Ty::OptionVecVec => sub_type(field_ty) + .and_then(sub_type) + .and_then(sub_type) + .unwrap_or(field_ty), _ => field_ty, } } @@ -139,3 +147,19 @@ where { iter.next().filter(|_| iter.next().is_none()) } + +#[cfg(feature = "unstable-v5")] +fn get_vec_ty(ty: &Type, vec_ty: Ty, vecvec_ty: Ty) -> Option<Ty> { + subty_if_name(ty, "Vec").map(|subty| { + if is_generic_ty(subty, "Vec") { + vecvec_ty + } else { + vec_ty + } + }) +} + +#[cfg(not(feature = "unstable-v5"))] +fn get_vec_ty(ty: &Type, vec_ty: Ty, _vecvec_ty: Ty) -> Option<Ty> { + is_generic_ty(ty, "Vec").then_some(vec_ty) +} |