From 4665fc94ab9508d115298dd988e2381354f46c01 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Tue, 14 Jul 2026 01:36:27 +0800 Subject: feat(picker): add core trait, types, and builder macro for argument parsing Implement the foundational `mingling_picker` library along with its companion `mingling_picker_macros` crate. The picker provides: - `Pickable` trait for parsing types from raw strings - `PickerResult` enum modeling parse outcomes - `Picker` struct for storing and indexing command-line arguments - `PickerRequirement` struct for declarative parameter definitions - `PickerPattern` family (1..32) of typed pattern structs via the `internal_repeat` proc macro - `req!` proc macro as a succinct builder for `PickerRequirement` Re-export `mingling_picker::macros::*` from the `mingling` crate when the `picker` feature is enabled, replacing the previous wildcard re-export of `mingling_macros`. --- mingling_picker/src/pickable/implements.rs | 43 ++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 mingling_picker/src/pickable/implements.rs (limited to 'mingling_picker/src/pickable') diff --git a/mingling_picker/src/pickable/implements.rs b/mingling_picker/src/pickable/implements.rs new file mode 100644 index 0000000..34f9727 --- /dev/null +++ b/mingling_picker/src/pickable/implements.rs @@ -0,0 +1,43 @@ +use crate::{Pickable, PickerResult}; +use std::path::PathBuf; + +macro_rules! impl_pickable { + ($($t:ty),*) => { + $( + impl Pickable for $t { + fn pick(raw_str: &str) -> PickerResult { + raw_str.parse::<$t>().into() + } + } + )* + }; +} + +impl_pickable!( + i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, usize, f32, f64, isize +); + +impl Pickable for String { + fn pick(raw_str: &str) -> PickerResult { + PickerResult::Parsed(raw_str.to_string()) + } +} + +impl Pickable for PathBuf { + fn pick(raw_str: &str) -> PickerResult { + PickerResult::Parsed(PathBuf::from(raw_str)) + } +} + +impl Pickable for Option { + fn pick(raw_str: &str) -> PickerResult { + if raw_str.is_empty() { + PickerResult::Parsed(None) + } else { + match T::pick(raw_str) { + PickerResult::Parsed(v) => PickerResult::Parsed(Some(v)), + _ => PickerResult::Parsed(None), + } + } + } +} -- cgit