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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
// 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 icu_segmenter::LineBreakOptions;
use icu_segmenter::LineBreakStrictness;
use icu_segmenter::LineBreakWordOption;
#[diplomat::bridge]
pub mod ffi {
use crate::errors::ffi::ICU4XError;
use crate::provider::ffi::ICU4XDataProvider;
use alloc::boxed::Box;
use core::convert::TryFrom;
use icu_segmenter::{
LineBreakIteratorLatin1, LineBreakIteratorPotentiallyIllFormedUtf8, LineBreakIteratorUtf16,
LineSegmenter,
};
#[diplomat::opaque]
/// An ICU4X line-break segmenter, capable of finding breakpoints in strings.
#[diplomat::rust_link(icu::segmenter::LineSegmenter, Struct)]
pub struct ICU4XLineSegmenter(LineSegmenter);
#[diplomat::rust_link(icu::segmenter::LineBreakStrictness, Enum)]
pub enum ICU4XLineBreakStrictness {
Loose,
Normal,
Strict,
Anywhere,
}
#[diplomat::rust_link(icu::segmenter::LineBreakWordOption, Enum)]
pub enum ICU4XLineBreakWordOption {
Normal,
BreakAll,
KeepAll,
}
#[diplomat::rust_link(icu::segmenter::LineBreakOptions, Struct)]
pub struct ICU4XLineBreakOptionsV1 {
pub strictness: ICU4XLineBreakStrictness,
pub word_option: ICU4XLineBreakWordOption,
pub ja_zh: bool,
}
#[diplomat::opaque]
#[diplomat::rust_link(icu::segmenter::LineBreakIterator, Struct)]
#[diplomat::rust_link(
icu::segmenter::LineBreakIteratorPotentiallyIllFormedUtf8,
Typedef,
compact
)]
pub struct ICU4XLineBreakIteratorUtf8<'a>(LineBreakIteratorPotentiallyIllFormedUtf8<'a, 'a>);
#[diplomat::opaque]
#[diplomat::rust_link(icu::segmenter::LineBreakIterator, Struct)]
#[diplomat::rust_link(icu::segmenter::LineBreakIteratorUtf16, Typedef, compact)]
pub struct ICU4XLineBreakIteratorUtf16<'a>(LineBreakIteratorUtf16<'a, 'a>);
#[diplomat::opaque]
#[diplomat::rust_link(icu::segmenter::LineBreakIterator, Struct)]
#[diplomat::rust_link(icu::segmenter::LineBreakIteratorLatin1, Typedef, compact)]
pub struct ICU4XLineBreakIteratorLatin1<'a>(LineBreakIteratorLatin1<'a, 'a>);
impl ICU4XLineSegmenter {
/// Construct a [`ICU4XLineSegmenter`] with default options. It automatically loads the best
/// available payload data for Burmese, Khmer, Lao, and Thai.
#[diplomat::rust_link(icu::segmenter::LineSegmenter::new_auto, FnInStruct)]
pub fn create_auto(
provider: &ICU4XDataProvider,
) -> Result<Box<ICU4XLineSegmenter>, ICU4XError> {
Ok(Box::new(ICU4XLineSegmenter(call_constructor!(
LineSegmenter::new_auto [r => Ok(r)],
LineSegmenter::try_new_auto_with_any_provider,
LineSegmenter::try_new_auto_with_buffer_provider,
provider
)?)))
}
/// Construct a [`ICU4XLineSegmenter`] with default options and LSTM payload data for
/// Burmese, Khmer, Lao, and Thai.
#[diplomat::rust_link(icu::segmenter::LineSegmenter::new_lstm, FnInStruct)]
pub fn create_lstm(
provider: &ICU4XDataProvider,
) -> Result<Box<ICU4XLineSegmenter>, ICU4XError> {
Ok(Box::new(ICU4XLineSegmenter(call_constructor!(
LineSegmenter::new_lstm [r => Ok(r)],
LineSegmenter::try_new_lstm_with_any_provider,
LineSegmenter::try_new_lstm_with_buffer_provider,
provider,
)?)))
}
/// Construct a [`ICU4XLineSegmenter`] with default options and dictionary payload data for
/// Burmese, Khmer, Lao, and Thai..
#[diplomat::rust_link(icu::segmenter::LineSegmenter::new_dictionary, FnInStruct)]
pub fn create_dictionary(
provider: &ICU4XDataProvider,
) -> Result<Box<ICU4XLineSegmenter>, ICU4XError> {
Ok(Box::new(ICU4XLineSegmenter(call_constructor!(
LineSegmenter::new_dictionary [r => Ok(r)],
LineSegmenter::try_new_dictionary_with_any_provider,
LineSegmenter::try_new_dictionary_with_buffer_provider,
provider,
)?)))
}
/// Construct a [`ICU4XLineSegmenter`] with custom options. It automatically loads the best
/// available payload data for Burmese, Khmer, Lao, and Thai.
#[diplomat::rust_link(icu::segmenter::LineSegmenter::new_auto_with_options, FnInStruct)]
pub fn create_auto_with_options_v1(
provider: &ICU4XDataProvider,
options: ICU4XLineBreakOptionsV1,
) -> Result<Box<ICU4XLineSegmenter>, ICU4XError> {
Ok(Box::new(ICU4XLineSegmenter(call_constructor!(
LineSegmenter::new_auto_with_options [r => Ok(r)],
LineSegmenter::try_new_auto_with_options_with_any_provider,
LineSegmenter::try_new_auto_with_options_with_buffer_provider,
provider,
options.into(),
)?)))
}
/// Construct a [`ICU4XLineSegmenter`] with custom options and LSTM payload data for
/// Burmese, Khmer, Lao, and Thai.
#[diplomat::rust_link(icu::segmenter::LineSegmenter::new_lstm_with_options, FnInStruct)]
pub fn create_lstm_with_options_v1(
provider: &ICU4XDataProvider,
options: ICU4XLineBreakOptionsV1,
) -> Result<Box<ICU4XLineSegmenter>, ICU4XError> {
Ok(Box::new(ICU4XLineSegmenter(call_constructor!(
LineSegmenter::new_lstm_with_options [r => Ok(r)],
LineSegmenter::try_new_lstm_with_options_with_any_provider,
LineSegmenter::try_new_lstm_with_options_with_buffer_provider,
provider,
options.into(),
)?)))
}
/// Construct a [`ICU4XLineSegmenter`] with custom options and dictionary payload data for
/// Burmese, Khmer, Lao, and Thai.
#[diplomat::rust_link(
icu::segmenter::LineSegmenter::new_dictionary_with_options,
FnInStruct
)]
pub fn create_dictionary_with_options_v1(
provider: &ICU4XDataProvider,
options: ICU4XLineBreakOptionsV1,
) -> Result<Box<ICU4XLineSegmenter>, ICU4XError> {
Ok(Box::new(ICU4XLineSegmenter(call_constructor!(
LineSegmenter::new_dictionary_with_options [r => Ok(r)],
LineSegmenter::try_new_dictionary_with_options_with_any_provider,
LineSegmenter::try_new_dictionary_with_options_with_buffer_provider,
provider,
options.into(),
)?)))
}
/// Segments a (potentially ill-formed) UTF-8 string.
#[diplomat::rust_link(icu::segmenter::LineSegmenter::segment_utf8, FnInStruct)]
#[diplomat::rust_link(icu::segmenter::LineSegmenter::segment_str, FnInStruct, hidden)]
pub fn segment_utf8<'a>(&'a self, input: &'a str) -> Box<ICU4XLineBreakIteratorUtf8<'a>> {
let input = input.as_bytes(); // #2520
Box::new(ICU4XLineBreakIteratorUtf8(self.0.segment_utf8(input)))
}
/// Segments a UTF-16 string.
#[diplomat::rust_link(icu::segmenter::LineSegmenter::segment_utf16, FnInStruct)]
pub fn segment_utf16<'a>(
&'a self,
input: &'a [u16],
) -> Box<ICU4XLineBreakIteratorUtf16<'a>> {
Box::new(ICU4XLineBreakIteratorUtf16(self.0.segment_utf16(input)))
}
/// Segments a Latin-1 string.
#[diplomat::rust_link(icu::segmenter::LineSegmenter::segment_latin1, FnInStruct)]
pub fn segment_latin1<'a>(
&'a self,
input: &'a [u8],
) -> Box<ICU4XLineBreakIteratorLatin1<'a>> {
Box::new(ICU4XLineBreakIteratorLatin1(self.0.segment_latin1(input)))
}
}
impl<'a> ICU4XLineBreakIteratorUtf8<'a> {
/// Finds the next breakpoint. Returns -1 if at the end of the string or if the index is
/// out of range of a 32-bit signed integer.
#[allow(clippy::should_implement_trait)]
#[diplomat::rust_link(icu::segmenter::LineBreakIterator::next, FnInStruct)]
#[diplomat::rust_link(
icu::segmenter::LineBreakIterator::Item,
AssociatedTypeInStruct,
hidden
)]
pub fn next(&mut self) -> i32 {
self.0
.next()
.and_then(|u| i32::try_from(u).ok())
.unwrap_or(-1)
}
}
impl<'a> ICU4XLineBreakIteratorUtf16<'a> {
/// Finds the next breakpoint. Returns -1 if at the end of the string or if the index is
/// out of range of a 32-bit signed integer.
#[allow(clippy::should_implement_trait)]
#[diplomat::rust_link(icu::segmenter::LineBreakIterator::next, FnInStruct)]
#[diplomat::rust_link(
icu::segmenter::LineBreakIterator::Item,
AssociatedTypeInStruct,
hidden
)]
pub fn next(&mut self) -> i32 {
self.0
.next()
.and_then(|u| i32::try_from(u).ok())
.unwrap_or(-1)
}
}
impl<'a> ICU4XLineBreakIteratorLatin1<'a> {
/// Finds the next breakpoint. Returns -1 if at the end of the string or if the index is
/// out of range of a 32-bit signed integer.
#[allow(clippy::should_implement_trait)]
#[diplomat::rust_link(icu::segmenter::LineBreakIterator::next, FnInStruct)]
#[diplomat::rust_link(
icu::segmenter::LineBreakIterator::Item,
AssociatedTypeInStruct,
hidden
)]
pub fn next(&mut self) -> i32 {
self.0
.next()
.and_then(|u| i32::try_from(u).ok())
.unwrap_or(-1)
}
}
}
impl From<ffi::ICU4XLineBreakStrictness> for LineBreakStrictness {
fn from(other: ffi::ICU4XLineBreakStrictness) -> Self {
match other {
ffi::ICU4XLineBreakStrictness::Loose => Self::Loose,
ffi::ICU4XLineBreakStrictness::Normal => Self::Normal,
ffi::ICU4XLineBreakStrictness::Strict => Self::Strict,
ffi::ICU4XLineBreakStrictness::Anywhere => Self::Anywhere,
}
}
}
impl From<ffi::ICU4XLineBreakWordOption> for LineBreakWordOption {
fn from(other: ffi::ICU4XLineBreakWordOption) -> Self {
match other {
ffi::ICU4XLineBreakWordOption::Normal => Self::Normal,
ffi::ICU4XLineBreakWordOption::BreakAll => Self::BreakAll,
ffi::ICU4XLineBreakWordOption::KeepAll => Self::KeepAll,
}
}
}
impl From<ffi::ICU4XLineBreakOptionsV1> for LineBreakOptions {
fn from(other: ffi::ICU4XLineBreakOptionsV1) -> Self {
let mut options = LineBreakOptions::default();
options.strictness = other.strictness.into();
options.word_option = other.word_option.into();
options.ja_zh = other.ja_zh;
options
}
}
|