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
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

//! Experimental.

use crate::dimension::provider::units::UnitsDisplayNameV1;
use fixed_decimal::SignedFixedDecimal;
use icu_decimal::FixedDecimalFormatter;
use icu_plurals::PluralRules;
use writeable::{impl_display_with_writeable, Writeable};

pub struct FormattedUnit<'l> {
    pub(crate) value: &'l SignedFixedDecimal,
    // TODO: review using options and essentials.
    // pub(crate) _options: &'l UnitsFormatterOptions,
    // pub(crate) essential: &'l UnitsEssentialsV1<'l>,
    pub(crate) display_name: &'l UnitsDisplayNameV1<'l>,
    pub(crate) fixed_decimal_formatter: &'l FixedDecimalFormatter,
    pub(crate) plural_rules: &'l PluralRules,
}

impl Writeable for FormattedUnit<'_> {
    fn write_to_parts<W>(&self, sink: &mut W) -> core::result::Result<(), core::fmt::Error>
    where
        W: writeable::PartsWrite + ?Sized,
    {
        self.display_name
            .patterns
            .get(self.value.into(), self.plural_rules)
            .interpolate((self.fixed_decimal_formatter.format(self.value),))
            .write_to_parts(sink)
    }
}

impl_display_with_writeable!(FormattedUnit<'_>);

#[test]
fn test_basic() {
    use icu_locale_core::locale;
    use writeable::assert_writeable_eq;

    use crate::dimension::units::formatter::UnitsFormatter;
    use crate::dimension::units::options::{UnitsFormatterOptions, Width};

    let test_cases = [
        (
            locale!("en-US"),
            "meter",
            "1",
            UnitsFormatterOptions {
                width: Width::Long,
                ..Default::default()
            },
            "1 meter",
        ),
        (
            locale!("en-US"),
            "meter",
            "12345.67",
            UnitsFormatterOptions::default(),
            "12,345.67 m",
        ),
        (
            locale!("en-US"),
            "century",
            "12345.67",
            UnitsFormatterOptions {
                width: Width::Long,
                ..Default::default()
            },
            "12,345.67 centuries",
        ),
        (
            locale!("de-DE"),
            "meter",
            "12345.67",
            UnitsFormatterOptions::default(),
            "12.345,67 m",
        ),
        (
            locale!("ar-EG"),
            "meter",
            "12345.67",
            UnitsFormatterOptions {
                width: Width::Long,
                ..Default::default()
            },
            "١٢٬٣٤٥٫٦٧ متر",
        ),
    ];

    for (locale, unit, value, options, expected) in test_cases {
        let fmt = UnitsFormatter::try_new(locale.into(), unit, options).unwrap();
        let value = value.parse().unwrap();
        assert_writeable_eq!(fmt.format_fixed_decimal(&value), expected);
    }
}