diff options
Diffstat (limited to 'mingling/src/picker/global.rs')
| -rw-r--r-- | mingling/src/picker/global.rs | 72 |
1 files changed, 52 insertions, 20 deletions
diff --git a/mingling/src/picker/global.rs b/mingling/src/picker/global.rs index c203524..f062671 100644 --- a/mingling/src/picker/global.rs +++ b/mingling/src/picker/global.rs @@ -3,33 +3,65 @@ use mingling_core::{Program, ProgramCollect}; use crate::consts::REMAINS; -/// Picks a global flag from the program's arguments. +/// Provides helper methods for picking arguments from a [`Program`]'s argument list. /// -/// This function takes ownership of the program's current arguments, picks the specified `flag` -/// from them, and then returns the remaining arguments back to the program. It returns the -/// boolean value of the flag. -pub fn pick_global_flag<C>(program: &mut Program<C>, flag: &PickerArg<Flag>) -> bool +/// This trait abstracts the functionality of extracting specific arguments or flags +/// from the program's current arguments, while restoring any remaining arguments +/// back into the program. +pub trait PickerHelper<C> where C: ProgramCollect<Enum = C>, { - let args = program.take_args(); - let (flag, args) = args.pick(flag).pick(&REMAINS).unwrap(); - program.replace_args(args.into()); - *flag + /// Takes ownership of the program's current arguments. + /// + /// Returns the program's argument list as a [`Vec<String>`], leaving the program + /// with no arguments until [`replace_args`] is called. + /// + /// [`replace_args`]: PickerHelper::replace_args + fn take_args(&mut self) -> Vec<String>; + + /// Replaces the program's current arguments with the provided list. + /// + /// Returns the previous argument list that was replaced. + fn replace_args(&mut self, args: Vec<String>) -> Vec<String>; + + /// Picks a flag from the program's arguments. + /// + /// This function takes ownership of the program's current arguments, picks the specified `flag` + /// from them, and then returns the remaining arguments back to the program. It returns the + /// boolean value of the flag. + fn pick_flag(&mut self, flag: &PickerArg<Flag>) -> bool { + let args = self.take_args(); + let (flag, args) = args.pick(flag).pick(&REMAINS).unwrap(); + self.replace_args(args.into()); + *flag + } + + /// Picks a argument from the program's arguments. + /// + /// This function takes ownership of the program's current arguments, picks the specified `arg` + /// from them, and then returns the remaining arguments back to the program. It returns the + /// picked argument value, or `None` if the argument was not present. + fn pick_argument<A>(&mut self, arg: &PickerArg<A>) -> Option<A> + where + A: for<'a> Pickable<'a> + Default, + { + let args = self.take_args(); + let (arg, remains) = args.pick(arg).pick(&REMAINS).unpack(); + self.replace_args(remains.unwrap().into()); + arg + } } -/// Picks a global argument from the program's arguments. -/// -/// This function takes ownership of the program's current arguments, picks the specified `arg` -/// from them, and then returns the remaining arguments back to the program. It returns the -/// picked argument value, or `None` if the argument was not present. -pub fn pick_global_argument<C, A>(program: &mut Program<C>, arg: &PickerArg<A>) -> Option<A> +impl<C> PickerHelper<C> for Program<C> where - A: for<'a> Pickable<'a> + Default, C: ProgramCollect<Enum = C>, { - let args = program.take_args(); - let (arg, remains) = args.pick(arg).pick(&REMAINS).unpack(); - program.replace_args(remains.unwrap().into()); - arg + fn take_args(&mut self) -> Vec<String> { + self.take_args() + } + + fn replace_args(&mut self, args: Vec<String>) -> Vec<String> { + self.replace_args(args) + } } |
