devela/code/result/opt_res/
unwrap.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// devela::code::result::opt_res::unwrap
//
//!
//

/// An unwrapper macro that works in compile-time.
///
/// It supports unwrapping [`Option`], [`Result`] and [`OptRes`][super::OptRes].
#[macro_export]
#[cfg_attr(cargo_primary_package, doc(hidden))]
macro_rules! unwrap {
    (

      // Option<T>
      // ---------

      // Unwraps the contained `Some` value, or otherwise returns `None`.
      some? $value:expr ) => {
        match $value {
            Some(v) => v,
            None => return None,
        }
    };
    (
      // Unwraps the contained `Some` value, or panics if it's `None`.
      some $value:expr) => {
        match $value {
            Some(v) => v,
            None => core::panic![],
        }
    };
    (
      // Unwraps the contained `Some` value, or panics with the given message if it's `None`.
      some_expect $value:expr, $message:literal) => {
        match $value {
            Some(v) => v,
            None => core::panic!["{}", $message],
        }
    };
    (
      // Unwraps the contained `Some` value, or the given default if it's `None`.
      some_or $value:expr, $default:expr) => {
        match $value {
            Some(v) => v,
            None => $default,
        }
    };
    (
      // Transforms the `Option` into a `Result`, mapping `Some(T)` to `Ok(T)`,
      // and `None` to `Err($err)`.
      some_ok_or $value:expr, $err:expr) => {
        match $value {
            Some(v) => Ok(v),
            None => Err($err),
        }
    };
    (
      // Unwraps the contained `Some` value or otherwise returns Err($err).
      some_ok_or? $value:expr, $err:expr) => {
        match $value {
            Some(v) => v,
            None => return Err($err),
        }
    };
    (

      // Result<T, E>
      // ------------

      // Unwraps the contained `Ok` value, or otherwise returns the `Err` value.
      ok? $value:expr ) => {
        match $value {
            Ok(v) => v,
            Err(e) => return Err(e),
        }
    };
    (
      // Unwraps the contained `Ok` value, or panics if it's `Err`.
      ok $value:expr ) => {
        match $value {
            Ok(v) => v,
            Err(_) => core::panic![],
        }
    };
    (
      // Unwraps the contained `Ok` value, or panics with the given message if it's `Err`.
      ok_expect $value:expr, $message:literal) => {
        match $value {
            Ok(v) => v,
            Err(_) => core::panic!["{}", $message],
        }
    };
    (
      // Unwraps the contained `Ok` value, or a provided default if it's `Err`.
      ok_or $value:expr, $default:expr) => {
        match $value {
            Ok(v) => v,
            Err(_) => $default,
        }
    };
    (
      // Transforms the `Result` into an `Option`, mapping `Ok(T)` to `Some(T)`,
      // and `Err(_)` to `None`.
      ok_some $value:expr) => {
        match $value {
            Ok(v) => Some(v),
            Err(_) => None,
        }
    };
    (
      // Unwraps the contained `Ok` value, otherwise returns `None`.
      ok_some? $value:expr) => {
        match $value {
            Ok(v) => v,
            Err(_) => return None,
        }
    };

    (
      // Unwraps the contained `Err` value, or panics if it's `Ok`.
      err $value:expr ) => {
        match $value {
            Ok(_) => core::panic![],
            Err(v) => v,
        }
    };
    (
      // Unwraps the contained `Err` value, or panics the given message if it's `Ok`.
      err_expect $value:expr, $message:literal) => {
        match $value {
            Ok(_) => core::panic!["{}", $message],
            Err(v) => v,
        }
    };
    (
      // Unwraps the contained `Err` value, or a provided default if it's `Ok`.
      err_or $value:expr, $default:expr) => {
        match $value {
            Ok(_) => $default,
            Err(v) => v,
        }
    };
    (
      // Transforms the `Result` into an `Option`, mapping `Err(E)` to `Some(E)`,
      // and `Ok(_)` to `None`.
      err_some $value:expr) => {
        match $value {
            Ok(_) => None,
            Err(e) => Some(e),
        }
    };
    (
      // Unwraps the contained `Err` value, otherwise returns `None`.
      err_some? $value:expr) => {
        match $value {
            Ok(_) => return None,
            Err(e) => e,
        }
    };
    (

      // OptRes<T, E>
      // ------------

      // Unwraps the contained `Some(Ok)` value,
      // or otherwise either returns the `Some(Err)` value or `None`.
      sok? $value:expr ) => {
        match $value {
            Some(Ok(v)) => v,
            Some(Err(e)) => return Some(Err(e)),
            None => return None,
        }
    };
    (
      // Unwraps the contained `Some(Ok)` value,
      // or panics if it's `Some(Err)` or `None`.
      sok $value:expr ) => {
        match $value {
            Some(Ok(v)) => v,
            Some(Err(_)) => core::panic![],
            None => core::panic![],
        }
    };

    (
      // Unwraps the contained `Some(Ok)` value,
      // or panics with the given message if it's `Some(Err)` or `None`.
      sok_expect $value:expr, $message:literal) => {
        match $value {
            Some(Ok(v)) => v,
            Some(Err(_)) => core::panic!["{}", $message],
            None => core::panic!["{}", $message],
        }
    };
    (
      // Unwraps the contained `Some(Ok)` value,
      // or a provided default if it's `Some(Err)` or `None`.
      sok_or $value:expr, $default:expr) => {
        match $value {
            Some(Ok(v)) => v,
            Some(Err(_)) => $default,
            None => $default,
        }
    };
    (
      // Unwraps the contained `Some(Err)` value,
      // or panics if it's `Some(Ok)` or `None`.
      serr $value:expr ) => {
        match $value {
            Some(Ok(_)) => core::panic![],
            Some(Err(v)) => v,
            None => core::panic![],
        }
    };
    (
      // Unwraps the contained `Some(Err)` value,
      // or panics with the given message if it's `Some(Ok)` or `None`.
      serr_expect $value:expr, $message:literal) => {
        match $value {
            Some(Ok(_)) => core::panic!["{}", $message],
            Some(Err(v)) => v,
            None => core::panic!["{}", $message],
        }
    };
    (
      // Unwraps the contained `Some(Err)` value,
      // or a provided default if it's `Some(Ok)` or `None`.
      serr_or $value:expr, $default:expr) => {
        match $value {
            Some(Ok(_)) => $default,
            Some(Err(v)) => v,
            None => $default,
        }
    };
}
#[doc(inline)]
pub use unwrap;

