about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/formula.rs76
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);