From 432a6e2892cf900c2f8dc80cf4f9f6bc8adb4b7b Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Tue, 28 Jul 2026 12:00:57 +0800 Subject: feat(picker): add convenience methods to PickArgParsed tuples Add `unwrap_or_default`, `unwrap_or_else`, and `expect` methods to reduce boilerplate when working with parsed argument tuples --- arg_picker/src/picker/parse.rs | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'arg_picker/src') diff --git a/arg_picker/src/picker/parse.rs b/arg_picker/src/picker/parse.rs index 9db5bd9..4a959e3 100644 --- a/arg_picker/src/picker/parse.rs +++ b/arg_picker/src/picker/parse.rs @@ -33,6 +33,55 @@ internal_repeat!(1..=32 => { ((p.v$.expect(concat!("missing required argument at position ", $)),+)) } + /// Returns the parsed values, using the default values for any missing + /// required arguments, or panicking if a route was selected. + /// + /// # Panics + /// + /// Panics if a route was selected. + /// + /// # Type Constraints + /// + /// All types in the tuple must implement [`Default`]. + pub fn unwrap_or_default(self) -> ((T$,+)) + where + ( + T$: Default, + +) { + let p = self.parse(); + ((p.v$.unwrap_or_default(),+)) + } + + /// Returns the parsed values, using the provided closure to generate + /// default values for any missing required arguments, or panicking if + /// a route was selected. + /// + /// # Panics + /// + /// Panics if a route was selected. + pub fn unwrap_or_else(self, op: F) -> ((T$,+)) + where + F: FnOnce(Route) -> ((T$,+)), + { + let r = self.to_result(); + r.unwrap_or_else(op) + } + + /// Returns the parsed values, or panics with the given message if + /// a route was selected. + /// + /// # Panics + /// + /// Panics if a route was selected, with the provided message. + /// + /// # Type Constraints + /// + /// `Route` must implement [`std::fmt::Debug`] so that the error + /// message can include the route value. + pub fn expect(self, msg: &str) -> ((T$,+)) where Route: std::fmt::Debug { + self.to_result().expect(msg) + } + /// Returns the individual option values without checking the route. pub fn unpack(self) -> ((Option,+)) { let p = self.parse(); -- cgit