From 06199a26474b1e53761ca6838014c4dc8d3488e2 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 15 Jul 2026 17:09:27 +0800 Subject: feat(picker): add unwrap, unpack, to_result, to_option on PickerPattern --- mingling_picker/src/pickable.rs | 2 -- mingling_picker/src/pickable/implements.rs | 29 ------------------- mingling_picker/src/picker/parse.rs | 45 +++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 32 deletions(-) delete mode 100644 mingling_picker/src/pickable/implements.rs 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 { - vec![] - } - - fn pick(_raw_strs: &[&str]) -> crate::PickerArgResult { - todo!() - } -} - -impl<'a> Pickable<'a> for Vec { - fn get_attr(flag: &'a crate::PickerFlag<'a, Self>) -> PickerFlagAttr { - PickerFlagAttr::positional_or_multi(flag) - } - - fn tag(_ctx: super::TagPhaseContext) -> Vec { - vec![] - } - - fn pick(_raw_strs: &[&str]) -> crate::PickerArgResult { - 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, @@ -17,11 +20,51 @@ internal_repeat!(1..=32 => { use crate::PickerResult$; }); +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,+)) { + 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; $] = [ -- cgit