diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-07-28 12:00:57 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-07-28 12:00:57 +0800 |
| commit | 432a6e2892cf900c2f8dc80cf4f9f6bc8adb4b7b (patch) | |
| tree | cdbbadf71a30bcebd100a4e228659442b53326e6 | |
| parent | a875ca155a2c8d96bf68d37fcd3e14afea7f4430 (diff) | |
Add `unwrap_or_default`, `unwrap_or_else`, and `expect` methods to
reduce boilerplate when working with parsed argument tuples
| -rw-r--r-- | CHANGELOG.md | 8 | ||||
| -rw-r--r-- | arg_picker/src/picker/parse.rs | 49 |
2 files changed, 57 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index cd2c0a3..70137fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,14 @@ None Each type implements `SinglePickable`, performing filesystem validation at parse time and returning `NotFound` when the precondition is not met. +2. **[`picker:parsing`]** Added convenience methods to the internal `repeat!`-generated tuple implementations for `PickArgParsed<T1, T2, ...>` structs in `arg_picker::picker::parse`: + + - **`unwrap_or_default(self)`** — Returns the parsed values, using `Default::default()` for any missing required arguments. Panics if a route was selected. + - **`unwrap_or_else<F>(self, op: F)`** — Returns the parsed values, using the provided closure to generate default values for any missing required arguments. Panics if a route was selected. + - **`expect(self, msg: &str)`** — Returns the parsed values, or panics with the given message if a route was selected. Requires `Route: std::fmt::Debug`. + + These methods provide ergonomic alternatives to `to_result()` + `unwrap()` / `unwrap_or_default()` / `unwrap_or_else()` / `expect()` chaining, reducing boilerplate when working with `PickArgParsed` tuples directly. + #### **BREAKING CHANGES** (API CHANGES): None 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<F>(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<T$>,+)) { let p = self.parse(); |
