warrant/
lib.rs

1///
2/// Checks a condition.
3/// If the condition is not satisfied, the block of code following the `else` keyword is executed.
4///
5#[macro_export] macro_rules! warrant {
6    ($condition:expr, else $else_block:block) => {
7        if !$condition {
8            $else_block
9        }
10    };
11}
12
13pub use warrant as guard;
14
15
16#[cfg(test)]
17mod macro_tests {
18    use super::*;
19
20    #[test]
21    fn expression_test() {
22        let a = 1;
23        warrant!(a % 2 == 0, else {
24            return
25        });
26        unreachable!();
27    }
28
29    #[test]
30    fn variable_test() {
31        let a = false;
32        warrant!(a, else {
33            return
34        });
35
36        unreachable!();
37    }
38
39    fn some_condition_satisfied() -> bool {
40        false
41    }
42
43    #[test]
44    fn function_test() {
45        warrant!(some_condition_satisfied(), else {
46            return
47        });
48        unreachable!();
49    }
50
51    #[test]
52    fn test_guard() {
53        guard!(some_condition_satisfied(), else {
54            return
55        });
56        unreachable!();
57    }
58}