| 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 ). |
| 4 |
|
| 5 |
#[diplomat::bridge] |
| 6 |
pub mod ffi { |
| 7 |
use crate::{ |
| 8 |
errors::ffi::ICU4XError, locale::ffi::ICU4XLocale, provider::ffi::ICU4XDataProvider, |
| 9 |
}; |
| 10 |
use alloc::boxed::Box; |
| 11 |
use alloc::string::String; |
| 12 |
use alloc::vec::Vec; |
| 13 |
use icu_list::{ListFormatter, ListLength}; |
| 14 |
use writeable::Writeable; |
| 15 |
|
| 16 |
/// A list of strings |
| 17 |
#[diplomat::opaque] |
| 18 |
#[diplomat::attr(*, disable)] |
| 19 |
pub struct ICU4XList(pub Vec<String>); |
| 20 |
|
| 21 |
impl ICU4XList { |
| 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())) |
| 26 |
} |
| 27 |
|
| 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))) |
| 33 |
} |
| 34 |
|
| 35 |
/// Push a string to the list |
| 36 |
/// |
| 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()); |
| 41 |
} |
| 42 |
|
| 43 |
/// The number of elements in this list |
| 44 |
#[diplomat::attr(supports = accessors, getter = "length")] |
| 45 |
pub fn len(&self) -> usize { |
| 46 |
self.0.len() |
| 47 |
} |
| 48 |
|
| 49 |
/// Whether this list is empty |
| 50 |
#[diplomat::attr(supports = accessors, getter)] |
| 51 |
pub fn is_empty(&self) -> bool { |
| 52 |
self.0.is_empty() |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
#[diplomat::rust_link(icu::list::ListLength, Enum)] |
| 57 |
#[diplomat::enum_convert(ListLength, needs_wildcard)] |
| 58 |
pub enum ICU4XListLength { |
| 59 |
Wide, |
| 60 |
Short, |
| 61 |
Narrow, |
| 62 |
} |
| 63 |
#[diplomat::opaque] |
| 64 |
#[diplomat::rust_link(icu::list::ListFormatter, Struct)] |
| 65 |
pub struct ICU4XListFormatter(pub ListFormatter); |
| 66 |
|
| 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, |
| 73 |
locale: &ICU4XLocale, |
| 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, |
| 81 |
provider, |
| 82 |
&locale, |
| 83 |
length.into() |
| 84 |
)?))) |
| 85 |
} |
| 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, |
| 91 |
locale: &ICU4XLocale, |
| 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, |
| 99 |
provider, |
| 100 |
&locale, |
| 101 |
length.into() |
| 102 |
)?))) |
| 103 |
} |
| 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, |
| 117 |
provider, |
| 118 |
&locale, |
| 119 |
length.into() |
| 120 |
)?))) |
| 121 |
} |
| 122 |
|
| 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)] |
| 127 |
pub fn format( |
| 128 |
&self, |
| 129 |
list: &ICU4XList, |
| 130 |
write: &mut DiplomatWriteable, |
| 131 |
) -> Result<(), ICU4XError> { |
| 132 |
self.0.format(list.0.iter()).write_to(write)?; |
| 133 |
Ok(()) |
| 134 |
} |
| 135 |
|
| 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( |
| 142 |
&self, |
| 143 |
list: &[&str], |
| 144 |
write: &mut DiplomatWriteable, |
| 145 |
) -> Result<(), ICU4XError> { |
| 146 |
self.0.format(list.iter()).write_to(write)?; |
| 147 |
Ok(()) |
| 148 |
} |
| 149 |
|
| 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] |
| 155 |
pub fn format_utf8( |
| 156 |
&self, |
| 157 |
list: &[&DiplomatStr], |
| 158 |
write: &mut DiplomatWriteable, |
| 159 |
) -> Result<(), ICU4XError> { |
| 160 |
self.0 |
| 161 |
.format( |
| 162 |
list.iter() |
| 163 |
.copied() |
| 164 |
.map(crate::utf::PotentiallyInvalidUtf8) |
| 165 |
.map(crate::utf::LossyWrap), |
| 166 |
) |
| 167 |
.write_to(write)?; |
| 168 |
Ok(()) |
| 169 |
} |
| 170 |
|
| 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] |
| 176 |
pub fn format_utf16( |
| 177 |
&self, |
| 178 |
list: &[&DiplomatStr16], |
| 179 |
write: &mut DiplomatWriteable, |
| 180 |
) -> Result<(), ICU4XError> { |
| 181 |
self.0 |
| 182 |
.format( |
| 183 |
list.iter() |
| 184 |
.copied() |
| 185 |
.map(crate::utf::PotentiallyInvalidUtf16) |
| 186 |
.map(crate::utf::LossyWrap), |
| 187 |
) |
| 188 |
.write_to(write)?; |
| 189 |
Ok(()) |
| 190 |
} |
| 191 |
} |
| 192 |
} |