From 53fc28520c1314cd1bd223628c30d6a4423756a1 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 15 Jul 2026 16:36:05 +0800 Subject: feat(picker): add early route on missing flag Introduce `or_route` and a `PickerResult` type that carries an optional route value. When a flag is absent, the closure given to `or_route` is evaluated and stored in `error_route`, allowing the caller to exit early without a default value. --- mingling_picker/src/picker/parse.rs | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'mingling_picker/src/picker/parse.rs') diff --git a/mingling_picker/src/picker/parse.rs b/mingling_picker/src/picker/parse.rs index ab72206..f9a59d7 100644 --- a/mingling_picker/src/picker/parse.rs +++ b/mingling_picker/src/picker/parse.rs @@ -14,14 +14,15 @@ use mingling_picker_macros::internal_repeat; internal_repeat!(1..=32 => { use crate::PickerPattern$; + use crate::PickerResult$; }); internal_repeat!(1..=32 => { - impl<'a, (T$,+)> PickerPattern$<'a, (T$,+)> + impl<'a, (T$,+), Route> PickerPattern$<'a, (T$,+), Route> where (T$: Pickable<'a>,+) { #[allow(clippy::type_complexity)] - pub fn parse(mut self) -> PickerArgResult<((T$,+))> { + pub fn parse(mut self) -> PickerResult$<(T$,+), Route> { // ArgInfos let arg_infos: [PickerArgInfo; $] = [ ( @@ -35,10 +36,10 @@ internal_repeat!(1..=32 => { PickerFlagAttr, // Tag Func - Option) -> Vec>>, + Box) -> Vec>, // Pick Func - Option>, + Box)>, // Index usize @@ -50,16 +51,16 @@ internal_repeat!(1..=32 => { T$::get_attr(self.flag_$), // Tag Func - Some(Box::new(|args| { + Box::new(|args| { let ctx = TagPhaseContext { arg_info: &arg_infos[$], args, }; T$::tag(ctx) - })), + }), // Pick Func - Some(Box::new(|args| { + Box::new(|args, error_route| { self.result_$ = match T$::pick(args) { PickerArgResult::Parsed(mut value) => { // Postprocess @@ -79,12 +80,17 @@ internal_repeat!(1..=32 => { PickerArgResult::Parsed(value) } else { + if error_route.is_none() { + if let Some(get_route) = self.route_$ { + *error_route = Some(get_route()); + } + } other } }, } - })), + }), // Index $ @@ -99,7 +105,7 @@ internal_repeat!(1..=32 => { for (_, tag_func, pick_func, _idx) in bundle { // Tag phase - let tagged = (tag_func.unwrap())(&self.args); + let tagged = tag_func(&self.args); let mut args_to_pick: Vec<&str> = vec![]; for i in tagged { @@ -108,10 +114,17 @@ internal_repeat!(1..=32 => { } // Pick phase - (pick_func.unwrap())(args_to_pick.as_slice()); + pick_func(args_to_pick.as_slice(), &mut self.error_route); } - + // Combine Result + let result: PickerResult$<(T$,+), Route> = PickerResult$ { + route: self.error_route, + ( + v$: self.result_$.to_option(), + +) + }; + result } } }); -- cgit