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