third_party_rust_nom/examples/custom_error.rs
Geoffroy Couprie 9cff115667
Ensure all examples compile (#1604)
* docs: Remove unused example

* docs: Ensure all examples compile

Co-authored-by: Ed Page <eopage@gmail.com>
2022-12-30 16:29:22 +01:00

45 lines
832 B
Rust

extern crate nom;
use nom::error::ErrorKind;
use nom::error::ParseError;
use nom::Err::Error;
use nom::IResult;
#[derive(Debug, PartialEq)]
pub enum CustomError<I> {
MyError,
Nom(I, ErrorKind),
}
impl<I> ParseError<I> for CustomError<I> {
fn from_error_kind(input: I, kind: ErrorKind) -> Self {
CustomError::Nom(input, kind)
}
fn append(_: I, _: ErrorKind, other: Self) -> Self {
other
}
}
pub fn parse(_input: &str) -> IResult<&str, &str, CustomError<&str>> {
Err(Error(CustomError::MyError))
}
fn main() {}
#[cfg(test)]
mod tests {
use super::parse;
use super::CustomError;
use nom::Err::Error;
#[test]
fn it_works() {
let err = parse("").unwrap_err();
match err {
Error(e) => assert_eq!(e, CustomError::MyError),
_ => panic!("Unexpected error: {:?}", err),
}
}
}