aboutsummaryrefslogtreecommitdiff
path: root/mingling_picker/src/picker.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-07-17 09:03:32 +0800
committer魏曹先生 <1992414357@qq.com>2026-07-17 09:03:32 +0800
commited4b8dfc825b59012ad850d6c88067e7007962a2 (patch)
tree8f5f4cd9229fae5f088f9b23ca9607341a3b5468 /mingling_picker/src/picker.rs
parentd77e8926dff6466f0c46ee4529dd9f7b9772ae99 (diff)
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`.
Diffstat (limited to 'mingling_picker/src/picker.rs')
-rw-r--r--mingling_picker/src/picker.rs86
1 files changed, 77 insertions, 9 deletions
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<N>(self, arg: impl Into<&'a PickerArg<'a, N>>) -> PickerPattern1<'a, N, Route>
+ where
+ N: Pickable<'a>,
+ {
+ Self::build_pattern1(self.args, arg.into(), None::<Route>)
+ }
+
+ /// Creates a `PickerPattern1` from the given arg.
+ /// If parsing fails, attempts the fallback arg.
+ pub fn pick_or<N, F>(
+ 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<N>(
+ 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<N, F>(
+ 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<String> {
}
}
}
+
+// 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<N>(
+ args: PickerArgs<'a>,
+ arg: &'a PickerArg<'a, N>,
+ error_route: Option<Route>,
+ ) -> 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,
+ }
+ }
+}