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
37
use crate::constants::MAX_CODEPOINT;
use core::fmt;
use std::error;

/// Errors during Unicode intervals manipulations.
#[derive(Debug, PartialEq)]
pub enum Error {
    /// Provided category name is invalid.
    InvalidCategory(Box<str>),
    /// Provided Unicode version is invalid.
    InvalidVersion(Box<str>),
    /// Provided codepoints do not agree. Maximum should be greater or equal to minimum.
    InvalidCodepoints(u32, u32),
    /// Codepoint is not in the allowed range.
    CodepointNotInRange(u32, u32),
}

impl error::Error for Error {}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::InvalidCategory(category) => f.write_fmt(format_args!(
                "'{category}' is not a valid Unicode category"
            )),
            Error::InvalidVersion(version) => {
                f.write_fmt(format_args!("'{version}' is not a valid Unicode version"))
            }
            Error::InvalidCodepoints(minimum, maximum) => f.write_fmt(format_args!(
                "Minimum codepoint should be less or equal than maximum codepoint. Got {minimum} < {maximum}"
            )),
            Error::CodepointNotInRange(minimum, maximum) => f.write_fmt(format_args!(
                "Codepoints should be in [0; {MAX_CODEPOINT}] range. Got: [{minimum}; {maximum}]"
            )),
        }
    }
}