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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use super::*;

/// 讓 Rust 程式語言的字串型別擁有將中文數字轉成數值的能力。
pub trait ChineseToNumber<T> {
    /// 將中文數字轉成數值。
    ///
    /// ## 範例
    ///
    /// ```rust
    /// use chinese_number::{ChineseCountMethod, ChineseToNumber};
    ///
    /// assert_eq!(1234567890123456789u64, "一百二十三京四千五百六十七兆八千九百零一億二千三百四十五萬六千七百八十九".to_number(ChineseCountMethod::TenThousand).unwrap());
    /// ```
    fn to_number(&self, method: ChineseCountMethod) -> Result<T, ChineseToNumberError>;

    /// 將中文數字直接轉成數值,不進行單位計算。
    ///
    /// ## 範例
    ///
    /// ```rust
    /// use chinese_number::{ChineseCountMethod, ChineseToNumber};
    ///
    /// assert_eq!(123456789u64, "一二三四五六七八九".to_number_naive().unwrap());
    /// ```
    fn to_number_naive(&self) -> Result<T, ChineseToNumberError>;
}

impl<T: AsRef<str>> ChineseToNumber<u8> for T {
    #[inline]
    fn to_number(&self, _method: ChineseCountMethod) -> Result<u8, ChineseToNumberError> {
        from_chinese_to_u8(self)
    }

    #[inline]
    fn to_number_naive(&self) -> Result<u8, ChineseToNumberError> {
        from_chinese_to_u8_naive(self)
    }
}

impl<T: AsRef<str>> ChineseToNumber<u16> for T {
    #[inline]
    fn to_number(&self, _method: ChineseCountMethod) -> Result<u16, ChineseToNumberError> {
        from_chinese_to_u16(self)
    }

    #[inline]
    fn to_number_naive(&self) -> Result<u16, ChineseToNumberError> {
        from_chinese_to_u16_naive(self)
    }
}

impl<T: AsRef<str>> ChineseToNumber<u32> for T {
    #[inline]
    fn to_number(&self, method: ChineseCountMethod) -> Result<u32, ChineseToNumberError> {
        match method {
            ChineseCountMethod::Low => from_chinese_to_u32_low(self),
            ChineseCountMethod::TenThousand => from_chinese_to_u32_ten_thousand(self),
            ChineseCountMethod::Middle => from_chinese_to_u32_middle(self),
            ChineseCountMethod::High => from_chinese_to_u32_high(self),
        }
    }

    #[inline]
    fn to_number_naive(&self) -> Result<u32, ChineseToNumberError> {
        from_chinese_to_u32_naive(self)
    }
}

impl<T: AsRef<str>> ChineseToNumber<u64> for T {
    #[inline]
    fn to_number(&self, method: ChineseCountMethod) -> Result<u64, ChineseToNumberError> {
        match method {
            ChineseCountMethod::Low => from_chinese_to_u64_low(self),
            ChineseCountMethod::TenThousand => from_chinese_to_u64_ten_thousand(self),
            ChineseCountMethod::Middle => from_chinese_to_u64_middle(self),
            ChineseCountMethod::High => from_chinese_to_u64_high(self),
        }
    }

    #[inline]
    fn to_number_naive(&self) -> Result<u64, ChineseToNumberError> {
        from_chinese_to_u64_naive(self)
    }
}

impl<T: AsRef<str>> ChineseToNumber<u128> for T {
    #[inline]
    fn to_number(&self, method: ChineseCountMethod) -> Result<u128, ChineseToNumberError> {
        match method {
            ChineseCountMethod::Low => from_chinese_to_u128_low(self),
            ChineseCountMethod::TenThousand => from_chinese_to_u128_ten_thousand(self),
            ChineseCountMethod::Middle => from_chinese_to_u128_middle(self),
            ChineseCountMethod::High => from_chinese_to_u128_high(self),
        }
    }

    #[inline]
    fn to_number_naive(&self) -> Result<u128, ChineseToNumberError> {
        from_chinese_to_u128_naive(self)
    }
}

impl<T: AsRef<str>> ChineseToNumber<usize> for T {
    #[inline]
    fn to_number(&self, method: ChineseCountMethod) -> Result<usize, ChineseToNumberError> {
        match method {
            ChineseCountMethod::Low => from_chinese_to_usize_low(self),
            ChineseCountMethod::TenThousand => from_chinese_to_usize_ten_thousand(self),
            ChineseCountMethod::Middle => from_chinese_to_usize_middle(self),
            ChineseCountMethod::High => from_chinese_to_usize_high(self),
        }
    }

    #[inline]
    fn to_number_naive(&self) -> Result<usize, ChineseToNumberError> {
        from_chinese_to_usize_naive(self)
    }
}

impl<T: AsRef<str>> ChineseToNumber<i8> for T {
    #[inline]
    fn to_number(&self, _method: ChineseCountMethod) -> Result<i8, ChineseToNumberError> {
        from_chinese_to_i8(self)
    }

