File: custom_errors.rs

package info (click to toggle)
rust-nom-4 4.2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 820 kB
  • sloc: makefile: 2
file content (36 lines) | stat: -rw-r--r-- 828 bytes parent folder | download | duplicates (4)
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
#![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")
  })
}

#[cfg(feature = "alloc")]
fn test4(input: &str) -> IResult<&str, Vec<&str>, CustomError> {
  count!(input, test1, 4)
}