From 4836fceada0d9a87a82e1f2b011b3f9f5956dc4c Mon Sep 17 00:00:00 2001 From: Weicao-CatilGrass <1992414357@qq.com> Date: Sat, 23 May 2026 17:44:17 +0800 Subject: Add Default derive and Option Pickable implementation --- CHANGELOG.md | 4 ++++ mingling/src/parser/picker.rs | 1 + mingling/src/parser/picker/builtin.rs | 13 +++++++++++++ 3 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b7bf1a..26fcabf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,10 @@ fn render_greeting(prev: Greeting, res: &MyRes) { } ``` +5. **\[picker\]** Implement `Pickable` for `Option` + +Added `impl + Default> Pickable for Option`, allowing optional values to be directly parsed via `Pickable` without manually handling the `Option` wrapping logic. + #### **BREAKING CHANGES** (API CHANGES): 1. **\[core\]** Panic Unwind will not be supported when the `async` feature is enabled 2. **\[core\]** `modify_res` signature changed: now returns `Return` instead of `()` diff --git a/mingling/src/parser/picker.rs b/mingling/src/parser/picker.rs index 725d4e6..b0cdb70 100644 --- a/mingling/src/parser/picker.rs +++ b/mingling/src/parser/picker.rs @@ -14,6 +14,7 @@ pub mod path; /// /// The `Picker` struct holds parsed arguments and provides a fluent interface /// to extract values associated with specific flags. +#[derive(Default)] pub struct Picker { /// The parsed command-line arguments. pub args: Argument, diff --git a/mingling/src/parser/picker/builtin.rs b/mingling/src/parser/picker/builtin.rs index 9fbbfd1..e7a178d 100644 --- a/mingling/src/parser/picker/builtin.rs +++ b/mingling/src/parser/picker/builtin.rs @@ -60,6 +60,7 @@ impl Pickable for bool { } } +/// Special: parses a size string (e.g. "10MB") into a `usize` representing the number of bytes. impl Pickable for usize { type Output = usize; @@ -73,6 +74,7 @@ impl Pickable for usize { } } +/// Special: parses a comma-separated list of size strings (e.g. "10MB,20KB") into a `Vec`. impl Pickable for Vec { type Output = Vec; @@ -90,6 +92,7 @@ impl Pickable for Vec { } } +/// Special: dumps the remaining arguments into an `Argument` struct. impl Pickable for Argument { type Output = Argument; @@ -100,3 +103,13 @@ impl Pickable for Argument { Some(args.dump_remains().into()) } } + +/// Special: parses a single value of type `T` using the `Pickable` implementation for `T`, and wraps it in an `Option`. +impl + Default> Pickable for Option { + type Output = Option; + + fn pick(args: &mut Argument, flag: mingling_core::Flag) -> Option { + let r = T::pick(args, flag); + Some(r) + } +} -- cgit