icu4x_ecma402/
list.rs

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
// 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 ).

use core::fmt::{self, Display, Write};
use ecma402_traits::listformat::{
    options::{Style, Type},
    Options,
};
use ecma402_traits::Locale;
use writeable::Writeable;

#[derive(Debug)]
pub struct ListFormat(icu::list::ListFormatter);

impl ecma402_traits::listformat::Format for ListFormat {
    type Error = icu_provider::DataError;

    fn try_new<L>(locale: L, opts: Options) -> Result<Self, Self::Error>
    where
        L: Locale,
        Self: Sized,
    {
        #[allow(clippy::unwrap_used)] // ecma402_traits::Locale::to_string is a valid locale
        let locale = icu::locale::Locale::try_from_str(&locale.to_string()).unwrap();

        let prefs = icu::list::ListFormatterPreferences::from(&locale);

        let length = match opts.style {
            Style::Long => icu::list::ListLength::Wide,
            Style::Narrow => icu::list::ListLength::Narrow,
            Style::Short => icu::list::ListLength::Short,
        };
        let options = icu::list::ListFormatterOptions::default().with_length(length);

        Ok(Self(match opts.in_type {
            Type::Conjunction => icu::list::ListFormatter::try_new_and(prefs, options),
            Type::Disjunction => icu::list::ListFormatter::try_new_or(prefs, options),
        }?))
    }

    fn format<I, L, W>(&self, list: L, writer: &mut W) -> fmt::Result
    where
        I: Display,
        L: IntoIterator<Item = I>,
        W: Write,
    {
        struct WriteableWrap<J: Display>(J);

        impl<J: Display> Writeable for WriteableWrap<J> {
            fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
                write!(sink, "{}", self.0)
            }
        }

        let values = list.into_iter().map(WriteableWrap).collect::<Vec<_>>();
        self.0.format(values.iter()).write_to(writer)
    }
}

#[test]
fn test() {
    use ecma402_traits::listformat::Format;

    let mut buf = String::new();

    ListFormat::try_new(
        crate::testing::TestLocale("es"),
        Options {
            in_type: Type::Conjunction,
            style: Style::Long,
        },
    )
    .unwrap()
    .format(["Mallorca", "Ibiza"], &mut buf)
    .unwrap();

    assert_eq!(buf, "Mallorca e Ibiza");
}