konst_macro_rules/
option_macros_.rs

1#[macro_export]
2macro_rules! opt_unwrap {
3    ($e:expr $(,)?) => {
4        match $e {
5            $crate::__::Some(x) => x,
6            $crate::__::None => $crate::utils::panic("invoked `unwrap` macro on a `None` value"),
7        }
8    };
9}
10
11#[macro_export]
12macro_rules! opt_unwrap_or {
13    ($e:expr, $v:expr $(,)?) => {
14        match ($e, $v) {
15            ($crate::__::Some(x), _) => x,
16            ($crate::__::None, value) => value,
17        }
18    };
19}
20
21#[macro_export]
22macro_rules! opt_unwrap_or_else {
23    ($e:expr, || $v:expr $(,)?) => {
24        match $e {
25            $crate::__::Some(x) => x,
26            $crate::__::None => $v,
27        }
28    };
29    ($opt:expr, | $($anything:tt)* ) => {
30        compile_error!("expected the closure to take no arguments")
31    };
32    ($e:expr, $v:expr $(,)?) => {
33        match $e {
34            $crate::__::Some(x) => x,
35            $crate::__::None => $v(),
36        }
37    };
38}
39
40#[macro_export]
41macro_rules! opt_ok_or {
42    ($e:expr, $v:expr $(,)?) => {
43        match ($e, $v) {
44            ($crate::__::Some(x), _) => $crate::__::Ok(x),
45            ($crate::__::None, value) => $crate::__::Err(value),
46        }
47    };
48}
49
50#[macro_export]
51macro_rules! opt_ok_or_else {
52    ($e:expr, || $v:expr $(,)?) => {
53        match $e {
54            $crate::__::Some(x) => $crate::__::Ok(x),
55            $crate::__::None => $crate::__::Err($v),
56        }
57    };
58    ($opt:expr, | $($anything:tt)* ) => {
59        compile_error!("expected the closure to take no arguments")
60    };
61    ($e:expr, $v:expr $(,)?) => {
62        match $e {
63            $crate::__::Some(x) => $crate::__::Ok(x),
64            $crate::__::None => $crate::__::Err($v()),
65        }
66    };
67}
68
69#[macro_export]
70macro_rules! opt_map {
71    ($opt:expr, |$param:pat| $mapper:expr $(,)? ) => {
72        match $opt {
73            $crate::__::Some($param) => $crate::__::Some($mapper),
74            $crate::__::None => $crate::__::None,
75        }
76    };
77    ($opt:expr, | $($anything:tt)* ) => {
78        compile_error!("expected the closure to take a pattern as an argument")
79    };
80    ($opt:expr, $function:path $(,)?) => {
81        match $opt {
82            $crate::__::Some(x) => $crate::__::Some($function(x)),
83            $crate::__::None => $crate::__::None,
84        }
85    };
86}
87
88#[macro_export]
89macro_rules! opt_and_then {
90    ($opt:expr, |$param:pat| $mapper:expr $(,)? ) => {
91        match $opt {
92            $crate::__::Some($param) => $mapper,
93            $crate::__::None => $crate::__::None,
94        }
95    };
96    ($opt:expr, | $($anything:tt)* ) => {
97        compile_error!("expected the closure to take a pattern as an argument")
98    };
99    ($opt:expr, $function:path $(,)?) => {
100        match $opt {
101            $crate::__::Some(x) => $function(x),
102            $crate::__::None => $crate::__::None,
103        }
104    };
105}
106
107#[macro_export]
108macro_rules! opt_flatten {
109    ($opt:expr $(,)? ) => {
110        match $opt {
111            $crate::__::Some(x) => x,
112            $crate::__::None => $crate::__::None,
113        }
114    };
115}
116
117#[macro_export]
118macro_rules! opt_or_else {
119    ($opt:expr, || $mapper:expr $(,)? ) => {
120        match $opt {
121            $crate::__::Some(x) => $crate::__::Some(x),
122            $crate::__::None => $mapper,
123        }
124    };
125    ($opt:expr, | $($anything:tt)* ) => {
126        compile_error!("expected the closure to take no arguments")
127    };
128    ($opt:expr, $function:path $(,)?) => {
129        match $opt {
130            $crate::__::Some(x) => $crate::__::Some(x),
131            $crate::__::None => $function(),
132        }
133    };
134}
135
136#[macro_export]
137macro_rules! opt_filter {
138    ($e:expr, |$param:pat| $v:expr $(,)?) => {
139        match $e {
140            $crate::__::Some(x)
141                if {
142                    let $param = &x;
143                    $v
144                } =>
145            {
146                $crate::__::Some(x)
147            }
148            _ => $crate::__::None,
149        }
150    };
151    ($opt:expr, | $($anything:tt)* ) => {
152        compile_error!("expected the closure to take a pattern as an argument")
153    };
154    ($e:expr, $function:path $(,)?) => {
155        match $e {
156            $crate::__::Some(x) if $function(&x) => $crate::__::Some(x),
157            _ => $crate::__::None,
158        }
159    };
160}