aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-07-15 17:09:27 +0800
committer魏曹先生 <1992414357@qq.com>2026-07-15 17:09:27 +0800
commit06199a26474b1e53761ca6838014c4dc8d3488e2 (patch)
tree0fd9cdfcc680aa22799d8a9eb51beaa10379ae36
parentd1a6e9810b4f1555fb9852065af4c9df6ed463f0 (diff)
feat(picker): add unwrap, unpack, to_result, to_option on PickerPattern
-rw-r--r--mingling_picker/src/pickable.rs2
-rw-r--r--mingling_picker/src/pickable/implements.rs29
-rw-r--r--mingling_picker/src/picker/parse.rs45
3 files changed, 44 insertions, 32 deletions
diff --git a/mingling_picker/src/pickable.rs b/mingling_picker/src/pickable.rs
index 3f86745..4f8c534 100644
--- a/mingling_picker/src/pickable.rs
+++ b/mingling_picker/src/pickable.rs
@@ -1,7 +1,5 @@
use crate::{PickerArgInfo, PickerArgResult, PickerArgs, PickerFlag, PickerFlagAttr};
-mod implements;
-
/// `Pickable` trait defines how to parse a type instance from command-line arguments.
///
/// This trait is the core abstraction of the `Picker` argument parsing system, dividing the
diff --git a/mingling_picker/src/pickable/implements.rs b/mingling_picker/src/pickable/implements.rs
deleted file mode 100644
index 7d19a8c..0000000
--- a/mingling_picker/src/pickable/implements.rs
+++ /dev/null
@@ -1,29 +0,0 @@
-use crate::{Pickable, PickerFlagAttr};
-
-impl<'a> Pickable<'a> for String {
- fn get_attr(flag: &'a crate::PickerFlag<'a, Self>) -> PickerFlagAttr {
- PickerFlagAttr::positional_or_single(flag)
- }
-
- fn tag(_ctx: super::TagPhaseContext) -> Vec<usize> {
- vec![]
- }
-
- fn pick(_raw_strs: &[&str]) -> crate::PickerArgResult<Self> {
- todo!()
- }
-}
-
-impl<'a> Pickable<'a> for Vec<String> {
- fn get_attr(flag: &'a crate::PickerFlag<'a, Self>) -> PickerFlagAttr {
- PickerFlagAttr::positional_or_multi(flag)
- }
-
- fn tag(_ctx: super::TagPhaseContext) -> Vec<usize> {
- vec![]
- }
-
- fn pick(_raw_strs: &[&str]) -> crate::PickerArgResult<Self> {
- todo!()
- }
-}
diff --git a/mingling_picker/src/picker/parse.rs b/mingling_picker/src/picker/parse.rs
index 67122a5..744254d 100644
--- a/mingling_picker/src/picker/parse.rs
+++ b/mingling_picker/src/picker/parse.rs
@@ -6,6 +6,9 @@
//
// P.S. If there's a better way, please let me know. Thanks!
// --------------------------------------------------------------------------------------------
+//
+// Then, I must disable `clippy::type_complexity` — this guy is way too noisy.
+#![allow(clippy::type_complexity)]
use crate::{
Pickable, PickerArgInfo, PickerArgResult, PickerArgs, PickerFlagAttr, TagPhaseContext,
@@ -19,9 +22,49 @@ internal_repeat!(1..=32 => {
internal_repeat!(1..=32 => {
impl<'a, (T$,+), Route> PickerPattern$<'a, (T$,+), Route>
+ where (T$: Pickable<'a>,+) {
+ /// Unwraps the result, panicking if a route was selected.
+ ///
+ /// # Panics
+ ///
+ /// Panics if a route was selected.
+ pub fn unwrap(self) -> ((T$,+)) {
+ let p = self.parse();
+ ((p.v$.unwrap(),+))
+ }
+
+ /// Returns the individual option values without checking the route.
+ pub fn unpack(self) -> ((Option<T$>,+)) {
+ let p = self.parse();
+ ((p.v$,+))
+ }
+
+ /// Converts to a `Result`, returning `Err(route)` if a route was selected,
+ /// or `Ok(values)` otherwise.
+ pub fn to_result(self) -> Result<((T$,+)), Route> {
+ let p = self.parse();
+ if let Some(r) = p.route {
+ return Err(r);
+ }
+ Ok(p.unwrap())
+ }
+
+ /// Converts to an `Option`, returning `None` if a route was selected,
+ /// or `Some(values)` otherwise.
+ pub fn to_option(self) -> Option<((T$,+))> {
+ let p = self.parse();
+ if p.route.is_some() {
+ return None;
+ }
+ Some(p.unwrap())
+ }
+ }
+});
+
+internal_repeat!(1..=32 => {
+ impl<'a, (T$,+), Route> PickerPattern$<'a, (T$,+), Route>
where (T$: Pickable<'a>,+)
{
- #[allow(clippy::type_complexity)]
pub fn parse(mut self) -> PickerResult$<(T$,+), Route> {
// ArgInfos
let arg_infos: [PickerArgInfo; $] = [