From ed4b8dfc825b59012ad850d6c88067e7007962a2 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Fri, 17 Jul 2026 09:03:32 +0800 Subject: feat: add shorthand methods for fallback picker patterns Introduce `pick_or`, `pick_or_default`, and `pick_or_route` methods on both `Picker` and existing patterns to combine picking with a fallback in a single call. Extract shared `PickerPattern1` construction into `Picker::build_pattern1`. --- mingling_picker/src/picker.rs | 86 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 77 insertions(+), 9 deletions(-) (limited to 'mingling_picker/src/picker.rs') diff --git a/mingling_picker/src/picker.rs b/mingling_picker/src/picker.rs index 81fb10a..7cf1525 100644 --- a/mingling_picker/src/picker.rs +++ b/mingling_picker/src/picker.rs @@ -254,6 +254,59 @@ impl<'a> Iterator for PickerIter<'a> { impl<'a> ExactSizeIterator for PickerIter<'a> {} +impl<'a, Route> Picker<'a, Route> { + /// Creates a `PickerPattern1` from the given arg to start a picking chain. + /// + /// This method initiates a parameter picking chain with one arg. + /// The result is initially `Unparsed`. + pub fn pick(self, arg: impl Into<&'a PickerArg<'a, N>>) -> PickerPattern1<'a, N, Route> + where + N: Pickable<'a>, + { + Self::build_pattern1(self.args, arg.into(), None::) + } + + /// Creates a `PickerPattern1` from the given arg. + /// If parsing fails, attempts the fallback arg. + pub fn pick_or( + self, + arg: impl Into<&'a PickerArg<'a, N>>, + or_arg: F, + ) -> PickerPattern1<'a, N, Route> + where + N: Pickable<'a>, + F: FnMut() -> N + 'static, + { + self.pick(arg).or(or_arg) + } + + /// Creates a `PickerPattern1` from the given arg. + /// If parsing fails, uses the provided default value. + pub fn pick_or_default( + self, + arg: impl Into<&'a PickerArg<'a, N>>, + ) -> PickerPattern1<'a, N, Route> + where + N: Pickable<'a> + Default, + { + self.pick(arg).or_default() + } + + /// Creates a `PickerPattern1` from the given arg. + /// If parsing fails, switches to the given error route. + pub fn pick_or_route( + self, + arg: impl Into<&'a PickerArg<'a, N>>, + error_route: F, + ) -> PickerPattern1<'a, N, Route> + where + N: Pickable<'a>, + F: FnMut() -> Route + 'static, + { + self.pick(arg).or_route(error_route) + } +} + /// Trait for converting types into a `Picker` /// /// Implemented for: @@ -289,15 +342,7 @@ pub trait IntoPicker<'a> { Self: Sized, N: Pickable<'a> + Default + Sized, { - PickerPattern1 { - args: self.to_picker().args, - arg_1: arg.into(), - result_1: PickerArgResult::Unparsed, - default_1: None, - route_1: None, - post_1: None, - error_route: None, - } + Picker::build_pattern1(self.to_picker().args, arg.into(), None::<()>) } /// Converts the value into a `Picker` with a specified route type. @@ -362,3 +407,26 @@ impl<'a> IntoPicker<'a> for Vec { } } } + +// Private helper: shared construction logic for `PickerPattern1`. +// Both `Picker::pick` and `IntoPicker::pick` delegate to this. +impl<'a, Route> Picker<'a, Route> { + pub(crate) fn build_pattern1( + args: PickerArgs<'a>, + arg: &'a PickerArg<'a, N>, + error_route: Option, + ) -> PickerPattern1<'a, N, Route> + where + N: Pickable<'a>, + { + PickerPattern1 { + args, + error_route, + arg_1: arg, + result_1: PickerArgResult::Unparsed, + route_1: None, + default_1: None, + post_1: None, + } + } +} -- cgit