Skip to content

Commit

Permalink
chore: Put Vec<Vec<_>> behave for derive behind unstable-v5 flag
Browse files Browse the repository at this point in the history
  • Loading branch information
tmccombs committed Jan 8, 2023
1 parent ccce3c3 commit 73df3d1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
32 changes: 20 additions & 12 deletions clap_derive/src/utils/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,13 @@ impl Ty {

if is_unit_ty(ty) {
t(Unit)
} else if let Some(subty) = subty_if_name(ty, "Vec") {
if is_generic_ty(subty, "Vec") {
t(VecVec)
} else {
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 let Some(subty) = subty_if_name(subty, "Vec") {
if is_generic_ty(subty, "Vec") {
t(OptionVecVec)
} else {
t(OptionVec)
}
} else if let Some(vt) = get_vec_ty(subty, OptionVec, OptionVecVec) {
t(vt)
} else {
t(Option)
}
Expand Down Expand Up @@ -155,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(|| vec_ty)
}
12 changes: 5 additions & 7 deletions tests/derive/occurrences.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(feature = "unstable-grouped")]
#![cfg(all(feature = "unstable-grouped", feature = "unstable-v5"))]
use clap::Parser;

#[test]
Expand All @@ -19,23 +19,21 @@ fn test_vec_of_vec() {

#[test]
fn test_vec_of_vec_opt_out() {
fn parser(s: &str) -> Result<Vec<Vec<String>>, std::convert::Infallible> {
Ok(s.split(':')
.map(|v| v.split(',').map(str::to_owned).collect())
.collect())
fn parser(s: &str) -> Result<Vec<String>, std::convert::Infallible> {
Ok(s.split(',').map(str::to_owned).collect())
}

#[derive(Parser, PartialEq, Debug)]
struct Opt {
#[arg(value_parser = parser, short = 'p')]
arg: ::std::vec::Vec<Vec<String>>,
arg: Vec<::std::vec::Vec<String>>,
}

assert_eq!(
Opt {
arg: vec![vec!["1".into(), "2".into()], vec!["a".into(), "b".into()]],
},
Opt::try_parse_from(["test", "-p", "1,2:a,b"]).unwrap(),
Opt::try_parse_from(["test", "-p", "1,2", "-p", "a,b"]).unwrap(),
);
}

Expand Down

0 comments on commit 73df3d1

Please sign in to comment.