about summary refs log tree commit diff
path: root/src/formula/numbers.rs
blob: d7b165f5f54e132f68fd94d82ebbaf07ea8fd4b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use core::fmt;
use std::ops::{Range, RangeBounds, RangeFrom, RangeInclusive, RangeTo, RangeToInclusive};

use crate::formula::Formula;

//MARK: Ranges
pub enum Ranges<T> {
	Exlusive(Range<T>),
	Inclusive(RangeInclusive<T>),
	From(RangeFrom<T>),
	To(RangeTo<T>),
	ToInclusive(RangeToInclusive<T>),
}

impl<T> From<Range<T>> for Ranges<T> {
	fn from(value: Range<T>) -> Self {
		Ranges::Exlusive(value)
	}
}

impl<T> From<RangeFrom<T>> for Ranges<T> {
	fn from(value: RangeFrom<T>) -> Self {
		Ranges::From(value)
	}
}

impl<T> From<RangeTo<T>> for Ranges<T> {
	fn from(value: RangeTo<T>) -> Self {
		Ranges::To(value)
	}
}

impl<T> From<RangeInclusive<T>> for Ranges<T> {
	fn from(value: RangeInclusive<T>) -> Self {
		Ranges::Inclusive(value)
	}
}

impl<T> From<RangeToInclusive<T>> for Ranges<T> {
	fn from(value: RangeToInclusive<T>) -> Self {
		Ranges::ToInclusive(value)
	}
}

impl<T: fmt::Display> fmt::Display for Ranges<T> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Ranges::Exlusive(range) => {
				write!(f, "from {} and less than {}", range.start, range.end)
			}
			Ranges::Inclusive(range) => write!(f, "from {} to {}", range.start(), range.end()),
			Ranges::From(range) => write!(f, "greater than {}", range.start),
			Ranges::To(range) => write!(f, "less than {}", range.end),
			Ranges::ToInclusive(range) => write!(f, "from 0 to {}", range.end),
		}
	}
}

impl<T: PartialEq> Ranges<T> {
	pub fn contains<U>(&self, item: &U) -> bool
	where
		T: PartialOrd<U>,
		U: PartialOrd<T> + ?Sized,
	{
		match self {
			Ranges::Exlusive(range) => range.contains(item),
			Ranges::Inclusive(range) => range.contains(item),
			Ranges::From(range) => range.contains(item),
			Ranges::To(range) => range.contains(item),
			Ranges::ToInclusive(range) => range.contains(item),
		}
	}
}

impl From<F32Formula> for Formula<f32> {
	fn from(value: F32Formula) -> Self {
		let F32Formula { bounds } = value;
		let form = Formula::new("Failed to parse [opt] as a number");

		if let Some(bounds) = bounds {
			let fail = format!("[opt] must be {bounds}");

			form.check(fail, move |u| bounds.contains(&u))
		} else {
			form
		}
	}
}

pub struct F32Formula {
	bounds: Option<Ranges<f32>>,
}

impl F32Formula {
	pub fn new() -> Self {
		Self { bounds: None }
	}

	pub fn bounds<R: Into<Ranges<f32>>>(mut self, bounds: R) -> Self {
		self.bounds = Some(bounds.into());
		self
	}
}

//MARK: Signed/Unsigned Formula impl
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);