el_macro/
lib.rs

1#[macro_export]
2macro_rules! bind {
3
4    ($($n: ident)+ = $e: expr, or $f: expr) => {
5        let Ok($($n)+) = $crate::IntoResult::into_result($e) else { $f };
6    };
7
8    ($($n: ident)+ = $e: expr, or $h: expr, $f: expr) => {
9        let $($n)+ = match $crate::IntoResult::into_result($e) {
10            Ok($($n)+) => { $($n)+ },
11            Err(err) => {
12                $h(err);
13                $f
14            },
15        };
16    };
17
18}
19
20
21#[macro_export]
22macro_rules! if_matches {
23
24    ($e: expr, $p: pat => $f: expr) => {
25        if let $p = $e {
26            Some($f())
27        } else {
28            None
29        }
30    };
31
32}
33
34
35pub trait IntoResult {
36
37    type Value;
38    type Error;
39
40    fn into_result(self) -> Result<Self::Value, Self::Error>;
41
42}
43
44
45impl<T> IntoResult for Option<T> {
46
47    type Value = T;
48    type Error = ();
49
50    fn into_result(self) -> Result<Self::Value, Self::Error> {
51        self.ok_or(())
52    }
53
54}
55
56
57impl<T, E> IntoResult for Result<T, E> {
58
59    type Value = T;
60    type Error = E;
61
62    fn into_result(self) -> Result<Self::Value, Self::Error> {
63        self
64    }
65
66}