1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
8 errors::ffi::ICU4XError, locale::ffi::ICU4XLocale, provider::ffi::ICU4XDataProvider,
10 use alloc::boxed::Box;
11 use alloc::string::String;
13 use icu_list::{ListFormatter, ListLength};
14 use writeable::Writeable;
18 #[diplomat::attr(*, disable)]
19 pub struct ICU4XList(pub Vec<String>);
22 /// Create a new list of strings
23 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
24 pub fn create() -> Box<ICU4XList> {
25 Box::new(ICU4XList(Vec::new()))
28 /// Create a new list of strings with preallocated space to hold
29 /// at least `capacity` elements
30 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "with_capacity")]
31 pub fn create_with_capacity(capacity: usize) -> Box<ICU4XList> {
32 Box::new(ICU4XList(Vec::with_capacity(capacity)))
35 /// Push a string to the list
37 /// Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according
38 /// to the WHATWG Encoding Standard.
39 pub fn push(&mut self, val: &DiplomatStr) {
40 self.0.push(String::from_utf8_lossy(val).into_owned());
43 /// The number of elements in this list
44 #[diplomat::attr(supports = accessors, getter = "length")]
45 pub fn len(&self) -> usize {
49 /// Whether this list is empty
50 #[diplomat::attr(supports = accessors, getter)]
51 pub fn is_empty(&self) -> bool {
56 #[diplomat::rust_link(icu::list::ListLength, Enum)]
57 #[diplomat::enum_convert(ListLength, needs_wildcard)]
58 pub enum ICU4XListLength {
64 #[diplomat::rust_link(icu::list::ListFormatter, Struct)]
65 pub struct ICU4XListFormatter(pub ListFormatter);
67 impl ICU4XListFormatter {
68 /// Construct a new ICU4XListFormatter instance for And patterns
69 #[diplomat::rust_link(icu::list::ListFormatter::try_new_and_with_length, FnInStruct)]
70 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "and_with_length")]
71 pub fn create_and_with_length(
72 provider: &ICU4XDataProvider,
74 length: ICU4XListLength,
75 ) -> Result<Box<ICU4XListFormatter>, ICU4XError> {
76 let locale = locale.to_datalocale();
77 Ok(Box::new(ICU4XListFormatter(call_constructor!(
78 ListFormatter::try_new_and_with_length,
79 ListFormatter::try_new_and_with_length_with_any_provider,
80 ListFormatter::try_new_and_with_length_with_buffer_provider,
86 /// Construct a new ICU4XListFormatter instance for And patterns
87 #[diplomat::rust_link(icu::list::ListFormatter::try_new_or_with_length, FnInStruct)]
88 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "or_with_length")]
89 pub fn create_or_with_length(
90 provider: &ICU4XDataProvider,
92 length: ICU4XListLength,
93 ) -> Result<Box<ICU4XListFormatter>, ICU4XError> {
94 let locale = locale.to_datalocale();
95 Ok(Box::new(ICU4XListFormatter(call_constructor!(
96 ListFormatter::try_new_or_with_length,
97 ListFormatter::try_new_or_with_length_with_any_provider,
98 ListFormatter::try_new_or_with_length_with_buffer_provider,
104 /// Construct a new ICU4XListFormatter instance for And patterns
105 #[diplomat::rust_link(icu::list::ListFormatter::try_new_unit_with_length, FnInStruct)]
106 #[diplomat::attr(all(supports = constructors, supports = fallible_constructors, supports = named_constructors), named_constructor = "unit_with_length")]
107 pub fn create_unit_with_length(
108 provider: &ICU4XDataProvider,
109 locale: &ICU4XLocale,
110 length: ICU4XListLength,
111 ) -> Result<Box<ICU4XListFormatter>, ICU4XError> {
112 let locale = locale.to_datalocale();
113 Ok(Box::new(ICU4XListFormatter(call_constructor!(
114 ListFormatter::try_new_unit_with_length,
115 ListFormatter::try_new_unit_with_length_with_any_provider,
116 ListFormatter::try_new_unit_with_length_with_buffer_provider,
123 #[diplomat::rust_link(icu::list::ListFormatter::format, FnInStruct)]
124 #[diplomat::rust_link(icu::list::ListFormatter::format_to_string, FnInStruct, hidden)]
125 #[diplomat::rust_link(icu::list::FormattedList, Struct, hidden)]
126 #[diplomat::attr(*, disable)]
130 write: &mut DiplomatWriteable,
131 ) -> Result<(), ICU4XError> {
132 self.0.format(list.0.iter()).write_to(write)?;
136 #[diplomat::rust_link(icu::list::ListFormatter::format, FnInStruct)]
137 #[diplomat::rust_link(icu::list::ListFormatter::format_to_string, FnInStruct, hidden)]
138 #[diplomat::rust_link(icu::list::FormattedList, Struct, hidden)]
139 #[diplomat::attr(dart, disable)]
140 #[diplomat::skip_if_ast]
141 pub fn format_valid_utf8(
144 write: &mut DiplomatWriteable,
145 ) -> Result<(), ICU4XError> {
146 self.0.format(list.iter()).write_to(write)?;
150 #[diplomat::rust_link(icu::list::ListFormatter::format, FnInStruct)]
151 #[diplomat::rust_link(icu::list::ListFormatter::format_to_string, FnInStruct, hidden)]
152 #[diplomat::rust_link(icu::list::FormattedList, Struct, hidden)]
153 #[diplomat::attr(dart, disable)]
154 #[diplomat::skip_if_ast]
157 list: &[&DiplomatStr],
158 write: &mut DiplomatWriteable,
159 ) -> Result<(), ICU4XError> {
164 .map(crate::utf::PotentiallyInvalidUtf8)
165 .map(crate::utf::LossyWrap),
171 #[diplomat::rust_link(icu::list::ListFormatter::format, FnInStruct)]
172 #[diplomat::rust_link(icu::list::ListFormatter::format_to_string, FnInStruct, hidden)]
173 #[diplomat::rust_link(icu::list::FormattedList, Struct, hidden)]
174 #[diplomat::attr(dart, rename = "format")]
175 #[diplomat::skip_if_ast]
178 list: &[&DiplomatStr16],
179 write: &mut DiplomatWriteable,
180 ) -> Result<(), ICU4XError> {
185 .map(crate::utf::PotentiallyInvalidUtf16)
186 .map(crate::utf::LossyWrap),