From a7a116110a80dee887ee2d9a2660fbff5e0cbc62 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 15 Jul 2026 16:08:16 +0800 Subject: feat(picker): add Route generic parameter for error routing --- mingling_picker/src/picker/parse.rs | 40 ++++++++++++++++------------------ mingling_picker/src/picker/patterns.rs | 15 ++++++++----- 2 files changed, 28 insertions(+), 27 deletions(-) (limited to 'mingling_picker/src/picker') diff --git a/mingling_picker/src/picker/parse.rs b/mingling_picker/src/picker/parse.rs index 8a55e03..ab72206 100644 --- a/mingling_picker/src/picker/parse.rs +++ b/mingling_picker/src/picker/parse.rs @@ -7,7 +7,9 @@ // P.S. If there's a better way, please let me know. Thanks! // -------------------------------------------------------------------------------------------- -use crate::{Pickable, PickerArgInfo, PickerArgs, PickerFlagAttr, PickerResult, TagPhaseContext}; +use crate::{ + Pickable, PickerArgInfo, PickerArgResult, PickerArgs, PickerFlagAttr, TagPhaseContext, +}; use mingling_picker_macros::internal_repeat; internal_repeat!(1..=32 => { @@ -19,7 +21,7 @@ internal_repeat!(1..=32 => { where (T$: Pickable<'a>,+) { #[allow(clippy::type_complexity)] - pub fn parse(mut self) -> PickerResult<((T$,+))> { + pub fn parse(mut self) -> PickerArgResult<((T$,+))> { // ArgInfos let arg_infos: [PickerArgInfo; $] = [ ( @@ -27,17 +29,16 @@ internal_repeat!(1..=32 => { +) ]; - // Read & sort attrs - let mut attrs: [ + let mut bundle: [ ( // Flag Attr PickerFlagAttr, // Tag Func - Box) -> Vec>, + Option) -> Vec>>, // Pick Func - Box, + Option>, // Index usize @@ -49,23 +50,23 @@ internal_repeat!(1..=32 => { T$::get_attr(self.flag_$), // Tag Func - Box::new(|args| { + Some(Box::new(|args| { let ctx = TagPhaseContext { arg_info: &arg_infos[$], args, }; T$::tag(ctx) - }), + })), // Pick Func - Box::new(|args| { + Some(Box::new(|args| { self.result_$ = match T$::pick(args) { - PickerResult::Parsed(mut value) => { + PickerArgResult::Parsed(mut value) => { // Postprocess if let Some(post) = self.post_$ { value = post(value); } - PickerResult::Parsed(value) + PickerArgResult::Parsed(value) }, other => { if let Some(get_default) = self.default_$ { @@ -76,14 +77,14 @@ internal_repeat!(1..=32 => { value = post(value); } - PickerResult::Parsed(value) + PickerArgResult::Parsed(value) } else { other } }, } - }), + })), // Index $ @@ -91,14 +92,14 @@ internal_repeat!(1..=32 => { +) ]; - // Sort by Attr Ord (descending) - attrs.sort_by(|a, b| b.0.cmp(&a.0)); + // Sort by Bundle Ord (descending) + bundle.sort_by(|a, b| b.0.cmp(&a.0)); // Parsing - for (_, tag_func, pick_func, _idx) in attrs { + for (_, tag_func, pick_func, _idx) in bundle { // Tag phase - let tagged = tag_func(&self.args); + let tagged = (tag_func.unwrap())(&self.args); let mut args_to_pick: Vec<&str> = vec![]; for i in tagged { @@ -107,13 +108,10 @@ internal_repeat!(1..=32 => { } // Pick phase - pick_func(args_to_pick.as_slice()); + (pick_func.unwrap())(args_to_pick.as_slice()); } - // Sort by Index Ord - attrs.sort_by(|a, b| a.3.cmp(&b.3)); - PickerResult::NotFound } } }); diff --git a/mingling_picker/src/picker/patterns.rs b/mingling_picker/src/picker/patterns.rs index b828849..d8a2797 100644 --- a/mingling_picker/src/picker/patterns.rs +++ b/mingling_picker/src/picker/patterns.rs @@ -4,10 +4,11 @@ use crate::{Pickable, Picker, PickerArgResult, PickerArgs, PickerFlag}; internal_repeat!(1..=32 => { #[doc(hidden)] - pub struct PickerPattern$<'a, (T$,+)> + pub struct PickerPattern$<'a, (T$,+), Route> where (T$: Pickable<'a>,+) { pub args: PickerArgs<'a>, + pub error_route: Option, ( pub flag_$: &'a PickerFlag<'a, T$>, pub result_$: PickerArgResult, @@ -18,7 +19,7 @@ internal_repeat!(1..=32 => { }); internal_repeat!(1..=32 => { - impl<'a, (T$,+)> PickerPattern$<'a, (T$,+)> + impl<'a, (T$,+), Route> PickerPattern$<'a, (T$,+), Route> where (T$: Pickable<'a>,+) { /// Sets a default value provider for this flag. @@ -90,7 +91,7 @@ internal_repeat!(1..=32 => { }); 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)] @@ -99,13 +100,14 @@ internal_repeat!(1..32 => { /// This method extends the current picking pattern by appending an additional flag. /// The previous flags and their results are preserved as part of the new pattern. /// The new flag's result is initially `Unparsed`. - pub fn pick(self, flag: &'a PickerFlag<'a, N>) -> PickerPattern$+<'a, (T$,+), N> + pub fn pick(self, flag: &'a PickerFlag<'a, N>) -> PickerPattern$+<'a, (T$,+), N, Route> where N: Pickable<'a>, { PickerPattern$+ { // Args args: self.args, + error_route: self.error_route, // Current flag_$+: flag, @@ -125,17 +127,18 @@ internal_repeat!(1..32 => { } }); -impl<'a> Picker<'a> { +impl<'a, Route> Picker<'a, Route> { /// Creates a `PickerPattern1` from the given flag to start a picking chain. /// /// This method initiates a parameter picking chain with one flag. /// The result is initially `Unparsed`. - pub fn pick(self, flag: &'a PickerFlag<'a, N>) -> PickerPattern1<'a, N> + pub fn pick(self, flag: &'a PickerFlag<'a, N>) -> PickerPattern1<'a, N, Route> where N: Pickable<'a>, { PickerPattern1 { args: self.args, + error_route: None::, flag_1: flag, result_1: PickerArgResult::Unparsed, default_1: None, -- cgit