From 96884aa1b2820dd7ec3fd0c8b5cb5827f1e61544 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Sat, 11 Mar 2017 12:38:24 -0500 Subject: [PATCH] refactor: implements PartialEq for some of the base structs --- src/app/parser.rs | 7 +++---- src/app/usage.rs | 9 +++++---- src/args/arg.rs | 6 ++++++ src/args/arg_builder/base.rs | 6 ++++++ src/args/arg_builder/flag.rs | 6 ++++++ src/args/arg_builder/option.rs | 6 ++++++ src/args/arg_builder/positional.rs | 6 ++++++ 7 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/app/parser.rs b/src/app/parser.rs index 1288f7e1..751ec189 100644 --- a/src/app/parser.rs +++ b/src/app/parser.rs @@ -1168,13 +1168,12 @@ impl<'a, 'b> Parser<'a, 'b> args.extend(self.arg_names_in_group(n)); g_vec.push(*n); } else { - args.push(*n); + if !args.contains(n) { + args.push(*n); + } } } - // TODO: faster way to sort/dedup? - args.sort(); - args.dedup(); args.iter().map(|s| *s).collect() } diff --git a/src/app/usage.rs b/src/app/usage.rs index 19938b40..b083ef4f 100644 --- a/src/app/usage.rs +++ b/src/app/usage.rs @@ -421,13 +421,14 @@ pub fn get_required_usage_from<'a, 'b>(p: &Parser<'a, 'b>, }); ret_val.push_back(arg); } - let mut g_vec = vec![]; + let mut g_vec: Vec = vec![]; for g in desc_reqs.iter().filter(|n| p.groups.iter().any(|g| &&g.name == n)) { let g_string = p.args_in_group(g).join("|"); - g_vec.push(format!("<{}>", &g_string[..g_string.len()])); + let elem = format!("<{}>", &g_string[..g_string.len()]); + if !g_vec.contains(&elem) { + g_vec.push(elem); + } } - g_vec.sort(); - g_vec.dedup(); for g in g_vec { ret_val.push_back(g); } diff --git a/src/args/arg.rs b/src/args/arg.rs index 2fd93a66..8e3734a0 100644 --- a/src/args/arg.rs +++ b/src/args/arg.rs @@ -3327,3 +3327,9 @@ impl<'a, 'b, 'z> From<&'z Arg<'a, 'b>> for Arg<'a, 'b> { } } } + +impl<'n, 'e> PartialEq for Arg<'n, 'e> { + fn eq(&self, other: &Arg<'n, 'e>) -> bool { + self.b == other.b + } +} \ No newline at end of file diff --git a/src/args/arg_builder/base.rs b/src/args/arg_builder/base.rs index 1b5e534c..8b566e38 100644 --- a/src/args/arg_builder/base.rs +++ b/src/args/arg_builder/base.rs @@ -26,3 +26,9 @@ impl<'n, 'e> Base<'n, 'e> { impl<'n, 'e, 'z> From<&'z Arg<'n, 'e>> for Base<'n, 'e> { fn from(a: &'z Arg<'n, 'e>) -> Self { a.b.clone() } } + +impl<'n, 'e> PartialEq for Base<'n, 'e> { + fn eq(&self, other: &Base<'n, 'e>) -> bool { + self.name == other.name + } +} \ No newline at end of file diff --git a/src/args/arg_builder/flag.rs b/src/args/arg_builder/flag.rs index 6dc37243..d1c16690 100644 --- a/src/args/arg_builder/flag.rs +++ b/src/args/arg_builder/flag.rs @@ -105,6 +105,12 @@ impl<'n, 'e> DispOrder for FlagBuilder<'n, 'e> { fn disp_ord(&self) -> usize { self.s.disp_ord } } +impl<'n, 'e> PartialEq for FlagBuilder<'n, 'e> { + fn eq(&self, other: &FlagBuilder<'n, 'e>) -> bool { + self.b == other.b + } +} + #[cfg(test)] mod test { use args::settings::ArgSettings; diff --git a/src/args/arg_builder/option.rs b/src/args/arg_builder/option.rs index 75f14a73..91c2ee6f 100644 --- a/src/args/arg_builder/option.rs +++ b/src/args/arg_builder/option.rs @@ -149,6 +149,12 @@ impl<'n, 'e> DispOrder for OptBuilder<'n, 'e> { fn disp_ord(&self) -> usize { self.s.disp_ord } } +impl<'n, 'e> PartialEq for OptBuilder<'n, 'e> { + fn eq(&self, other: &OptBuilder<'n, 'e>) -> bool { + self.b == other.b + } +} + #[cfg(test)] mod test { use args::settings::ArgSettings; diff --git a/src/args/arg_builder/positional.rs b/src/args/arg_builder/positional.rs index 73fde4c1..ddbd8964 100644 --- a/src/args/arg_builder/positional.rs +++ b/src/args/arg_builder/positional.rs @@ -138,6 +138,12 @@ impl<'n, 'e> DispOrder for PosBuilder<'n, 'e> { fn disp_ord(&self) -> usize { self.index as usize } } +impl<'n, 'e> PartialEq for PosBuilder<'n, 'e> { + fn eq(&self, other: &PosBuilder<'n, 'e>) -> bool { + self.b == other.b + } +} + #[cfg(test)] mod test { use args::settings::ArgSettings;