diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-03-30 17:26:03 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-03-30 17:26:03 +0800 |
| commit | 4421fb2794f2af292f8781e7d12ae002a3f10a9b (patch) | |
| tree | 31a358dd80a7e1d3855f4a6281eaf5e010bfb429 /mingling/src/parser/picker | |
| parent | c9e3676e0e2353dc09471c783a3a263dee0fdcb1 (diff) | |
Add argument parser module with picker API
Diffstat (limited to 'mingling/src/parser/picker')
| -rw-r--r-- | mingling/src/parser/picker/builtin.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/mingling/src/parser/picker/builtin.rs b/mingling/src/parser/picker/builtin.rs new file mode 100644 index 0000000..0b1ce08 --- /dev/null +++ b/mingling/src/parser/picker/builtin.rs @@ -0,0 +1,53 @@ +use size::Size; + +use crate::parser::Pickable; + +impl Pickable for String { + type Output = String; + + fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> { + args.pick_argument(flag) + } +} + +macro_rules! impl_pickable_for_number { + ($($t:ty),*) => { + $( + impl Pickable for $t { + type Output = $t; + + fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> { + let Some(picked) = args.pick_argument(flag) else { + return None; + }; + picked.parse().ok() + } + } + )* + }; +} + +impl_pickable_for_number!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, f32, f64); + +impl Pickable for bool { + type Output = bool; + + fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> { + Some(args.pick_flag(flag)) + } +} + +impl Pickable for usize { + type Output = usize; + + fn pick(args: &mut crate::parser::Argument, flag: mingling_core::Flag) -> Option<Self::Output> { + let Some(picked) = args.pick_argument(flag) else { + return None; + }; + let size_parse = Size::from_str(picked.as_str()); + match size_parse { + Ok(size) => Some(size.bytes() as usize), + Err(_) => None, + } + } +} |
