unicode_intervals/
error.rs1use crate::constants::MAX_CODEPOINT;
2use core::fmt;
3use std::error;
4
5#[derive(Debug, PartialEq)]
7pub enum Error {
8 InvalidCategory(Box<str>),
10 InvalidVersion(Box<str>),
12 InvalidCodepoints(u32, u32),
14 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}