diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-07-14 02:17:33 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-07-14 02:17:33 +0800 |
| commit | de370df01a171b4fd910dfa9a067830acd1f9a3c (patch) | |
| tree | 53350a4cdaa93437da6f06916ac371f82e79c57b /mingling_picker/src | |
| parent | 62f6b476284da67efc55483a81b57f99a478702a (diff) | |
refactor(mingling_picker): move parse result out of requirement and into
pattern
BREAKING CHANGE: `PickerRequirement` no longer stores a `PickerResult`.
Parsed results are now held in the `PickerPattern` structs.
The `result`, `result_mut`, `set_result`, `reset_result`, and
`with_result` methods on `PickerRequirement` have been removed.
The `Picker::pick` method is introduced to begin a pattern definition.
Diffstat (limited to 'mingling_picker/src')
| -rw-r--r-- | mingling_picker/src/picker/patterns.rs | 22 | ||||
| -rw-r--r-- | mingling_picker/src/requirement.rs | 48 |
2 files changed, 28 insertions, 42 deletions
diff --git a/mingling_picker/src/picker/patterns.rs b/mingling_picker/src/picker/patterns.rs index 74a8cd8..80632d5 100644 --- a/mingling_picker/src/picker/patterns.rs +++ b/mingling_picker/src/picker/patterns.rs @@ -1,12 +1,26 @@ -use crate::{Pickable, PickerArguments, PickerRequirement}; +use crate::{Pickable, Picker, PickerArguments, PickerRequirement, PickerResult}; mingling_picker_macros::internal_repeat! (1..=32 => { - pub struct PickerPattern$<'a, (Type$,)+> - where (Type$: Pickable + Default,)+ + pub struct PickerPattern$<'a, (T$,)+> + where (T$: Pickable + Default,)+ { pub args: PickerArguments<'a>, ( - pub require_$: PickerRequirement<'a, Type$>, + pub require_$: &'a PickerRequirement<'a, T$>, + pub result_$: PickerResult<T$>, )+ } }); + +impl<'a> Picker<'a> { + pub fn pick<N>(self, req: &'a PickerRequirement<'a, N>) -> PickerPattern1<'a, N> + where + N: Pickable + Default, + { + PickerPattern1 { + args: self.args, + require_1: req, + result_1: PickerResult::Unparsed, + } + } +} diff --git a/mingling_picker/src/requirement.rs b/mingling_picker/src/requirement.rs index 32fefff..564ae45 100644 --- a/mingling_picker/src/requirement.rs +++ b/mingling_picker/src/requirement.rs @@ -1,4 +1,5 @@ -use crate::{Pickable, PickerResult}; +use crate::Pickable; +use std::marker::PhantomData; /// Represents a constraint definition for a parameter selection. /// @@ -19,25 +20,23 @@ use crate::{Pickable, PickerResult}; /// than by a `--name` or `-n` flag. /// - `false`: The parameter is a named (flag-based) parameter. /// -/// - `result`: The parsed result of this parameter requirement. Initially set to -/// `PickerResult::Unparsed`. After parsing, contains either the successfully parsed value or an -/// error. -#[derive(Default)] +/// - `_type`: PhantomData to hold the type parameter. +#[derive(Default, Clone, Copy)] pub struct PickerRequirement<'a, Type> where Type: Default + Pickable, { /// Full name, may include variant names (aliases), e.g., `["config", "cfg"]`. - full: &'a [&'a str], + pub full: &'a [&'a str], /// Short name, e.g., `'c'`. - short: Option<char>, + pub short: Option<char>, /// Whether the parameter is positional (no flag, matched by position). - positional: bool, + pub positional: bool, - /// The parsed result of this parameter requirement. - result: PickerResult<Type>, + /// PhantomData to hold the type parameter. + pub internal_type: PhantomData<Type>, } impl<'a, Type> PickerRequirement<'a, Type> @@ -50,7 +49,7 @@ where full, short, positional, - result: PickerResult::Unparsed, + internal_type: PhantomData, } } @@ -120,31 +119,4 @@ where self.positional = positional; self } - - /// Returns a reference to the current parse result. - pub fn result(&self) -> &PickerResult<Type> { - &self.result - } - - /// Returns a mutable reference to the current parse result. - pub fn result_mut(&mut self) -> &mut PickerResult<Type> { - &mut self.result - } - - /// Sets the parse result. - pub fn set_result(&mut self, result: PickerResult<Type>) { - self.result = result; - } - - /// Replaces the parse result with `PickerResult::Unparsed` and returns self. - pub fn reset_result(mut self) -> Self { - self.result = PickerResult::Unparsed; - self - } - - /// Sets the parse result and returns self. - pub fn with_result(mut self, result: PickerResult<Type>) -> Self { - self.result = result; - self - } } |
