alkale 2.0.0

A simple LL(1) lexer library for Rust.
Documentation
//! Container module for numeric errors.

use core::fmt;
use core::{
    error::Error,
    fmt::{Display, Formatter},
    num::ParseFloatError,
};

use num_traits::{CheckedAdd, CheckedMul, Unsigned, Zero};

/// An error that may occur during integer parsing.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum IntegerParseError {
    /// Indicates that the number is well-formed, but the value is outside of the
    /// possible range.
    OutOfRange,
    /// Indicates that the number is not well-formed, 1 or more characters
    /// are not a part of the base.
    InvalidCharacter(char),
}

impl Display for IntegerParseError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::OutOfRange => f.write_str("integer out of range"),
            Self::InvalidCharacter(c) => {
                write!(f, "invalid character '{c}'")
            }
        }
    }
}

impl Error for IntegerParseError {}

/// Represents a successful number parse or an error for both integer and float cases.
#[derive(Debug, Clone, PartialEq)]
#[expect(clippy::exhaustive_enums)]
pub enum ParseNumberResult<
    R: Copy + Zero + CheckedAdd<Output = R> + CheckedMul<Output = R> + From<u8> + Unsigned,
> {
    /// Variant containing the result of an integer parsing.
    Integer(Result<R, IntegerParseError>),
    /// Variant containing the result of a float parsing.
    Float(Result<f64, ParseFloatError>),
}