diff options
author | gennyble <gen@nyble.dev> | 2025-10-08 17:16:05 -0500 |
---|---|---|
committer | gennyble <gen@nyble.dev> | 2025-10-08 17:16:05 -0500 |
commit | 78f18ae8f7f45f3a3c9eec75761781dbb5e91189 (patch) | |
tree | 8e784546b3449ab26646b2d723b34cc60bc9a2ef /src | |
parent | 16f5ed4fc505f0f12946aca5c7514472adbd1fe3 (diff) | |
download | scurvy-78f18ae8f7f45f3a3c9eec75761781dbb5e91189.tar.gz scurvy-78f18ae8f7f45f3a3c9eec75761781dbb5e91189.zip |
macro and int formulas
Diffstat (limited to 'src')
-rw-r--r-- | src/formula.rs | 76 |
1 files changed, 46 insertions, 30 deletions
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<T: FromStr> From<&str> for Formula<T> { } } -impl From<UsizeFormula> for Formula<usize> { - 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<F32Formula> for Formula<f32> { fn from(value: F32Formula) -> Self { let F32Formula { bounds } = value; @@ -100,21 +85,6 @@ impl F32Formula { } } -pub struct UsizeFormula { - bounds: Option<Ranges<usize>>, -} - -impl UsizeFormula { - pub fn new() -> Self { - Self { bounds: None } - } - - pub fn bounds<R: Into<Ranges<usize>>>(mut self, bounds: R) -> Self { - self.bounds = Some(bounds.into()); - self - } -} - pub enum Ranges<T> { Exlusive(Range<T>), Inclusive(RangeInclusive<T>), @@ -190,3 +160,49 @@ impl PathFormula { Self {} } } + +macro_rules! int_formula_impl { + ($meow:ident $woof:ty) => { + pub struct $meow { + bounds: Option<Ranges<$woof>>, + } + + impl $meow { + pub fn new() -> Self { + Self { bounds: None } + } + + pub fn bounds<R: Into<Ranges<$woof>>>(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); |