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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
// 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]
pub mod ffi {
use crate::errors::ffi::ICU4XError;
use alloc::boxed::Box;
use core::str;
use diplomat_runtime::DiplomatWriteable;
use icu_locid::extensions::unicode::Key;
use icu_locid::subtags::{Language, Region, Script};
use icu_locid::Locale;
use writeable::Writeable;
use crate::common::ffi::ICU4XOrdering;
#[diplomat::opaque]
/// An ICU4X Locale, capable of representing strings like `"en-US"`.
#[diplomat::rust_link(icu::locid::Locale, Struct)]
pub struct ICU4XLocale(pub Locale);
impl ICU4XLocale {
/// Construct an [`ICU4XLocale`] from an locale identifier.
///
/// This will run the complete locale parsing algorithm. If code size and
/// performance are critical and the locale is of a known shape (such as
/// `aa-BB`) use `create_und`, `set_language`, `set_script`, and `set_region`.
#[diplomat::rust_link(icu::locid::Locale::try_from_bytes, FnInStruct)]
#[diplomat::rust_link(icu::locid::Locale::from_str, FnInStruct, hidden)]
pub fn create_from_string(name: &str) -> Result<Box<ICU4XLocale>, ICU4XError> {
let name = name.as_bytes(); // #2520
Ok(Box::new(ICU4XLocale(Locale::try_from_bytes(name)?)))
}
/// Construct a default undefined [`ICU4XLocale`] "und".
#[diplomat::rust_link(icu::locid::Locale::UND, AssociatedConstantInStruct)]
pub fn create_und() -> Box<ICU4XLocale> {
Box::new(ICU4XLocale(Locale::UND))
}
/// Clones the [`ICU4XLocale`].
#[diplomat::rust_link(icu::locid::Locale, Struct)]
#[allow(clippy::should_implement_trait)]
pub fn clone(&self) -> Box<ICU4XLocale> {
Box::new(ICU4XLocale(self.0.clone()))
}
/// Write a string representation of the `LanguageIdentifier` part of
/// [`ICU4XLocale`] to `write`.
#[diplomat::rust_link(icu::locid::Locale::id, StructField)]
pub fn basename(
&self,
write: &mut diplomat_runtime::DiplomatWriteable,
) -> Result<(), ICU4XError> {
self.0.id.write_to(write)?;
Ok(())
}
/// Write a string representation of the unicode extension to `write`
#[diplomat::rust_link(icu::locid::Locale::extensions, StructField)]
pub fn get_unicode_extension(
&self,
bytes: &str,
write: &mut diplomat_runtime::DiplomatWriteable,
) -> Result<(), ICU4XError> {
let bytes = bytes.as_bytes(); // #2520
self.0
.extensions
.unicode
.keywords
.get(&Key::try_from_bytes(bytes)?)
.ok_or(ICU4XError::LocaleUndefinedSubtagError)?
.write_to(write)?;
Ok(())
}
/// Write a string representation of [`ICU4XLocale`] language to `write`
#[diplomat::rust_link(icu::locid::Locale::id, StructField)]
pub fn language(
&self,
write: &mut diplomat_runtime::DiplomatWriteable,
) -> Result<(), ICU4XError> {
self.0.id.language.write_to(write)?;
Ok(())
}
/// Set the language part of the [`ICU4XLocale`].
#[diplomat::rust_link(icu::locid::Locale::try_from_bytes, FnInStruct)]
pub fn set_language(&mut self, bytes: &str) -> Result<(), ICU4XError> {
let bytes = bytes.as_bytes(); // #2520
self.0.id.language = if bytes.is_empty() {
Language::UND
} else {
Language::try_from_bytes(bytes)?
};
Ok(())
}
/// Write a string representation of [`ICU4XLocale`] region to `write`
#[diplomat::rust_link(icu::locid::Locale::id, StructField)]
pub fn region(
&self,
write: &mut diplomat_runtime::DiplomatWriteable,
) -> Result<(), ICU4XError> {
if let Some(region) = self.0.id.region {
region.write_to(write)?;
Ok(())
} else {
Err(ICU4XError::LocaleUndefinedSubtagError)
}
}
/// Set the region part of the [`ICU4XLocale`].
#[diplomat::rust_link(icu::locid::Locale::try_from_bytes, FnInStruct)]
pub fn set_region(&mut self, bytes: &str) -> Result<(), ICU4XError> {
let bytes = bytes.as_bytes(); // #2520
self.0.id.region = if bytes.is_empty() {
None
} else {
Some(Region::try_from_bytes(bytes)?)
};
Ok(())
}
/// Write a string representation of [`ICU4XLocale`] script to `write`
#[diplomat::rust_link(icu::locid::Locale::id, StructField)]
pub fn script(
&self,
write: &mut diplomat_runtime::DiplomatWriteable,
) -> Result<(), ICU4XError> {
if let Some(script) = self.0.id.script {
script.write_to(write)?;
Ok(())
} else {
Err(ICU4XError::LocaleUndefinedSubtagError)
}
}
/// Set the script part of the [`ICU4XLocale`]. Pass an empty string to remove the script.
#[diplomat::rust_link(icu::locid::Locale::try_from_bytes, FnInStruct)]
pub fn set_script(&mut self, bytes: &str) -> Result<(), ICU4XError> {
let bytes = bytes.as_bytes(); // #2520
self.0.id.script = if bytes.is_empty() {
None
} else {
Some(Script::try_from_bytes(bytes)?)
};
Ok(())
}
/// Best effort locale canonicalizer that doesn't need any data
///
/// Use ICU4XLocaleCanonicalizer for better control and functionality
#[diplomat::rust_link(icu::locid::Locale::canonicalize, FnInStruct)]
pub fn canonicalize(bytes: &str, write: &mut DiplomatWriteable) -> Result<(), ICU4XError> {
let bytes = bytes.as_bytes(); // #2520
Locale::canonicalize(bytes)?.write_to(write)?;
Ok(())
}
/// Write a string representation of [`ICU4XLocale`] to `write`
#[diplomat::rust_link(icu::locid::Locale::write_to, FnInStruct)]
pub fn to_string(
&self,
write: &mut diplomat_runtime::DiplomatWriteable,
) -> Result<(), ICU4XError> {
self.0.write_to(write)?;
Ok(())
}
#[diplomat::rust_link(icu::locid::Locale::normalizing_eq, FnInStruct)]
pub fn normalizing_eq(&self, other: &str) -> bool {
let other = other.as_bytes(); // #2520
if let Ok(other) = str::from_utf8(other) {
self.0.normalizing_eq(other)
} else {
// invalid UTF8 won't be allowed in locales anyway
false
}
}
#[diplomat::rust_link(icu::locid::Locale::strict_cmp, FnInStruct)]
pub fn strict_cmp(&self, other: &str) -> ICU4XOrdering {
let other = other.as_bytes(); // #2520
self.0.strict_cmp(other).into()
}
/// Deprecated
///
/// Use `create_from_string("en").
#[cfg(feature = "provider_test")]
#[diplomat::attr(dart, disable)]
pub fn create_en() -> Box<ICU4XLocale> {
Box::new(ICU4XLocale(icu_locid::locale!("en")))
}
/// Deprecated
///
/// Use `create_from_string("bn").
#[cfg(feature = "provider_test")]
#[diplomat::attr(dart, disable)]
pub fn create_bn() -> Box<ICU4XLocale> {
Box::new(ICU4XLocale(icu_locid::locale!("bn")))
}
}
}
impl ffi::ICU4XLocale {
pub fn to_datalocale(&self) -> icu_provider::DataLocale {
(&self.0).into()
}
}
|