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
pub enum Mistake<T, E> { Fine(T, Vec<E>), Fail(Vec<E>), } impl<T, E> Mistake<T, E> { pub fn to_option(self, errors: &mut Vec<E>) -> Option<T> { match self { Mistake::Fine(val, errs) => { errors.extend(errs); Some(val) } Mistake::Fail(errs) => { errors.extend(errs); None } } } } impl<T, E> From<Result<T, E>> for Mistake<T, E> { fn from(result: Result<T, E>) -> Self { match result { Ok(val) => Mistake::Fine(val, Vec::new()), Err(err) => Mistake::Fail(vec![err]), } } } #[macro_export] macro_rules! attempt { ($mistake:expr, $errors:expr) => { match $mistake.to_option(&mut $errors) { Some(val) => val, None => return crate::Mistake::Fail($errors), } }; } #[macro_export] macro_rules! attempt_res { ($result:expr, $errors:expr) => { match $crate::Mistake::from($result).to_option(&mut $errors) { Some(val) => val, None => return crate::Mistake::Fail($errors), } }; }