chinese_format/measure/mod.rs
1mod define;
2mod define_count;
3mod define_multi_register;
4mod define_no_copy;
5
6use crate::{Chinese, ChineseFormat, Variant};
7
8/// Trait describing a [value](Self::value) combined with a [unit](Self::unit) of measurement.
9pub trait Measure {
10 /// The value, convertible to [Chinese].
11 fn value(&self) -> &dyn ChineseFormat;
12
13 /// The unit of measurement, convertible to [Chinese].
14 fn unit(&self) -> &dyn ChineseFormat;
15}
16
17/// [Measure] automatically implements [ChineseFormat],
18/// because its [Chinese] translation is obtained by concatenating
19/// the logograms of its [value](Measure::value) and the logograms of its [unit](Measure::unit).
20///
21/// Furthermore, a [Measure] is [omissible](Chinese::omissible)
22/// if and only if its [value](Measure::value) is omissible.
23///
24/// ```
25/// use chinese_format::{*, length::*};
26///
27/// let three_km = Kilometer::new(3);
28///
29/// assert_eq!(three_km.to_chinese(Variant::Simplified), Chinese {
30/// logograms: "三公里".to_string(),
31/// omissible: false
32/// });
33///
34///
35/// let zero_km = Kilometer::new(0);
36///
37/// assert_eq!(zero_km.to_chinese(Variant::Simplified), Chinese {
38/// logograms: "零公里".to_string(),
39/// omissible: true
40/// });
41/// ```
42impl<T: Measure> ChineseFormat for T {
43 fn to_chinese(&self, variant: Variant) -> Chinese {
44 let value_chinese = self.value().to_chinese(variant);
45
46 let logograms = format!("{}{}", value_chinese, self.unit().to_chinese(variant));
47
48 let omissible = value_chinese.omissible;
49
50 Chinese {
51 logograms,
52 omissible,
53 }
54 }
55}