From 559ff1a4d5d164c2fdbc29a9957e3db38651705f Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 15 Jul 2026 03:52:25 +0800 Subject: refactor(pickable): redesign Pickable trait with two-phase parse API Introduce `get_attr`, `tag`, and `pick` as the new trait methods, replacing the single `tag` method that returned a `PickerTag`. This enables the parser to first collect all argument position requirements and then resolve them in a second pass. Rename `PickerTag` to `PickerArgInfo` and reorder `PickerFlagAttr` variants to reflect parse priority (`Positional < Flag < Single < Multi`). Add `positional_or_else` helper to `PickerFlagAttr`. --- mingling_picker/src/infos.rs | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) (limited to 'mingling_picker/src/infos.rs') diff --git a/mingling_picker/src/infos.rs b/mingling_picker/src/infos.rs index 6270483..f820c9a 100644 --- a/mingling_picker/src/infos.rs +++ b/mingling_picker/src/infos.rs @@ -246,21 +246,7 @@ impl PickerResult { } } -/// Represents a command-line argument/flag tag for a picker. -/// -/// This struct defines how a picker argument should be parsed from the command line, -/// including its short name (e.g., `-n`), long name (e.g., `--name`), and aliases. -/// -/// # Fields -/// -/// * `short` - The short form of the tag, typically a single character (e.g., `-n`). -/// * `long` - The long form of the tag (e.g., `--name`). -/// * `alias` - Alternative names for the tag (e.g., `["-N", "--nickname"]`). -/// * `positional` - Whether this tag is a positional argument (no `-` or `--` prefix). -/// * `optional` - Whether this tag is optional or required. -/// * `multi` - Whether this tag can accept multiple values. -/// * `is_flag` - Whether this tag participates in parsing after a `--` separator. -pub struct PickerTag<'a> { +pub struct PickerArgInfo<'a> { /// The short form of the tag, e.g. `'n'` for `-n`. pub short: Option, /// The long form of the tag, e.g. `"name"` for `--name`. @@ -277,7 +263,7 @@ pub struct PickerTag<'a> { pub is_flag: bool, } -impl<'a, T> From> for PickerTag<'a> +impl<'a, T> From> for PickerArgInfo<'a> where T: Pickable<'a> + Default, { @@ -307,7 +293,7 @@ where } } -impl<'a, T: Pickable<'a> + Default> From<&'a PickerFlag<'a, T>> for PickerTag<'a> { +impl<'a, T: Pickable<'a> + Default> From<&'a PickerFlag<'a, T>> for PickerArgInfo<'a> { fn from(value: &'a PickerFlag<'a, T>) -> Self { let (long, alias) = match value.full.len() { 0 => (None, None), @@ -334,7 +320,7 @@ impl<'a, T: Pickable<'a> + Default> From<&'a PickerFlag<'a, T>> for PickerTag<'a } } -impl<'a> PickerTag<'a> { +impl<'a> PickerArgInfo<'a> { /// Create a new `PickerTag` with default values. pub fn new() -> Self { Self { @@ -433,7 +419,7 @@ impl<'a> PickerTag<'a> { } } -impl<'a> Default for PickerTag<'a> { +impl<'a> Default for PickerArgInfo<'a> { fn default() -> Self { Self::new() } -- cgit