unicode_intervals/
error.rs

1use crate::constants::MAX_CODEPOINT;
2use core::fmt;
3use std::error;
4
5/// Errors during Unicode intervals manipulations.
6#[derive(Debug, PartialEq)]
7pub enum Error {
8    /// Provided category name is invalid.
9    InvalidCategory(Box<str>),
10    /// Provided Unicode version is invalid.
11    InvalidVersion(Box<str>),
12    /// Provided codepoints do not agree. Maximum should be greater or equal to minimum.
13    InvalidCodepoints(u32, u32),
14    /// Codepoint is not in the allowed range.
15    CodepointNotInRange(u32, u32),
16}
17
18impl error::Error for Error {}
19
20impl fmt::Display for Error {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            Error::InvalidCategory(category) => f.write_fmt(format_args!(
24                "'{category}' is not a valid Unicode category"
25            )),
26            Error::InvalidVersion(version) => {
27                f.write_fmt(format_args!("'{version}' is not a valid Unicode version"))
28            }
29            Error::InvalidCodepoints(minimum, maximum) => f.write_fmt(format_args!(
30                "Minimum codepoint should be less or equal than maximum codepoint. Got {minimum} < {maximum}"
31            )),
32            Error::CodepointNotInRange(minimum, maximum) => f.write_fmt(format_args!(
33                "Codepoints should be in [0; {MAX_CODEPOINT}] range. Got: [{minimum}; {maximum}]"
34            )),
35        }
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn test_error_traits() {
45        let error = Error::InvalidCodepoints(1, 1);
46        assert_eq!(error, error);
47        assert_eq!(format!("{error:?}"), "InvalidCodepoints(1, 1)");
48    }
49}