aboutsummaryrefslogtreecommitdiff
path: root/mingling/src/parser
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-04-30 22:28:07 +0800
committer魏曹先生 <1992414357@qq.com>2026-04-30 22:28:07 +0800
commit5c5b828fcb75aef5708ff95ea1f7994f86bef5b4 (patch)
tree9cba36d493faf8ee7a36956c96d26a0e4000322e /mingling/src/parser
parenta1c407776897a0649deec1b7c6ee86347f91dcaf (diff)
Add `AsPicker` trait for types convertible to `Vec<String>`
Diffstat (limited to 'mingling/src/parser')
-rw-r--r--mingling/src/parser/picker.rs66
1 files changed, 66 insertions, 0 deletions
diff --git a/mingling/src/parser/picker.rs b/mingling/src/parser/picker.rs
index 4641a49..915bc35 100644
--- a/mingling/src/parser/picker.rs
+++ b/mingling/src/parser/picker.rs
@@ -719,3 +719,69 @@ where
T::build_enum(name)
}
}
+
+pub trait AsPicker
+where
+ Self: Into<Vec<String>>,
+{
+ /// Converts the value into a `Picker` by first converting it into a `Vec<String>`.
+ fn as_picker(self) -> Picker
+ where
+ Self: Sized,
+ Vec<String>: From<Self>,
+ {
+ let vec: Vec<String> = self.into();
+ Picker { args: vec.into() }
+ }
+
+ /// Extracts a value for the given flag and returns a `Pick1` builder (no route).
+ ///
+ /// The extracted type `TNext` must implement `Pickable` and `Default`.
+ /// If the flag is not present, the default value for `TNext` is used.
+ fn pick<TNext>(self, val: impl Into<Flag>) -> Pick1<TNext>
+ where
+ Self: Sized,
+ TNext: Pickable<Output = TNext> + Default,
+ {
+ let vec: Vec<String> = self.into();
+ let picker: Picker = vec.into();
+ picker.pick(val)
+ }
+
+ /// Extracts a value for the given flag, returning the provided default value if not present,
+ /// and returns a `Pick1` builder (no route).
+ ///
+ /// The extracted type `TNext` must implement `Pickable`.
+ /// If the flag is not present, the provided `or` value is used.
+ fn pick_or<TNext>(self, val: impl Into<Flag>, or: impl Into<TNext>) -> Pick1<TNext>
+ where
+ TNext: Pickable<Output = TNext>,
+ {
+ let vec: Vec<String> = self.into();
+ let picker: Picker = vec.into();
+ picker.pick_or(val, or)
+ }
+
+ /// Extracts a value for the given flag, storing the provided route if the flag is not present,
+ /// and returns a `PickWithRoute1` builder (with route).
+ ///
+ /// The extracted type `TNext` must implement `Pickable` and `Default`.
+ /// If the flag is not present, the default value for `TNext` is used and the provided `route`
+ /// is stored in the returned builder for later error handling.
+ fn pick_or_route<TNext, R>(self, val: impl Into<Flag>, route: R) -> PickWithRoute1<TNext, R>
+ where
+ TNext: Pickable<Output = TNext> + Default,
+ {
+ let vec: Vec<String> = self.into();
+ let picker: Picker = vec.into();
+ picker.pick_or_route(val, route)
+ }
+}
+
+// Implement AsPicker for any type that can be converted into a Vec<String>
+impl<T> AsPicker for T
+where
+ T: Sized,
+ Vec<String>: From<T>,
+{
+}