validit/
macros.rs

1//! Defines macros for validation check, such as `less!(smaller, greater)`.
2
3use std::fmt::Arguments;
4
5pub fn make_err(fmt: Arguments) -> anyerror::AnyError {
6    anyerror::AnyError::error(format!("{}", fmt))
7}
8
9/// Assert that function call `call(a,b,...)`(up to 8 arguments) to return true, otherwise it return
10/// an error.
11///
12/// For example:
13/// ```
14/// # use std::error::Error;
15/// # use validit::be_true;
16/// fn expect_true(a: u64, b:u64) -> Result<(), Box<dyn Error + 'static>> {
17///     fn le(a: u64, b: u64) -> bool { a <= b }
18///     be_true!(le(a,b));
19///     Ok(())
20/// }
21/// assert!(expect_true(1,2).is_ok());
22/// assert_eq!("expect to be true: le(a(3), b(2))", expect_true(3,2).unwrap_err().to_string());
23/// ```
24///
25/// Another example with 3 arguments:
26/// ```
27/// # use std::error::Error;
28/// # use validit::be_true;
29/// fn expect_true3(a: u64, b:u64) -> Result<(), Box<dyn Error + 'static>> {
30///     fn mid(l: u64, x: u64, r: u64) -> bool { l <= x && x <= r }
31///     be_true!(mid(a,5,b));
32///     Ok(())
33/// }
34/// assert!(expect_true3(1,10).is_ok());
35/// assert_eq!("expect to be true: mid(a(6), 5(5), b(10))", expect_true3(6,10).unwrap_err().to_string());
36/// ```
37#[macro_export]
38macro_rules! be_true {
39    // 0 args
40    ($($call: ident).+()) => {{
41        let __result = $($call).+();
42        if __result {
43            // Ok
44        } else {
45            Err($crate::macros::make_err(format_args!(
46                "expect to be true: {}()",
47                stringify!($($call).+)
48            )))?;
49        }
50    }};
51
52    // 1 args
53    ($($call: ident).+($a: expr)) => {{
54        let __a = $a;
55        let __result = $($call).+(__a);
56        if __result {
57            // Ok
58        } else {
59            Err($crate::macros::make_err(format_args!(
60                "expect to be true: {}({}({:?}))",
61                stringify!($($call).+),
62                stringify!($a),
63                __a,
64            )))?;
65        }
66    }};
67
68    // 2 args
69    ($($call: ident).+($a: expr, $b: expr)) => {{
70        let __a = $a;
71        let __b = $b;
72        let __result = $($call).+(__a, __b);
73        if __result {
74            // Ok
75        } else {
76            Err($crate::macros::make_err(format_args!(
77                "expect to be true: {}({}({:?}), {}({:?}))",
78                stringify!($($call).+),
79                stringify!($a),
80                __a,
81                stringify!($b),
82                __b,
83            )))?;
84        }
85    }};
86
87    // 3 args
88    ($($call: ident).+($a: expr, $b: expr, $c: expr)) => {{
89        let __a = $a;
90        let __b = $b;
91        let __c = $c;
92        let __result = $($call).+(__a, __b, __c);
93        if __result {
94            // Ok
95        } else {
96            Err($crate::macros::make_err(format_args!(
97                "expect to be true: {}({}({:?}), {}({:?}), {}({:?}))",
98                stringify!($($call).+),
99                stringify!($a),
100                __a,
101                stringify!($b),
102                __b,
103                stringify!($c),
104                __c,
105            )))?;
106        }
107    }};
108
109    // 4 args
110    ($($call: ident).+($a: expr, $b: expr, $c: expr, $d: expr)) => {{
111        let __a = $a;
112        let __b = $b;
113        let __c = $c;
114        let __d = $d;
115        let __result = $($call).+(__a, __b, __c, __d);
116        if __result {
117            // Ok
118        } else {
119            Err($crate::macros::make_err(format_args!(
120                "expect to be true: {}({}({:?}), {}({:?}), {}({:?}), {}({:?}))",
121                stringify!($($call).+),
122                stringify!($a),
123                __a,
124                stringify!($b),
125                __b,
126                stringify!($c),
127                __c,
128                stringify!($d),
129                __d,
130            )))?;
131        }
132    }};
133
134    // 5 args
135    ($($call: ident).+($a: expr, $b: expr, $c: expr, $d: expr, $e: expr)) => {{
136        let __a = $a;
137        let __b = $b;
138        let __c = $c;
139        let __d = $d;
140        let __e = $e;
141        let __result = $($call).+(__a, __b, __c, __d, __e);
142        if __result {
143            // Ok
144        } else {
145            Err($crate::macros::make_err(format_args!(
146                "expect to be true: {}({}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}))",
147                stringify!($($call).+),
148                stringify!($a),
149                __a,
150                stringify!($b),
151                __b,
152                stringify!($c),
153                __c,
154                stringify!($d),
155                __d,
156                stringify!($e),
157                __e,
158            )))?;
159        }
160    }};
161
162    // 6 args
163    ($($call: ident).+($a: expr, $b: expr, $c: expr, $d: expr, $e: expr, $f: expr)) => {{
164        let __a = $a;
165        let __b = $b;
166        let __c = $c;
167        let __d = $d;
168        let __e = $e;
169        let __f = $f;
170        let __result = $($call).+(__a, __b, __c, __d, __e, __f);
171        if __result {
172            // Ok
173        } else {
174            Err($crate::macros::make_err(format_args!(
175                "expect to be true: {}({}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}))",
176                stringify!($($call).+),
177                stringify!($a),
178                __a,
179                stringify!($b),
180                __b,
181                stringify!($c),
182                __c,
183                stringify!($d),
184                __d,
185                stringify!($e),
186                __e,
187                stringify!($f),
188                __f,
189            )))?;
190        }
191    }};
192
193    // 7 args
194    ($($call: ident).+($a: expr, $b: expr, $c: expr, $d: expr, $e: expr, $f: expr, $g: expr)) => {{
195        let __a = $a;
196        let __b = $b;
197        let __c = $c;
198        let __d = $d;
199        let __e = $e;
200        let __f = $f;
201        let __g = $g;
202        let __result = $($call).+(__a, __b, __c, __d, __e, __f, __g);
203        if __result {
204            // Ok
205        } else {
206            Err($crate::macros::make_err(format_args!(
207                "expect to be true: {}({}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}))",
208                stringify!($($call).+),
209                stringify!($a),
210                __a,
211                stringify!($b),
212                __b,
213                stringify!($c),
214                __c,
215                stringify!($d),
216                __d,
217                stringify!($e),
218                __e,
219                stringify!($f),
220                __f,
221                stringify!($g),
222                __g,
223            )))?;
224        }
225    }};
226
227    // 8 args
228    ($($call: ident).+($a: expr, $b: expr, $c: expr, $d: expr, $e: expr, $f: expr, $g: expr, $h: expr)) => {{
229        let __a = $a;
230        let __b = $b;
231        let __c = $c;
232        let __d = $d;
233        let __e = $e;
234        let __f = $f;
235        let __g = $g;
236        let __h = $h;
237        let __result = $($call).+(__a, __b, __c, __d, __e, __f, __g, __h);
238        if __result {
239            // Ok
240        } else {
241            Err($crate::macros::make_err(format_args!(
242                "expect to be true: {}({}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}))",
243                stringify!($($call).+),
244                stringify!($a),
245                __a,
246                stringify!($b),
247                __b,
248                stringify!($c),
249                __c,
250                stringify!($d),
251                __d,
252                stringify!($e),
253                __e,
254                stringify!($f),
255                __f,
256                stringify!($g),
257                __g,
258                stringify!($h),
259                __h,
260            )))?;
261        }
262    }};
263}
264
265/// Assert that `a` is less than `b`, otherwise it return an error.
266///
267/// For example:
268/// ```
269/// # use std::error::Error;
270/// # use validit::less;
271/// fn expect_less(a: u64, b:u64) -> Result<(), Box<dyn Error + 'static>> {
272///     less!(a,b);
273///     Ok(())
274/// }
275/// assert!(expect_less(1,2).is_ok());
276/// assert_eq!("expect: a(2) < b(2)", expect_less(2,2).unwrap_err().to_string());
277/// ```
278#[macro_export]
279macro_rules! less {
280    ($a: expr, $b: expr) => {{
281        let a = $a;
282        let b = $b;
283        if (a < b) {
284            // Ok
285        } else {
286            Err($crate::macros::make_err(format_args!(
287                "expect: {}({:?}) {} {}({:?})",
288                stringify!($a),
289                a,
290                "<",
291                stringify!($b),
292                b,
293            )))?;
294        }
295    }};
296}
297
298/// Assert that `a` is greater than `b`, otherwise it return an error.
299///
300/// For example:
301/// ```
302/// # use std::error::Error;
303/// # use validit::greater;
304/// fn expect_greater(a: u64, b:u64) -> Result<(), Box<dyn Error + 'static>> {
305///     greater!(a,b);
306///     Ok(())
307/// }
308/// assert!(expect_greater(2,1).is_ok());
309/// assert_eq!("expect: a(2) > b(2)", expect_greater(2,2).unwrap_err().to_string());
310/// ```
311#[macro_export]
312macro_rules! greater {
313    ($a: expr, $b: expr) => {{
314        let a = $a;
315        let b = $b;
316        if (a > b) {
317            // Ok
318        } else {
319            Err($crate::macros::make_err(format_args!(
320                "expect: {}({:?}) {} {}({:?})",
321                stringify!($a),
322                a,
323                ">",
324                stringify!($b),
325                b,
326            )))?;
327        }
328    }};
329}
330
331/// Assert that `a` is less than or equal to `b`, otherwise it return an error.
332///
333/// For example:
334/// ```
335/// # use std::error::Error;
336/// # use validit::less_equal;
337/// fn expect_less_equal(a: u64, b:u64) -> Result<(), Box<dyn Error + 'static>> {
338///     less_equal!(a,b);
339///     Ok(())
340/// }
341/// assert!(expect_less_equal(2,2).is_ok());
342/// assert_eq!("expect: a(3) <= b(2)", expect_less_equal(3,2).unwrap_err().to_string());
343/// ```
344#[macro_export]
345macro_rules! less_equal {
346    ($a: expr, $b: expr) => {{
347        let a = $a;
348        let b = $b;
349        if (a <= b) {
350            // Ok
351        } else {
352            Err($crate::macros::make_err(format_args!(
353                "expect: {}({:?}) {} {}({:?})",
354                stringify!($a),
355                a,
356                "<=",
357                stringify!($b),
358                b,
359            )))?;
360        }
361    }};
362}
363
364/// Assert that `a` is greater than or equal to `b`, otherwise it return an error.
365///
366/// For example:
367/// ```
368/// # use std::error::Error;
369/// # use validit::greater_equal;
370/// fn expect_greater_equal(a: u64, b:u64) -> Result<(), Box<dyn Error + 'static>> {
371///     greater_equal!(a,b);
372///     Ok(())
373/// }
374/// assert!(expect_greater_equal(2,2).is_ok());
375/// assert_eq!("expect: a(2) >= b(3)", expect_greater_equal(2,3).unwrap_err().to_string());
376/// ```
377#[macro_export]
378macro_rules! greater_equal {
379    ($a: expr, $b: expr) => {{
380        let a = $a;
381        let b = $b;
382        if (a >= b) {
383            // Ok
384        } else {
385            Err($crate::macros::make_err(format_args!(
386                "expect: {}({:?}) {} {}({:?})",
387                stringify!($a),
388                a,
389                ">=",
390                stringify!($b),
391                b,
392            )))?;
393        }
394    }};
395}
396
397/// Assert that `a` equal to `b`, otherwise it return an error.
398///
399/// For example:
400/// ```
401/// # use std::error::Error;
402/// # use validit::equal;
403/// fn expect_equal(a: u64, b:u64) -> Result<(), Box<dyn Error + 'static>> {
404///     equal!(a,b);
405///     Ok(())
406/// }
407/// assert!(expect_equal(2,2).is_ok());
408/// assert_eq!("expect: a(3) == b(2)", expect_equal(3,2).unwrap_err().to_string());
409/// ```
410#[macro_export]
411macro_rules! equal {
412    ($a: expr, $b: expr) => {{
413        let a = $a;
414        let b = $b;
415        if (a == b) {
416            // Ok
417        } else {
418            Err($crate::macros::make_err(format_args!(
419                "expect: {}({:?}) {} {}({:?})",
420                stringify!($a),
421                a,
422                "==",
423                stringify!($b),
424                b,
425            )))?;
426        }
427    }};
428}