Macro nom::fix_error [] [src]

macro_rules! fix_error {
    ($i:expr, $t:ty, $submac:ident!( $($args:tt)* )) => { ... };
    ($i:expr, $t:ty, $f:expr) => { ... };
}

translate parser result from IResult<I,O,u32> to IResult<I,O,E> with a custom type

    // will add a Custom(42) error to the error chain
    named!(err_test, add_return_error!(ErrorKind::Custom(42u32), tag!("abcd")));

    #[derive(Debug,Clone,PartialEq)]
    pub struct ErrorStr(String);

    // Convert to IResult<&[u8], &[u8], ErrorStr>
    impl From<u32> for ErrorStr {
      fn from(i: u32) -> Self {
        ErrorStr(format!("custom error code: {}", i))
      }
    }

    named!(parser<&[u8], &[u8], ErrorStr>,
        fix_error!(ErrorStr, err_test)
      );

    let a = &b"efghblah"[..];
    assert_eq!(parser(a), Err(Err::Error(Context::Code(a, ErrorKind::Custom(ErrorStr("custom error code: 42".to_string()))))));