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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
use core::fmt;
use std::{
marker::PhantomData,
ops::{Range, RangeBounds, RangeFrom, RangeInclusive, RangeTo, RangeToInclusive},
path::PathBuf,
str::FromStr,
};
pub struct Formula<T: FromStr> {
pub(crate) check_fn: Option<(String, Box<dyn FnMut(&T) -> bool>)>,
pub(crate) failure: String,
pub(crate) missing: Option<String>,
phantom: PhantomData<T>,
}
impl<T: FromStr> Formula<T> {
pub fn new<S: Into<String>>(failure: S) -> Self {
Self {
check_fn: None,
failure: failure.into(),
missing: None,
phantom: PhantomData::default(),
}
}
pub fn check<S: Into<String>, F>(mut self, fail: S, check: F) -> Self
where
F: FnMut(&T) -> bool + 'static,
{
self.check_fn = Some((fail.into(), Box::new(check)));
self
}
pub fn required<S: Into<String>>(mut self, missing: S) -> Self {
self.missing = Some(missing.into());
self
}
}
impl<T: FromStr> From<String> for Formula<T> {
fn from(value: String) -> Self {
Formula::new(value)
}
}
impl<T: FromStr> From<&str> for Formula<T> {
fn from(value: &str) -> Self {
Formula::new(value)
}
}
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
}
}
}
impl From<PathFormula> for Formula<PathBuf> {
fn from(_value: PathFormula) -> Self {
Formula::new("Failed to parse [opt] as a path")
}
}
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
}
}
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),
}
}
}
pub struct PathFormula;
impl PathFormula {
pub fn new() -> Self {
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);
|