aboutsummaryrefslogtreecommitdiff
path: root/arg_picker/src/picker.rs
diff options
context:
space:
mode:
Diffstat (limited to 'arg_picker/src/picker.rs')
-rw-r--r--arg_picker/src/picker.rs108
1 files changed, 104 insertions, 4 deletions
diff --git a/arg_picker/src/picker.rs b/arg_picker/src/picker.rs
index ecab648..f722fb5 100644
--- a/arg_picker/src/picker.rs
+++ b/arg_picker/src/picker.rs
@@ -73,6 +73,26 @@ pub enum PickerArgs<'a> {
Owned(Vec<String>),
}
+impl<'a> From<PickerArgs<'a>> for Vec<String> {
+ fn from(value: PickerArgs<'a>) -> Self {
+ match value {
+ PickerArgs::Slice(items) => items.iter().map(|s| s.to_string()).collect(),
+ PickerArgs::Vec(items) => items.into_iter().map(|s| s.to_string()).collect(),
+ PickerArgs::Owned(items) => items,
+ }
+ }
+}
+
+impl<'a> From<&'a PickerArgs<'a>> for Vec<&'a str> {
+ fn from(value: &'a PickerArgs<'a>) -> Self {
+ match value {
+ PickerArgs::Slice(items) => items.to_vec(),
+ PickerArgs::Vec(items) => items.clone(),
+ PickerArgs::Owned(items) => items.iter().map(|s| s.as_str()).collect(),
+ }
+ }
+}
+
impl<'a> Default for PickerArgs<'a> {
fn default() -> Self {
Self::Vec(vec![])
@@ -138,6 +158,15 @@ impl<'a> IntoIterator for &'a PickerArgs<'a> {
}
}
+impl<'a, Route> From<PickerArgs<'a>> for Picker<'a, Route> {
+ fn from(args: PickerArgs<'a>) -> Self {
+ Picker {
+ route_phantom: PhantomData,
+ args,
+ }
+ }
+}
+
impl<'a, Route> From<&'a [&'a str]> for Picker<'a, Route> {
fn from(value: &'a [&'a str]) -> Self {
Picker {
@@ -340,11 +369,83 @@ pub trait IntoPicker<'a> {
fn pick<N>(self, arg: impl Into<&'a PickerArg<'a, N>>) -> PickerPattern1<'a, N, ()>
where
Self: Sized,
- N: Pickable<'a> + Default + Sized,
+ N: Pickable<'a> + Sized,
{
Picker::build_pattern1(self.to_picker().args, arg.into(), None::<()>)
}
+ /// Starts building a picker pattern with the first argument, using a default value provider.
+ ///
+ /// This is a shorthand for calling `.pick(arg).or(func)`.
+ ///
+ /// # Type Parameters
+ ///
+ /// * `Next` — The nominal type of the first argument; must implement [`Pickable`].
+ ///
+ /// # Parameters
+ ///
+ /// * `arg` — The argument definition, typically obtained from [`crate::macros::arg`].
+ /// * `func` — A closure that provides a default value if the arg is not provided by the user.
+ fn pick_or<Next, F>(
+ self,
+ arg: impl Into<&'a PickerArg<'a, Next>>,
+ func: F,
+ ) -> PickerPattern1<'a, Next, ()>
+ where
+ Self: Sized,
+ Next: Pickable<'a> + Sized,
+ F: FnMut() -> Next + 'static,
+ {
+ self.pick(arg).or(func)
+ }
+
+ /// Starts building a picker pattern with the first argument, using a default value.
+ ///
+ /// This is a shorthand for calling `.pick(arg).or_default()`.
+ ///
+ /// # Type Parameters
+ ///
+ /// * `Next` — The nominal type of the first argument; must implement [`Pickable`] and [`Default`].
+ ///
+ /// # Parameters
+ ///
+ /// * `arg` — The argument definition, typically obtained from [`crate::macros::arg`].
+ fn pick_or_default<Next>(
+ self,
+ arg: impl Into<&'a PickerArg<'a, Next>>,
+ ) -> PickerPattern1<'a, Next, ()>
+ where
+ Self: Sized,
+ Next: Pickable<'a> + Default + Sized,
+ {
+ self.pick(arg).or_default()
+ }
+
+ /// Starts building a picker pattern with the first argument, using a route if the arg is not provided.
+ ///
+ /// This is a shorthand for calling `.pick(arg).or_route(func)`.
+ ///
+ /// # Type Parameters
+ ///
+ /// * `Next` — The nominal type of the first argument; must implement [`Pickable`].
+ ///
+ /// # Parameters
+ ///
+ /// * `arg` — The argument definition, typically obtained from [`crate::macros::arg`].
+ /// * `func` — A closure that produces a route value if the arg is not provided by the user.
+ fn pick_or_route<Next, F, Route>(
+ self,
+ arg: impl Into<&'a PickerArg<'a, Next>>,
+ func: F,
+ ) -> PickerPattern1<'a, Next, Route>
+ where
+ Self: Sized,
+ Next: Pickable<'a> + Sized,
+ F: FnMut() -> Route + 'static,
+ {
+ self.pick(arg).with_route::<Route>().or_route(func)
+ }
+
/// Converts the value into a `Picker` with a specified route type.
///
/// This method allows changing the route (phantom type parameter) of the picker.
@@ -408,10 +509,9 @@ 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>(
+ /// Build the PickerPattern via Arguments
+ pub fn build_pattern1<N>(
args: PickerArgs<'a>,
arg: &'a PickerArg<'a, N>,
error_route: Option<Route>,