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

#[diplomat::bridge]
#[diplomat::abi_rename = "icu4x_{0}_mv1"]
#[diplomat::attr(auto, namespace = "icu4x")]
pub mod ffi {
    use alloc::boxed::Box;
    use diplomat_runtime::{DiplomatStr16Slice, DiplomatStrSlice};
    #[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
    use icu_list::{ListFormatterOptions, ListFormatterPreferences};

    #[cfg(feature = "buffer_provider")]
    use crate::provider::ffi::DataProvider;
    #[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
    use crate::{errors::ffi::DataError, locale_core::ffi::Locale};

    use writeable::Writeable;

    #[diplomat::rust_link(icu::list::ListLength, Enum)]
    #[diplomat::enum_convert(icu_list::ListLength, needs_wildcard)]
    pub enum ListLength {
        Wide,
        Short,
        Narrow,
    }
    #[diplomat::opaque]
    #[diplomat::rust_link(icu::list::ListFormatter, Struct)]
    pub struct ListFormatter(pub icu_list::ListFormatter);

    impl ListFormatter {
        /// Construct a new ListFormatter instance for And patterns from compiled data.
        #[diplomat::rust_link(icu::list::ListFormatter::try_new_and, FnInStruct)]
        #[diplomat::attr(supports = fallible_constructors, named_constructor = "and_with_length")]
        #[cfg(feature = "compiled_data")]
        #[diplomat::demo(default_constructor)]
        pub fn create_and_with_length(
            locale: &Locale,
            length: ListLength,
        ) -> Result<Box<ListFormatter>, DataError> {
            let prefs = ListFormatterPreferences::from(&locale.0);
            let options = ListFormatterOptions::default().with_length(length.into());
            Ok(Box::new(ListFormatter(
                icu_list::ListFormatter::try_new_and(prefs, options)?,
            )))
        }

        /// Construct a new ListFormatter instance for And patterns
        #[diplomat::rust_link(icu::list::ListFormatter::try_new_and, FnInStruct)]
        #[diplomat::attr(supports = fallible_constructors, named_constructor = "and_with_length_and_provider")]
        #[cfg(feature = "buffer_provider")]
        pub fn create_and_with_length_and_provider(
            provider: &DataProvider,
            locale: &Locale,
            length: ListLength,
        ) -> Result<Box<ListFormatter>, DataError> {
            let prefs = ListFormatterPreferences::from(&locale.0);
            let options = ListFormatterOptions::default().with_length(length.into());
            Ok(Box::new(ListFormatter(provider.call_constructor(
                move |provider| {
                    icu_list::ListFormatter::try_new_and_with_buffer_provider(
                        provider, prefs, options,
                    )
                },
            )?)))
        }

        /// Construct a new ListFormatter instance for And patterns from compiled data.
        #[diplomat::rust_link(icu::list::ListFormatter::try_new_or, FnInStruct)]
        #[diplomat::attr(supports = fallible_constructors, named_constructor = "or_with_length")]
        #[cfg(feature = "compiled_data")]
        pub fn create_or_with_length(
            locale: &Locale,
            length: ListLength,
        ) -> Result<Box<ListFormatter>, DataError> {
            let prefs = ListFormatterPreferences::from(&locale.0);
            let options = ListFormatterOptions::default().with_length(length.into());
            Ok(Box::new(ListFormatter(
                icu_list::ListFormatter::try_new_or(prefs, options)?,
            )))
        }

        /// Construct a new ListFormatter instance for And patterns
        #[diplomat::rust_link(icu::list::ListFormatter::try_new_or, FnInStruct)]
        #[diplomat::attr(supports = fallible_constructors, named_constructor = "or_with_length_and_provider")]
        #[cfg(feature = "buffer_provider")]
        pub fn create_or_with_length_and_provider(
            provider: &DataProvider,
            locale: &Locale,
            length: ListLength,
        ) -> Result<Box<ListFormatter>, DataError> {
            let prefs = ListFormatterPreferences::from(&locale.0);
            let options = ListFormatterOptions::default().with_length(length.into());
            Ok(Box::new(ListFormatter(provider.call_constructor(
                move |provider| {
                    icu_list::ListFormatter::try_new_or_with_buffer_provider(
                        provider, prefs, options,
                    )
                },
            )?)))
        }

        /// Construct a new ListFormatter instance for And patterns from compiled data.
        #[diplomat::rust_link(icu::list::ListFormatter::try_new_unit, FnInStruct)]
        #[diplomat::attr(supports = fallible_constructors, named_constructor = "unit_with_length")]
        #[cfg(feature = "compiled_data")]
        pub fn create_unit_with_length(
            locale: &Locale,
            length: ListLength,
        ) -> Result<Box<ListFormatter>, DataError> {
            let prefs = ListFormatterPreferences::from(&locale.0);
            let options = ListFormatterOptions::default().with_length(length.into());
            Ok(Box::new(ListFormatter(
                icu_list::ListFormatter::try_new_unit(prefs, options)?,
            )))
        }

        /// Construct a new ListFormatter instance for And patterns
        #[diplomat::rust_link(icu::list::ListFormatter::try_new_unit, FnInStruct)]
        #[diplomat::attr(supports = fallible_constructors, named_constructor = "unit_with_length_and_provider")]
        #[cfg(feature = "buffer_provider")]
        pub fn create_unit_with_length_and_provider(
            provider: &DataProvider,
            locale: &Locale,
            length: ListLength,
        ) -> Result<Box<ListFormatter>, DataError> {
            let prefs = ListFormatterPreferences::from(&locale.0);
            let options = ListFormatterOptions::default().with_length(length.into());
            Ok(Box::new(ListFormatter(provider.call_constructor(
                move |provider| {
                    icu_list::ListFormatter::try_new_unit_with_buffer_provider(
                        provider, prefs, options,
                    )
                },
            )?)))
        }

        #[diplomat::rust_link(icu::list::ListFormatter::format, FnInStruct)]
        #[diplomat::rust_link(icu::list::ListFormatter::format_to_string, FnInStruct, hidden)]
        #[diplomat::rust_link(icu::list::FormattedList, Struct, hidden)]
        #[diplomat::attr(not(supports = utf8_strings), disable)]
        #[diplomat::attr(*, rename = "format")]
        pub fn format_utf8(&self, list: &[DiplomatStrSlice], write: &mut DiplomatWrite) {
            let _infallible = self
                .0
                .format(
                    list.iter()
                        .map(|a| potential_utf::PotentialUtf8::from_bytes(a))
                        .map(writeable::adapters::LossyWrap),
                )
                .write_to(write);
        }

        #[diplomat::rust_link(icu::list::ListFormatter::format, FnInStruct)]
        #[diplomat::rust_link(icu::list::ListFormatter::format_to_string, FnInStruct, hidden)]
        #[diplomat::rust_link(icu::list::FormattedList, Struct, hidden)]
        #[diplomat::attr(not(supports = utf8_strings), rename = "format")]
        #[diplomat::attr(supports = utf8_strings, rename = "format16")]
        pub fn format_utf16(&self, list: &[DiplomatStr16Slice], write: &mut DiplomatWrite) {
            let _infallible = self
                .0
                .format(
                    list.iter()
                        .map(|a| potential_utf::PotentialUtf16::from_slice(a))
                        .map(writeable::adapters::LossyWrap),
                )
                .write_to(write);
        }
    }
}