#[cfg(test)]
mod tests {
    #[cfg(feature = "std")]
    use crate::panic_catch;
    use crate::{serr, sok, unwrap, OptRes};

    const OPTION_SOME: Option<bool> = Some(true);
    const OPTION_NONE: Option<bool> = None;

    const RESULT_OK: Result<bool, bool> = Ok(true);
    const RESULT_ERR: Result<bool, bool> = Err(true);

    const OPTRES_OK: OptRes<bool, bool> = sok(true);
    const OPTRES_ERR: OptRes<bool, bool> = serr(true);
    const OPTRES_NONE: OptRes<bool, bool> = None;

    #[test]
    fn test_unwrap_option() {
        assert![unwrap![some OPTION_SOME]];
        assert![unwrap![some_expect OPTION_SOME, "ERR"]];
        assert_eq![unwrap![some_or OPTION_SOME, false], true];
        assert_eq![unwrap![some_or OPTION_NONE, false], false];
    }
    #[test] #[cfg(feature = "std")] #[rustfmt::skip]
    fn test_unwrap_option_panic() {
        assert![panic_catch(|| { assert![unwrap![some OPTION_NONE]] }).is_err()];
        assert![panic_catch(|| { assert![unwrap![some_expect OPTION_NONE, "ERR"]] }).is_err()];
    }

    #[test]
    fn test_unwrap_result() {
        assert![unwrap![ok RESULT_OK]];
        assert![unwrap![ok_expect RESULT_OK, "ERR"]];
        assert_eq![unwrap![ok_or RESULT_OK, false], true];
        assert_eq![unwrap![ok_or RESULT_ERR, false], false];

        assert![unwrap![err RESULT_ERR]];
        assert![unwrap![err_expect RESULT_ERR, "ERR"]];
        assert_eq![unwrap![err_or RESULT_ERR, false], true];
        assert_eq![unwrap![err_or RESULT_OK, false], false];
    }
    #[test] #[cfg(feature = "std")] #[rustfmt::skip]
    fn test_unwrap_result_panic() {
        assert![panic_catch(|| { assert![unwrap![ok RESULT_ERR]] }).is_err()];
        assert![panic_catch(|| { assert![unwrap![ok_expect RESULT_ERR, "ERR"]] }).is_err()];

        assert![panic_catch(|| { assert![unwrap![err RESULT_OK]] }).is_err()];
        assert![panic_catch(|| { assert![unwrap![err_expect RESULT_OK, "ERR"]] }).is_err()];
    }

    #[test]
    fn test_unwrap_optres() {
        assert![unwrap![sok OPTRES_OK]];
        assert![unwrap![sok_expect OPTRES_OK, "ERR"]];
        assert_eq![unwrap![sok_or OPTRES_OK, false], true];
        assert_eq![unwrap![sok_or OPTRES_ERR, false], false];
        assert_eq![unwrap![sok_or OPTRES_NONE, false], false];

        assert![unwrap![serr OPTRES_ERR]];
        assert![unwrap![serr_expect OPTRES_ERR, "ERR"]];
        assert_eq![unwrap![serr_or OPTRES_ERR, false], true];
        assert_eq![unwrap![serr_or OPTRES_OK, false], false];
        assert_eq![unwrap![serr_or OPTRES_NONE, false], false];
    }
    #[test] #[cfg(feature = "std")] #[rustfmt::skip]
    fn test_unwrap_optres_panic() {
        assert![panic_catch(|| { assert![unwrap![sok OPTRES_ERR]] }).is_err()];
        assert![panic_catch(|| { assert![unwrap![sok OPTRES_NONE]] }).is_err()];
        assert![panic_catch(|| { assert![unwrap![sok_expect OPTRES_ERR, "ERR"]] }).is_err()];
        assert![panic_catch(|| { assert![unwrap![sok_expect OPTRES_NONE, "ERR"]] }).is_err()];

        assert![panic_catch(|| { assert![unwrap![serr OPTRES_OK]] }).is_err()];
        assert![panic_catch(|| { assert![unwrap![serr OPTRES_NONE]] }).is_err()];
        assert![panic_catch(|| { assert![unwrap![serr_expect OPTRES_OK, "ERR"]] }).is_err()];
        assert![panic_catch(|| { assert![unwrap![serr_expect OPTRES_NONE, "ERR"]] }).is_err()];
    }
}