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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use core::fmt::{self, Display, Formatter};
#[cfg(any(feature = "byte", feature = "bit"))]
pub use core::num::TryFromIntError;
#[cfg(feature = "std")]
use std::error::Error;

#[cfg(any(feature = "byte", feature = "bit"))]
use rust_decimal::Decimal;

#[cfg(any(feature = "byte", feature = "bit"))]
/// The error type returned when it exceeds representation range.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ExceededBoundsError;

#[cfg(any(feature = "byte", feature = "bit"))]
impl Display for ExceededBoundsError {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str("value exceeds the valid range")
    }
}

#[cfg(any(feature = "byte", feature = "bit"))]
#[cfg(feature = "std")]
impl Error for ExceededBoundsError {}

#[cfg(any(feature = "byte", feature = "bit"))]
/// The error type returned when parsing values.
#[derive(Debug, Clone)]
pub enum ValueParseError {
    ExceededBounds(Decimal),
    NotNumber(char),
    NoValue,
    NumberTooLong,
}

#[cfg(any(feature = "byte", feature = "bit"))]
impl Display for ValueParseError {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::ExceededBounds(value) => {
                f.write_fmt(format_args!("the value {value:?} exceeds the valid range"))
            },
            Self::NotNumber(c) => f.write_fmt(format_args!("the character {c:?} is not a number")),
            Self::NoValue => f.write_str("no value can be found"),
            Self::NumberTooLong => f.write_str("value number is too long"),
        }
    }
}

#[cfg(any(feature = "byte", feature = "bit"))]
#[cfg(feature = "std")]
impl Error for ValueParseError {}

/// The error type returned when parsing units.
#[derive(Debug, Clone)]
pub struct UnitParseError {
    pub character:                char,
    pub expected_characters:      &'static [char],
    pub also_expect_no_character: bool,
}

impl Display for UnitParseError {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let Self {
            character,
            expected_characters,
            also_expect_no_character,
        } = self;

        let expected_characters_length = expected_characters.len();

        f.write_fmt(format_args!("the character {character:?} is incorrect",))?;

        if expected_characters_length == 0 {
            f.write_str(" (no character is expected)")
        } else {
            f.write_fmt(format_args!(
                " ({expected_character:?}",
                expected_character = expected_characters[0]
            ))?;

            if expected_characters_length > 1 {
                for expected_character in
                    expected_characters[1..].iter().take(expected_characters_length - 2)
                {
                    f.write_fmt(format_args!(", {expected_character:?}"))?;
                }

                if *also_expect_no_character {
                    f.write_fmt(format_args!(
                        ", {expected_character:?} or no character",
                        expected_character = expected_characters[expected_characters_length - 1]
                    ))?;
                } else {
                    f.write_fmt(format_args!(
                        " or {expected_character:?} is expected)",
                        expected_character = expected_characters[expected_characters_length - 1]
                    ))?;
                }
            }

            if *also_expect_no_character {
                f.write_str(" or no character")?;
            }

            f.write_str(" is expected)")
        }
    }
}

#[cfg(any(feature = "byte", feature = "bit"))]
#[cfg(feature = "std")]
impl Error for UnitParseError {}

#[cfg(any(feature = "byte", feature = "bit"))]
/// The error type returned when parsing values with a unit.
#[derive(Debug, Clone)]
pub enum ParseError {
    Value(ValueParseError),
    Unit(UnitParseError),
}

#[cfg(any(feature = "byte", feature = "bit"))]
impl From<ValueParseError> for ParseError {
    #[inline]
    fn from(error: ValueParseError) -> Self {
        Self::Value(error)
    }
}

#[cfg(any(feature = "byte", feature = "bit"))]
impl From<UnitParseError> for ParseError {
    #[inline]
    fn from(error: UnitParseError) -> Self {
        Self::Unit(error)
    }
}

#[cfg(any(feature = "byte", feature = "bit"))]
impl Display for ParseError {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            ParseError::Value(error) => Display::fmt(error, f),
            ParseError::Unit(error) => Display::fmt(error, f),
        }
    }
}

#[cfg(any(feature = "byte", feature = "bit"))]
#[cfg(feature = "std")]
impl Error for ParseError {}