    #[inline]
    fn to_number_naive(&self) -> Result<i8, ChineseToNumberError> {
        from_chinese_to_i8_naive(self)
    }
}

impl<T: AsRef<str>> ChineseToNumber<i16> for T {
    #[inline]
    fn to_number(&self, _method: ChineseCountMethod) -> Result<i16, ChineseToNumberError> {
        from_chinese_to_i16(self)
    }

    #[inline]
    fn to_number_naive(&self) -> Result<i16, ChineseToNumberError> {
        from_chinese_to_i16_naive(self)
    }
}

impl<T: AsRef<str>> ChineseToNumber<i32> for T {
    #[inline]
    fn to_number(&self, method: ChineseCountMethod) -> Result<i32, ChineseToNumberError> {
        match method {
            ChineseCountMethod::Low => from_chinese_to_i32_low(self),
            ChineseCountMethod::TenThousand => from_chinese_to_i32_ten_thousand(self),
            ChineseCountMethod::Middle => from_chinese_to_i32_middle(self),
            ChineseCountMethod::High => from_chinese_to_i32_high(self),
        }
    }

    #[inline]
    fn to_number_naive(&self) -> Result<i32, ChineseToNumberError> {
        from_chinese_to_i32_naive(self)
    }
}

impl<T: AsRef<str>> ChineseToNumber<i64> for T {
    #[inline]
    fn to_number(&self, method: ChineseCountMethod) -> Result<i64, ChineseToNumberError> {
        match method {
            ChineseCountMethod::Low => from_chinese_to_i64_low(self),
            ChineseCountMethod::TenThousand => from_chinese_to_i64_ten_thousand(self),
            ChineseCountMethod::Middle => from_chinese_to_i64_middle(self),
            ChineseCountMethod::High => from_chinese_to_i64_high(self),
        }
    }

    #[inline]
    fn to_number_naive(&self) -> Result<i64, ChineseToNumberError> {
        from_chinese_to_i64_naive(self)
    }
}

impl<T: AsRef<str>> ChineseToNumber<i128> for T {
    #[inline]
    fn to_number(&self, method: ChineseCountMethod) -> Result<i128, ChineseToNumberError> {
        match method {
            ChineseCountMethod::Low => from_chinese_to_i128_low(self),
            ChineseCountMethod::TenThousand => from_chinese_to_i128_ten_thousand(self),
            ChineseCountMethod::Middle => from_chinese_to_i128_middle(self),
            ChineseCountMethod::High => from_chinese_to_i128_high(self),
        }
    }

    #[inline]
    fn to_number_naive(&self) -> Result<i128, ChineseToNumberError> {
        from_chinese_to_i128_naive(self)
    }
}

impl<T: AsRef<str>> ChineseToNumber<isize> for T {
    #[inline]
    fn to_number(&self, method: ChineseCountMethod) -> Result<isize, ChineseToNumberError> {
        match method {
            ChineseCountMethod::Low => from_chinese_to_isize_low(self),
            ChineseCountMethod::TenThousand => from_chinese_to_isize_ten_thousand(self),
            ChineseCountMethod::Middle => from_chinese_to_isize_middle(self),
            ChineseCountMethod::High => from_chinese_to_isize_high(self),
        }
    }

    #[inline]
    fn to_number_naive(&self) -> Result<isize, ChineseToNumberError> {
        from_chinese_to_isize_naive(self)
    }
}

impl<T: AsRef<str>> ChineseToNumber<f32> for T {
    #[inline]
    fn to_number(&self, method: ChineseCountMethod) -> Result<f32, ChineseToNumberError> {
        match method {
            ChineseCountMethod::Low => from_chinese_to_f32_low(self),
            ChineseCountMethod::TenThousand => from_chinese_to_f32_ten_thousand(self),
            ChineseCountMethod::Middle => from_chinese_to_f32_middle(self),
            ChineseCountMethod::High => from_chinese_to_f32_high(self),
        }
    }

    #[inline]
    fn to_number_naive(&self) -> Result<f32, ChineseToNumberError> {
        from_chinese_to_f32_naive(self)
    }
}

impl<T: AsRef<str>> ChineseToNumber<f64> for T {
    #[inline]
    fn to_number(&self, method: ChineseCountMethod) -> Result<f64, ChineseToNumberError> {
        match method {
            ChineseCountMethod::Low => from_chinese_to_f64_low(self),
            ChineseCountMethod::TenThousand => from_chinese_to_f64_ten_thousand(self),
            ChineseCountMethod::Middle => from_chinese_to_f64_middle(self),
            ChineseCountMethod::High => from_chinese_to_f64_high(self),
        }
    }

    #[inline]
    fn to_number_naive(&self) -> Result<f64, ChineseToNumberError> {
        from_chinese_to_f64_naive(self)
    }
}