Make getters return iterators rather than slices

This commit is contained in:
CreepySkeleton 2020-07-06 17:33:02 +03:00
parent 51d0b31105
commit 7e8d120543
2 changed files with 13 additions and 22 deletions

View file

@ -61,11 +61,7 @@ pub trait Generator {
fn all_subcommands(app: &App) -> Vec<(String, String)> { fn all_subcommands(app: &App) -> Vec<(String, String)> {
let mut subcmds: Vec<_> = Self::subcommands(app); let mut subcmds: Vec<_> = Self::subcommands(app);
for sc_v in app for sc_v in app.get_subcommands().map(|s| Self::all_subcommands(&s)) {
.get_subcommands()
.iter()
.map(|s| Self::all_subcommands(&s))
{
subcmds.extend(sc_v); subcmds.extend(sc_v);
} }
@ -123,7 +119,6 @@ pub trait Generator {
let mut shorts: Vec<char> = p let mut shorts: Vec<char> = p
.get_arguments() .get_arguments()
.iter()
.filter_map(|a| { .filter_map(|a| {
if a.get_index().is_none() && a.get_short().is_some() { if a.get_index().is_none() && a.get_short().is_some() {
Some(a.get_short().unwrap()) Some(a.get_short().unwrap())
@ -151,7 +146,6 @@ pub trait Generator {
let mut longs: Vec<String> = p let mut longs: Vec<String> = p
.get_arguments() .get_arguments()
.iter()
.filter_map(|a| { .filter_map(|a| {
if a.get_index().is_none() && a.get_long().is_some() { if a.get_index().is_none() && a.get_long().is_some() {
Some(a.get_long().unwrap().to_string()) Some(a.get_long().unwrap().to_string())

View file

@ -133,34 +133,33 @@ impl<'b> App<'b> {
self.aliases.iter().map(|a| a.0) self.aliases.iter().map(|a| a.0)
} }
/// Get the list of subcommands /// Iterate through the set of subcommands.
#[inline] #[inline]
pub fn get_subcommands(&self) -> &[App<'b>] { pub fn get_subcommands(&self) -> impl Iterator<Item = &App<'b>> {
&self.subcommands self.subcommands.iter()
} }
/// Get the list of subcommands /// Iterate through the set of subcommands, getting a mutable reference to each.
#[inline] #[inline]
pub fn get_subcommands_mut(&mut self) -> &mut [App<'b>] { pub fn get_subcommands_mut(&mut self) -> impl Iterator<Item = &mut App<'b>> {
&mut self.subcommands self.subcommands.iter_mut()
} }
/// Get the list of arguments /// Iterate through the set of arguments
#[inline] #[inline]
pub fn get_arguments(&self) -> &[Arg<'b>] { pub fn get_arguments(&self) -> impl Iterator<Item = &Arg<'b>> {
&self.args.args self.args.args.iter()
} }
/// Get the list of *positional* arguments. /// Get the list of *positional* arguments.
#[inline] #[inline]
pub fn get_positionals(&self) -> impl Iterator<Item = &Arg<'b>> { pub fn get_positionals(&self) -> impl Iterator<Item = &Arg<'b>> {
self.get_arguments().iter().filter(|a| a.is_positional()) self.get_arguments().filter(|a| a.is_positional())
} }
/// Iterate through the *flags* that don't have custom heading. /// Iterate through the *flags* that don't have custom heading.
pub fn get_flags_no_heading(&self) -> impl Iterator<Item = &Arg<'b>> { pub fn get_flags_no_heading(&self) -> impl Iterator<Item = &Arg<'b>> {
self.get_arguments() self.get_arguments()
.iter()
.filter(|a| !a.is_set(ArgSettings::TakesValue) && a.get_index().is_none()) .filter(|a| !a.is_set(ArgSettings::TakesValue) && a.get_index().is_none())
.filter(|a| a.get_help_heading().is_none()) .filter(|a| a.get_help_heading().is_none())
} }
@ -168,7 +167,6 @@ impl<'b> App<'b> {
/// Iterate through the *options* that don't have custom heading. /// Iterate through the *options* that don't have custom heading.
pub fn get_opts_no_heading(&self) -> impl Iterator<Item = &Arg<'b>> { pub fn get_opts_no_heading(&self) -> impl Iterator<Item = &Arg<'b>> {
self.get_arguments() self.get_arguments()
.iter()
.filter(|a| a.is_set(ArgSettings::TakesValue) && a.get_index().is_none()) .filter(|a| a.is_set(ArgSettings::TakesValue) && a.get_index().is_none())
.filter(|a| a.get_help_heading().is_none()) .filter(|a| a.get_help_heading().is_none())
} }
@ -209,7 +207,7 @@ impl<'b> App<'b> {
where where
T: PartialEq<str> + ?Sized, T: PartialEq<str> + ?Sized,
{ {
self.get_subcommands().iter().find(|s| s.aliases_to(name)) self.get_subcommands().find(|s| s.aliases_to(name))
} }
} }
@ -2035,9 +2033,8 @@ impl<'b> App<'b> {
/// Iterate through all the names of all subcommands (not recursively), including aliases. /// Iterate through all the names of all subcommands (not recursively), including aliases.
/// Used for suggestions. /// Used for suggestions.
pub(crate) fn all_subcommand_names(&self) -> impl Iterator<Item = &str> { pub(crate) fn all_subcommand_names(&self) -> impl Iterator<Item = &str> {
self.get_subcommands().iter().map(|s| s.get_name()).chain( self.get_subcommands().map(|s| s.get_name()).chain(
self.get_subcommands() self.get_subcommands()
.iter()
.flat_map(|s| s.aliases.iter().map(|&(n, _)| n)), .flat_map(|s| s.aliases.iter().map(|&(n, _)| n)),
) )
} }