Skip to main content

byte_unit/
errors.rs

1#[cfg(any(feature = "byte", feature = "bit"))]
2pub use core::num::TryFromIntError;
3use core::{
4    error::Error,
5    fmt::{self, Display, Formatter},
6};
7
8#[cfg(any(feature = "byte", feature = "bit"))]
9use rust_decimal::Decimal;
10
11#[cfg(any(feature = "byte", feature = "bit"))]
12/// The error type returned when it exceeds representation range.
13#[derive(Debug, Copy, Clone, PartialEq, Eq)]
14pub struct ExceededBoundsError;
15
16#[cfg(any(feature = "byte", feature = "bit"))]
17impl Display for ExceededBoundsError {
18    #[inline]
19    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
20        f.write_str("value exceeds the valid range")
21    }
22}
23
24#[cfg(any(feature = "byte", feature = "bit"))]
25impl Error for ExceededBoundsError {}
26
27#[cfg(any(feature = "byte", feature = "bit"))]
28/// The error type returned when parsing values.
29#[derive(Debug, Clone)]
30pub enum ValueParseError {
31    ExceededBounds(Decimal),
32    NotNumber(char),
33    NoValue,
34    NumberTooLong,
35}
36
37#[cfg(any(feature = "byte", feature = "bit"))]
38impl Display for ValueParseError {
39    #[inline]
40    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
41        match self {
42            Self::ExceededBounds(value) => {
43                f.write_fmt(format_args!("the value {value:?} exceeds the valid range"))
44            },
45            Self::NotNumber(c) => f.write_fmt(format_args!("the character {c:?} is not a number")),
46            Self::NoValue => f.write_str("no value can be found"),
47            Self::NumberTooLong => f.write_str("value number is too long"),
48        }
49    }
50}
51
52#[cfg(any(feature = "byte", feature = "bit"))]
53impl Error for ValueParseError {}
54
55/// The error type returned when parsing units.
56#[derive(Debug, Clone)]
57pub struct UnitParseError {
58    pub character:                char,
59    pub expected_characters:      &'static [char],
60    pub also_expect_no_character: bool,
61}
62
63impl Display for UnitParseError {
64    #[inline]
65    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
66        let Self {
67            character,
68            expected_characters,
69            also_expect_no_character,
70        } = self;
71
72        let expected_characters_length = expected_characters.len();
73
74        f.write_fmt(format_args!("the character {character:?} is incorrect",))?;
75
76        if expected_characters_length == 0 {
77            f.write_str(" (no character is expected)")
78        } else {
79            f.write_fmt(format_args!(
80                " ({expected_character:?}",
81                expected_character = expected_characters[0]
82            ))?;
83
84            if expected_characters_length > 1 {
85                for expected_character in
86                    expected_characters[1..].iter().take(expected_characters_length - 2)
87                {
88                    f.write_fmt(format_args!(", {expected_character:?}"))?;
89                }
90
91                f.write_fmt(format_args!(
92                    " or {expected_character:?}",
93                    expected_character = expected_characters[expected_characters_length - 1]
94                ))?;
95            }
96
97            if *also_expect_no_character {
98                f.write_str(" or no character")?;
99            }
100
101            f.write_str(" is expected)")
102        }
103    }
104}
105
106impl Error for UnitParseError {}
107
108#[cfg(any(feature = "byte", feature = "bit"))]
109/// The error type returned when parsing values with a unit.
110#[derive(Debug, Clone)]
111pub enum ParseError {
112    Value(ValueParseError),
113    Unit(UnitParseError),
114}
115
116#[cfg(any(feature = "byte", feature = "bit"))]
117impl From<ValueParseError> for ParseError {
118    #[inline]
119    fn from(error: ValueParseError) -> Self {
120        Self::Value(error)
121    }
122}
123
124#[cfg(any(feature = "byte", feature = "bit"))]
125impl From<UnitParseError> for ParseError {
126    #[inline]
127    fn from(error: UnitParseError) -> Self {
128        Self::Unit(error)
129    }
130}
131
132#[cfg(any(feature = "byte", feature = "bit"))]
133impl Display for ParseError {
134    #[inline]
135    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
136        match self {
137            ParseError::Value(error) => Display::fmt(error, f),
138            ParseError::Unit(error) => Display::fmt(error, f),
139        }
140    }
141}
142
143#[cfg(any(feature = "byte", feature = "bit"))]
144impl Error for ParseError {}