From 2b7fd731aed81ffde284ff012ee46ee8753bb87c Mon Sep 17 00:00:00 2001 From: Donough Liu Date: Sun, 27 Dec 2020 01:18:43 +0800 Subject: [PATCH] Private keys --- src/mkeymap.rs | 11 ++++++++--- src/parse/features/suggestions.rs | 2 +- src/parse/parser.rs | 32 ++++++++++++------------------- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/mkeymap.rs b/src/mkeymap.rs index df770c0b..5ebd4eb9 100644 --- a/src/mkeymap.rs +++ b/src/mkeymap.rs @@ -1,11 +1,11 @@ use crate::{build::Arg, util::Id, INTERNAL_ERROR_MSG}; -use std::{ffi::OsString, ops::Index}; +use std::{ffi::OsString, ops::Index, iter::Iterator}; #[derive(PartialEq, Debug, Clone)] pub(crate) struct Key { - pub(crate) key: KeyType, - pub(crate) index: usize, + key: KeyType, + index: usize, } #[derive(Default, PartialEq, Debug, Clone)] @@ -97,6 +97,11 @@ impl<'help> MKeyMap<'help> { self.args.is_empty() } + /// Return iterators of all keys. + pub(crate) fn keys<'key>(&'key self) -> impl Iterator { + self.keys.iter().map(|x| &x.key) + } + pub(crate) fn _build(&mut self) { for (i, arg) in self.args.iter_mut().enumerate() { for k in _get_keys(arg) { diff --git a/src/parse/features/suggestions.rs b/src/parse/features/suggestions.rs index 5b2ae21f..b690ec63 100644 --- a/src/parse/features/suggestions.rs +++ b/src/parse/features/suggestions.rs @@ -52,7 +52,7 @@ where .filter_map(|subcommand| { subcommand._build(); - let longs = subcommand.args.keys.iter().map(|x| &x.key).filter_map(|a| { + let longs = subcommand.args.keys().filter_map(|a| { if let KeyType::Long(v) = a { Some(v.to_string_lossy().into_owned()) } else { diff --git a/src/parse/parser.rs b/src/parse/parser.rs index 55835b73..a0456809 100644 --- a/src/parse/parser.rs +++ b/src/parse/parser.rs @@ -122,11 +122,10 @@ impl<'help, 'app> Parser<'help, 'app> { let highest_idx = self .app .args - .keys - .iter() + .keys() .filter_map(|x| { - if let KeyType::Position(n) = x.key { - Some(n) + if let KeyType::Position(n) = x { + Some(*n) } else { None } @@ -139,9 +138,7 @@ impl<'help, 'app> Parser<'help, 'app> { let num_p = self .app .args - .keys - .iter() - .map(|x| &x.key) + .keys() .filter(|x| x.is_position()) .count(); @@ -334,9 +331,8 @@ impl<'help, 'app> Parser<'help, 'app> { let positional_count = self .app .args - .keys - .iter() - .filter(|x| x.key.is_position()) + .keys() + .filter(|x| x.is_position()) .count(); while let Some((arg_os, remaining_args)) = it.next() { @@ -542,9 +538,8 @@ impl<'help, 'app> Parser<'help, 'app> { pos_counter = self .app .args - .keys - .iter() - .filter(|x| x.key.is_position()) + .keys() + .filter(|x| x.is_position()) .count(); } @@ -570,9 +565,8 @@ impl<'help, 'app> Parser<'help, 'app> { == self .app .args - .keys - .iter() - .filter(|x| x.key.is_position()) + .keys() + .filter(|x| x.is_position()) .count()) { self.app.settings.set(AS::TrailingValues); @@ -1621,9 +1615,7 @@ impl<'help, 'app> Parser<'help, 'app> { let longs = self .app .args - .keys - .iter() - .map(|x| &x.key) + .keys() .filter_map(|x| match x { KeyType::Long(l) => Some(l.to_string_lossy().into_owned()), _ => None, @@ -1726,7 +1718,7 @@ impl<'help, 'app> Parser<'help, 'app> { } pub(crate) fn has_positionals(&self) -> bool { - self.app.args.keys.iter().any(|x| x.key.is_position()) + self.app.args.keys().any(|x| x.is_position()) } pub(crate) fn has_subcommands(&self) -> bool {