third_party_rust_nom/tests/custom_errors.rs
2017-12-10 19:50:12 +01:00

36 lines
794 B
Rust

#![allow(dead_code)]
#![cfg_attr(feature = "cargo-clippy", allow(block_in_if_condition_stmt))]
#[macro_use]
extern crate nom;
use nom::IResult;
use nom::digit;
use std::convert::From;
pub struct CustomError(String);
impl From<u32> for CustomError {
fn from(error: u32) -> Self {
CustomError(format!("error code was: {}", error))
}
}
fn test1(input: &str) -> IResult<&str, &str, CustomError> {
fix_error!(input, CustomError, tag!("abcd"))
}
fn test2(input: &str) -> IResult<&str, &str, CustomError> {
terminated!(input, test1, fix_error!(CustomError, digit))
}
fn test3(input: &str) -> IResult<&str, &str, CustomError> {
verify!(input, test1, |s: &str| s.starts_with("abcd"))
}
fn test4(input: &str) -> IResult<&str, Vec<&str>, CustomError> {
count!(input, test1, 4)
}