1pub enum Mistake<T, E> {
2 Fine(T, Vec<E>),
3 Fail(Vec<E>),
4}
5
6impl<T, E> Mistake<T, E> {
7 pub fn to_option(self, errors: &mut Vec<E>) -> Option<T> {
8 match self {
9 Mistake::Fine(val, errs) => {
10 errors.extend(errs);
11 Some(val)
12 }
13 Mistake::Fail(errs) => {
14 errors.extend(errs);
15 None
16 }
17 }
18 }
19}
20
21impl<T, E> From<Result<T, E>> for Mistake<T, E> {
22 fn from(result: Result<T, E>) -> Self {
23 match result {
24 Ok(val) => Mistake::Fine(val, Vec::new()),
25 Err(err) => Mistake::Fail(vec![err]),
26 }
27 }
28}
29
30#[macro_export]
31macro_rules! attempt {
32 ($mistake:expr, $errors:expr) => {
33 match $mistake.to_option(&mut $errors) {
34 Some(val) => val,
35 None => return crate::Mistake::Fail($errors),
36 }
37 };
38}
39
40#[macro_export]
41macro_rules! attempt_res {
42 ($result:expr, $errors:expr) => {
43 match $crate::Mistake::from($result).to_option(&mut $errors) {
44 Some(val) => val,
45 None => return crate::Mistake::Fail($errors),
46 }
47 };
48}