diff options
| -rw-r--r-- | mingling_picker/src/builtin/pick_string.rs | 22 | ||||
| -rw-r--r-- | mingling_picker/src/pickable.rs | 44 |
2 files changed, 51 insertions, 15 deletions
diff --git a/mingling_picker/src/builtin/pick_string.rs b/mingling_picker/src/builtin/pick_string.rs index 4a65873..c96f667 100644 --- a/mingling_picker/src/builtin/pick_string.rs +++ b/mingling_picker/src/builtin/pick_string.rs @@ -1,19 +1,11 @@ -use crate::parselib::{SingleMatcher, seek_single}; -use crate::pickable_needed::*; +use crate::PickerArgResult::NotFound; +use crate::{SinglePickable, pickable_needed::*}; -impl<'a> Pickable<'a> for String { - fn get_attr(flag: &'a PickerArg<'a, Self>) -> PickerArgAttr { - PickerArgAttr::positional_or_single(flag) - } - - fn tag(ctx: TagPhaseContext) -> Vec<usize> { - SingleMatcher::tag(ctx) - } - - fn pick(raw_strs: &[&str]) -> PickerArgResult<Self> { - match seek_single(raw_strs) { - Some(v) => PickerArgResult::Parsed(v.to_string()), - None => PickerArgResult::NotFound, +impl SinglePickable for String { + fn pick_single(str: Option<&str>) -> PickerArgResult<Self> { + match str { + Some(str) => PickerArgResult::Parsed(str.to_string()), + None => NotFound, } } } diff --git a/mingling_picker/src/pickable.rs b/mingling_picker/src/pickable.rs index c88e49b..7424e6f 100644 --- a/mingling_picker/src/pickable.rs +++ b/mingling_picker/src/pickable.rs @@ -83,3 +83,47 @@ pub struct TagPhaseContext<'a> { /// has already been tagged by another `Pickable`. pub mask: &'a [u8], } + +/// `SinglePickable` trait defines how to parse a type from a single command-line argument. +/// +/// This trait provides a simplified interface for types that consume exactly one argument value. +/// It is automatically implemented by the blanket `impl` of [`Pickable`], so types implementing +/// `SinglePickable` will work with the full `Pickable` argument parsing system. +/// +/// # Type Parameters +/// +/// * `Self` - The type to be parsed from a single argument string. +pub trait SinglePickable +where + Self: Sized, +{ + /// Parse a single optional string value into an instance of `Self`. + /// + /// # Parameters + /// + /// * `str` - An `Option<&str>` representing the raw argument value. If `None`, + /// it indicates that no argument value was provided (e.g., for flag-like arguments). + /// + /// # Returns + /// + /// Returns [`PickerArgResult<Self>`], i.e., the parsed `Self` instance on success, + /// or an appropriate error message on failure. + fn pick_single(str: Option<&str>) -> PickerArgResult<Self>; +} + +impl<'a, S> Pickable<'a> for S +where + S: SinglePickable, +{ + fn get_attr(flag: &'a PickerArg<'a, Self>) -> PickerArgAttr { + PickerArgAttr::positional_or_single(flag) + } + + fn tag(ctx: TagPhaseContext) -> Vec<usize> { + crate::parselib::SingleMatcher::tag(ctx) + } + + fn pick(raw_strs: &[&str]) -> PickerArgResult<Self> { + Self::pick_single(crate::parselib::seek_single(raw_strs)) + } +} |
