From 1f34aa214dcd94c327a4ce09a1a4e4db176099ef Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Thu, 16 Jul 2026 23:51:53 +0800 Subject: refactor(pickable): extract seek_single helper from pick_string --- mingling_picker/src/builtin/pick_string.rs | 16 ++++------------ mingling_picker/src/parselib/utils.rs | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/mingling_picker/src/builtin/pick_string.rs b/mingling_picker/src/builtin/pick_string.rs index 6c11ef3..4a65873 100644 --- a/mingling_picker/src/builtin/pick_string.rs +++ b/mingling_picker/src/builtin/pick_string.rs @@ -1,4 +1,4 @@ -use crate::parselib::{ParserStyle, SingleMatcher}; +use crate::parselib::{SingleMatcher, seek_single}; use crate::pickable_needed::*; impl<'a> Pickable<'a> for String { @@ -11,17 +11,9 @@ impl<'a> Pickable<'a> for String { } fn pick(raw_strs: &[&str]) -> PickerArgResult { - match raw_strs.len() { - 0 => PickerArgResult::NotFound, - 1 => { - let s = raw_strs[0]; - let sep = ParserStyle::global_style().value_separator; - if let Some(pos) = s.rfind(sep) { - return PickerArgResult::Parsed(s[pos + 1..].to_string()); - } - PickerArgResult::Parsed(s.to_string()) - } - _ => PickerArgResult::Parsed(raw_strs[1].to_string()), + match seek_single(raw_strs) { + Some(v) => PickerArgResult::Parsed(v.to_string()), + None => PickerArgResult::NotFound, } } } diff --git a/mingling_picker/src/parselib/utils.rs b/mingling_picker/src/parselib/utils.rs index ae3c203..4fd34b7 100644 --- a/mingling_picker/src/parselib/utils.rs +++ b/mingling_picker/src/parselib/utils.rs @@ -26,6 +26,31 @@ pub fn build_possible_flags(style: &ParserStyle, arg_info: &PickerArgInfo) -> Ve possible_flags } +/// Extract a single value from the raw strings tagged by [`SingleMatcher`]. +/// +/// Returns `None` if no value is available (empty slice), +/// the inline value after the style separator if present (eq mode), +/// or the value directly (positional or flag-following). +/// +/// This is the standard `pick` helper for all `Single`-type +/// [`Pickable`](crate::Pickable) implementations. +#[must_use] +pub fn seek_single<'a>(raw_strs: &'a [&'a str]) -> Option<&'a str> { + match raw_strs.len() { + 0 => None, + 1 => { + let s = raw_strs[0]; + let sep = ParserStyle::global_style().value_separator; + if let Some(pos) = s.rfind(sep) { + Some(&s[pos + 1..]) + } else { + Some(s) + } + } + _ => Some(raw_strs[1]), + } +} + /// Seeks the index of the end-of-options marker (`--`) in the argument list. /// /// This function searches for the standard end-of-options separator (`--`) -- cgit