From 78f18ae8f7f45f3a3c9eec75761781dbb5e91189 Mon Sep 17 00:00:00 2001 From: gennyble Date: Wed, 8 Oct 2025 17:16:05 -0500 Subject: macro and int formulas --- src/formula.rs | 76 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 30 deletions(-) (limited to 'src/formula.rs') diff --git a/src/formula.rs b/src/formula.rs index 3e84015..b25d485 100644 --- a/src/formula.rs +++ b/src/formula.rs @@ -49,21 +49,6 @@ impl From<&str> for Formula { } } -impl From for Formula { - fn from(value: UsizeFormula) -> Self { - let UsizeFormula { bounds } = value; - let form = Formula::new("Failed to parse [opt] as an integer"); - - if let Some(bounds) = bounds { - let fail = format!("[opt] must be {bounds}"); - - form.check(fail, move |u| bounds.contains(&u)) - } else { - form - } - } -} - impl From for Formula { fn from(value: F32Formula) -> Self { let F32Formula { bounds } = value; @@ -100,21 +85,6 @@ impl F32Formula { } } -pub struct UsizeFormula { - bounds: Option>, -} - -impl UsizeFormula { - pub fn new() -> Self { - Self { bounds: None } - } - - pub fn bounds>>(mut self, bounds: R) -> Self { - self.bounds = Some(bounds.into()); - self - } -} - pub enum Ranges { Exlusive(Range), Inclusive(RangeInclusive), @@ -190,3 +160,49 @@ impl PathFormula { Self {} } } + +macro_rules! int_formula_impl { + ($meow:ident $woof:ty) => { + pub struct $meow { + bounds: Option>, + } + + impl $meow { + pub fn new() -> Self { + Self { bounds: None } + } + + pub fn bounds>>(mut self, bounds: R) -> Self { + self.bounds = Some(bounds.into()); + self + } + } + + impl From<$meow> for Formula<$woof> { + fn from(value: $meow) -> Self { + let $meow { bounds } = value; + let form = Formula::new("Failed to parse [opt] as an integer"); + + if let Some(bounds) = bounds { + let fail = format!("[opt] must be {bounds}"); + + form.check(fail, move |u| bounds.contains(&u)) + } else { + form + } + } + } + }; +} + +int_formula_impl!(U8Formula u8); +int_formula_impl!(U16Formula u16); +int_formula_impl!(U32Formula u32); +int_formula_impl!(U64Formula u64); +int_formula_impl!(UsizeFormula usize); + +int_formula_impl!(I8Formula i8); +int_formula_impl!(I16Formula i16); +int_formula_impl!(I32Formula i32); +int_formula_impl!(I64Formula i64); +int_formula_impl!(IsizeFormula isize); -- cgit 1.4.1-3-g733a5