parsy 0.16.3

An easy-to-use, efficient parser combinators library
Documentation
use crate::{Parser, ParserInput, ParserResult, ParsingError};

/// See [`digit`](`crate::parsers::helpers::digit`)
#[derive(Clone, Copy)]
pub struct Digit {
    radix: u32,
}

impl Digit {
    pub const fn new(radix: u32) -> Self {
        Self { radix }
    }
}

impl Parser<()> for Digit {
    fn parse_inner(&self, input: &mut ParserInput) -> ParserResult<()> {
        let start = input.at();

        let c = input
            .try_eat_char()
            .ok_or_else(|| ParsingError::custom(start.range(0), "No character left"))?;

        if c.data.is_digit(self.radix) {
            Ok(c.forge_here(()))
        } else {
            Err(ParsingError::custom(
                start.range(c.data.len_utf8()),
                "Character filter failed",
            ))
        }
    }
}