icu_capi/
collections_sets.rs

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]
6pub mod ffi {
7    use crate::properties_sets::ffi::ICU4XCodePointSetData;
8    use alloc::boxed::Box;
9    use core::mem;
10    use icu_collections::codepointinvlist::CodePointInversionListBuilder;
11    use icu_properties::sets::CodePointSetData;
12
13    #[diplomat::opaque]
14    #[diplomat::rust_link(
15        icu::collections::codepointinvlist::CodePointInversionListBuilder,
16        Struct
17    )]
18    pub struct ICU4XCodePointSetBuilder(pub CodePointInversionListBuilder);
19
20    impl ICU4XCodePointSetBuilder {
21        /// Make a new set builder containing nothing
22        #[diplomat::rust_link(
23            icu::collections::codepointinvlist::CodePointInversionListBuilder::new,
24            FnInStruct
25        )]
26        #[diplomat::attr(supports = constructors, constructor)]
27        pub fn create() -> Box<Self> {
28            Box::new(Self(CodePointInversionListBuilder::new()))
29        }
30
31        /// Build this into a set
32        ///
33        /// This object is repopulated with an empty builder
34        #[diplomat::rust_link(
35            icu::collections::codepointinvlist::CodePointInversionListBuilder::build,
36            FnInStruct
37        )]
38        #[diplomat::rust_link(
39            icu::properties::sets::CodePointSetData::from_code_point_inversion_list,
40            FnInStruct,
41            hidden
42        )]
43        pub fn build(&mut self) -> Box<ICU4XCodePointSetData> {
44            let inner = mem::take(&mut self.0);
45            let built = inner.build();
46            let set = CodePointSetData::from_code_point_inversion_list(built);
47            Box::new(ICU4XCodePointSetData(set))
48        }
49
50        /// Complements this set
51        ///
52        /// (Elements in this set are removed and vice versa)
53        #[diplomat::rust_link(
54            icu::collections::codepointinvlist::CodePointInversionListBuilder::complement,
55            FnInStruct
56        )]
57        pub fn complement(&mut self) {
58            self.0.complement()
59        }
60
61        /// Returns whether this set is empty
62        #[diplomat::rust_link(
63            icu::collections::codepointinvlist::CodePointInversionListBuilder::is_empty,
64            FnInStruct
65        )]
66        #[diplomat::attr(supports = accessors, getter)]
67        pub fn is_empty(&self) -> bool {
68            self.0.is_empty()
69        }
70
71        /// Add a single character to the set
72        #[diplomat::rust_link(
73            icu::collections::codepointinvlist::CodePointInversionListBuilder::add_char,
74            FnInStruct
75        )]
76        #[diplomat::rust_link(
77            icu::collections::codepointinvlist::CodePointInversionListBuilder::add32,
78            FnInStruct,
79            hidden
80        )]
81        pub fn add_char(&mut self, ch: DiplomatChar) {
82            self.0.add32(ch)
83        }
84
85        /// Deprecated, use `add_char`.
86        #[diplomat::rust_link(
87            icu::collections::codepointinvlist::CodePointInversionListBuilder::add_u32,
88            FnInStruct
89        )]
90        #[diplomat::attr(*, disable)]
91        pub fn add_u32(&mut self, ch: u32) {
92            self.add_char(ch)
93        }
94
95        /// Add an inclusive range of characters to the set
96        #[diplomat::rust_link(
97            icu::collections::codepointinvlist::CodePointInversionListBuilder::add_range,
98            FnInStruct
99        )]
100        #[diplomat::rust_link(
101            icu::collections::codepointinvlist::CodePointInversionListBuilder::add_range32,
102            FnInStruct,
103            hidden
104        )]
105        pub fn add_inclusive_range(&mut self, start: DiplomatChar, end: DiplomatChar) {
106            self.0.add_range32(&(start..=end))
107        }
108
109        /// Deprecated, use `add_inclusive_range`.
110        #[diplomat::rust_link(
111            icu::collections::codepointinvlist::CodePointInversionListBuilder::add_range_u32,
112            FnInStruct
113        )]
114        #[diplomat::attr(*, disable)]
115        pub fn add_inclusive_range_u32(&mut self, start: u32, end: u32) {
116            self.add_inclusive_range(start, end)
117        }
118
119        /// Add all elements that belong to the provided set to the set
120        #[diplomat::rust_link(
121            icu::collections::codepointinvlist::CodePointInversionListBuilder::add_set,
122            FnInStruct
123        )]
124        #[diplomat::rust_link(
125            icu::properties::sets::CodePointSetData::as_code_point_inversion_list,
126            FnInStruct,
127            hidden
128        )]
129        #[diplomat::rust_link(
130            icu::properties::sets::CodePointSetData::to_code_point_inversion_list,
131            FnInStruct,
132            hidden
133        )]
134        pub fn add_set(&mut self, data: &ICU4XCodePointSetData) {
135            // This is a ZeroFrom and always cheap for a CPIL, may be expensive
136            // for other impls. In the future we can make this builder support multiple impls
137            // if we ever add them
138            let list = data.0.to_code_point_inversion_list();
139            self.0.add_set(&list);
140        }
141
142        /// Remove a single character to the set
143        #[diplomat::rust_link(
144            icu::collections::codepointinvlist::CodePointInversionListBuilder::remove_char,
145            FnInStruct
146        )]
147        #[diplomat::rust_link(
148            icu::collections::codepointinvlist::CodePointInversionListBuilder::remove32,
149            FnInStruct,
150            hidden
151        )]
152        pub fn remove_char(&mut self, ch: DiplomatChar) {
153            self.0.remove32(ch)
154        }
155
156        /// Remove an inclusive range of characters from the set
157        #[diplomat::rust_link(
158            icu::collections::codepointinvlist::CodePointInversionListBuilder::remove_range,
159            FnInStruct
160        )]
161        #[diplomat::rust_link(
162            icu::collections::codepointinvlist::CodePointInversionListBuilder::remove_range32,
163            FnInStruct,
164            hidden
165        )]
166        pub fn remove_inclusive_range(&mut self, start: DiplomatChar, end: DiplomatChar) {
167            self.0.remove_range32(&(start..=end))
168        }
169
170        /// Remove all elements that belong to the provided set from the set
171        #[diplomat::rust_link(
172            icu::collections::codepointinvlist::CodePointInversionListBuilder::remove_set,
173            FnInStruct
174        )]
175        pub fn remove_set(&mut self, data: &ICU4XCodePointSetData) {
176            // (see comment in add_set)
177            let list = data.0.to_code_point_inversion_list();
178            self.0.remove_set(&list);
179        }
180
181        /// Removes all elements from the set except a single character
182        #[diplomat::rust_link(
183            icu::collections::codepointinvlist::CodePointInversionListBuilder::retain_char,
184            FnInStruct
185        )]
186        #[diplomat::rust_link(
187            icu::collections::codepointinvlist::CodePointInversionListBuilder::retain32,
188            FnInStruct,
189            hidden
190        )]
191        pub fn retain_char(&mut self, ch: DiplomatChar) {
192            self.0.retain32(ch)
193        }
194
195        /// Removes all elements from the set except an inclusive range of characters f
196        #[diplomat::rust_link(
197            icu::collections::codepointinvlist::CodePointInversionListBuilder::retain_range,
198            FnInStruct
199        )]
200        #[diplomat::rust_link(
201            icu::collections::codepointinvlist::CodePointInversionListBuilder::retain_range32,
202            FnInStruct,
203            hidden
204        )]
205        pub fn retain_inclusive_range(&mut self, start: DiplomatChar, end: DiplomatChar) {
206            self.0.retain_range32(&(start..=end))
207        }
208
209        /// Removes all elements from the set except all elements in the provided set
210        #[diplomat::rust_link(
211            icu::collections::codepointinvlist::CodePointInversionListBuilder::retain_set,
212            FnInStruct
213        )]
214        pub fn retain_set(&mut self, data: &ICU4XCodePointSetData) {
215            // (see comment in add_set)
216            let list = data.0.to_code_point_inversion_list();
217            self.0.retain_set(&list);
218        }
219
220        /// Complement a single character to the set
221        ///
222        /// (Characters which are in this set are removed and vice versa)
223        #[diplomat::rust_link(
224            icu::collections::codepointinvlist::CodePointInversionListBuilder::complement_char,
225            FnInStruct
226        )]
227        #[diplomat::rust_link(
228            icu::collections::codepointinvlist::CodePointInversionListBuilder::complement32,
229            FnInStruct,
230            hidden
231        )]
232        pub fn complement_char(&mut self, ch: DiplomatChar) {
233            self.0.complement32(ch)
234        }
235
236        /// Complement an inclusive range of characters from the set
237        ///
238        /// (Characters which are in this set are removed and vice versa)
239        #[diplomat::rust_link(
240            icu::collections::codepointinvlist::CodePointInversionListBuilder::complement_range,
241            FnInStruct
242        )]
243        #[diplomat::rust_link(
244            icu::collections::codepointinvlist::CodePointInversionListBuilder::complement_range32,
245            FnInStruct,
246            hidden
247        )]
248        pub fn complement_inclusive_range(&mut self, start: DiplomatChar, end: DiplomatChar) {
249            self.0.complement_range32(&(start..=end))
250        }
251
252        /// Complement all elements that belong to the provided set from the set
253        ///
254        /// (Characters which are in this set are removed and vice versa)
255        #[diplomat::rust_link(
256            icu::collections::codepointinvlist::CodePointInversionListBuilder::complement_set,
257            FnInStruct
258        )]
259        pub fn complement_set(&mut self, data: &ICU4XCodePointSetData) {
260            // (see comment in add_set)
261            let list = data.0.to_code_point_inversion_list();
262            self.0.complement_set(&list);
263        }
264    }
265}