aboutsummaryrefslogtreecommitdiff
path: root/mingling_picker/src/picker/patterns.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-07-15 16:08:10 +0800
committer魏曹先生 <1992414357@qq.com>2026-07-15 16:08:10 +0800
commitc3e990a5c47dc8894899d774a0d89f089adec308 (patch)
tree85e8bdc834cc57bf41694f4c86e5bfd3386fcd09 /mingling_picker/src/picker/patterns.rs
parent7620fd1d1e9ff4105c2d93c80c73b2c2ec1cbc9a (diff)
refactor: rename `PickerResult` to `PickerArgResult` and add new
`PickerResult` struct
Diffstat (limited to 'mingling_picker/src/picker/patterns.rs')
-rw-r--r--mingling_picker/src/picker/patterns.rs29
1 files changed, 25 insertions, 4 deletions
diff --git a/mingling_picker/src/picker/patterns.rs b/mingling_picker/src/picker/patterns.rs
index 4f54f9d..b828849 100644
--- a/mingling_picker/src/picker/patterns.rs
+++ b/mingling_picker/src/picker/patterns.rs
@@ -1,6 +1,6 @@
use mingling_picker_macros::internal_repeat;
-use crate::{Pickable, Picker, PickerArgs, PickerFlag, PickerResult};
+use crate::{Pickable, Picker, PickerArgResult, PickerArgs, PickerFlag};
internal_repeat!(1..=32 => {
#[doc(hidden)]
@@ -10,7 +10,7 @@ internal_repeat!(1..=32 => {
pub args: PickerArgs<'a>,
(
pub flag_$: &'a PickerFlag<'a, T$>,
- pub result_$: PickerResult<T$>,
+ pub result_$: PickerArgResult<T$>,
pub default_$: Option<Box<dyn FnOnce() -> T$>>,
pub post_$: Option<Box<dyn FnOnce(T$) -> T$>>,
+)
@@ -43,6 +43,27 @@ internal_repeat!(1..=32 => {
self
}
+ /// Uses the default value for this flag's type if the flag is not provided.
+ ///
+ /// If the flag is not provided by the user at runtime, the default value for `T$`
+ /// (as defined by the `Default` trait) will be used.
+ ///
+ /// # Example
+ ///
+ /// ```ignore
+ /// let pattern = picker
+ /// .pick(&my_flag)
+ /// .or_default();
+ /// ```
+ #[allow(clippy::type_complexity)]
+ pub fn or_default(mut self) -> Self
+ where
+ T$: Default,
+ {
+ self.default_$ = Some(Box::new(|| T$::default()));
+ self
+ }
+
/// Attaches a post-processing function to this flag.
///
/// After the flag's value is parsed (or defaulted), the given closure will be
@@ -88,7 +109,7 @@ internal_repeat!(1..32 => {
// Current
flag_$+: flag,
- result_$+: PickerResult::Unparsed,
+ result_$+: PickerArgResult::Unparsed,
default_$+: None,
post_$+: None,
@@ -116,7 +137,7 @@ impl<'a> Picker<'a> {
PickerPattern1 {
args: self.args,
flag_1: flag,
- result_1: PickerResult::Unparsed,
+ result_1: PickerArgResult::Unparsed,
default_1: None,
post_1: None,
}