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
use core::fmt::{self, Display, Formatter};
#[cfg(feature = "std")]
use std::error::Error;

/// 將中文數字轉成數值時發生的錯誤。
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ChineseToNumberError {
    ChineseNumberEmpty,
    ChineseNumberIncorrect { char_index: usize },
    Overflow,
    Underflow,
}

impl Display for ChineseToNumberError {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            ChineseToNumberError::ChineseNumberEmpty => {
                f.write_str("a chinese number cannot be empty")
            },
            ChineseToNumberError::ChineseNumberIncorrect {
                char_index,
            } => f.write_fmt(format_args!(
                "the chinese number is incorrect (position: {})",
                char_index
            )),
            ChineseToNumberError::Overflow => f.write_str("number is too large"),
            ChineseToNumberError::Underflow => f.write_str("number is too small"),
        }
    }
}

#[cfg(feature = "std")]
impl Error for ChineseToNumberError {}