diff --git a/Cargo.toml b/Cargo.toml index d27cc1b..052dd6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "abortable_parser" -version = "0.2.1" +version = "0.2.2" authors = ["Jeremy Wall "] description = "A parser combinator library with an emphasis on error handling" repository = "https://github.com/zaphar/abortable_parser" diff --git a/src/combinators.rs b/src/combinators.rs index ea5fd0c..fe3c071 100644 --- a/src/combinators.rs +++ b/src/combinators.rs @@ -166,6 +166,28 @@ macro_rules! must { }; } +#[macro_export] +/// Replaces the the sub error in a Fail case with one of your own errors. +macro_rules! with_err { + ($i:expr, $f:ident!( $( $args:tt )* ), $e:expr) => {{ + let _i = $i.clone(); + match $f!($i, $($args)*) { + $crate::Result::Complete(i, o) => $crate::Result::Complete(i, o), + $crate::Result::Incomplete(ctx) => $crate::Result::Incomplete(ctx), + $crate::Result::Fail(e) => $crate::Result::Fail($crate::Error::new($e, Box::new(_i.clone()))), + $crate::Result::Abort(e) => $crate::Result::Abort($crate::Error::new($e, Box::new(_i.clone()))), + } + }}; + + ($i:expr, $f:ident( $( $args:tt )* ), $e:expr ) => { + with_err!($i, run!($f($($args)*)), $e:expr) + }; + + ($i:expr, $f:ident, $e:expr) => { + with_err!($i, run!($f), $e) + }; +} + /// Wraps any Error return from a subparser in another error. Stores the position at /// this point in the parse tree allowing you to associate context with wrapped errors. #[macro_export]