From 0ddfcbd044aa2a35f716f8d9b18c056dd6ec46af Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Mon, 3 Aug 2026 14:46:37 +0800 Subject: refactor(picker): replace global picker functions with PickerHelper trait --- mingling/src/picker/global.rs | 72 +++++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 20 deletions(-) (limited to 'mingling/src/picker') 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(program: &mut Program, flag: &PickerArg) -> 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 where C: ProgramCollect, { - 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`], leaving the program + /// with no arguments until [`replace_args`] is called. + /// + /// [`replace_args`]: PickerHelper::replace_args + fn take_args(&mut self) -> Vec; + + /// 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) -> Vec; + + /// 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) -> 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(&mut self, arg: &PickerArg) -> Option + 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(program: &mut Program, arg: &PickerArg) -> Option +impl PickerHelper for Program where - A: for<'a> Pickable<'a> + Default, C: ProgramCollect, { - 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 { + self.take_args() + } + + fn replace_args(&mut self, args: Vec) -> Vec { + self.replace_args(args) + } } -- cgit