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
// 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]
#[diplomat::abi_rename = "icu4x_{0}_mv1"]
#[diplomat::attr(auto, namespace = "icu4x")]
pub mod ffi {
    use alloc::boxed::Box;

    use crate::properties_iter::ffi::CodePointRangeIterator;
    use crate::properties_sets::ffi::CodePointSetData;
    #[cfg(feature = "buffer_provider")]
    use crate::{errors::ffi::DataError, provider::ffi::DataProvider};

    #[diplomat::opaque]
    /// An ICU4X ScriptWithExtensions map object, capable of holding a map of codepoints to scriptextensions values
    #[diplomat::rust_link(icu::properties::script::ScriptWithExtensions, Struct)]
    pub struct ScriptWithExtensions(pub icu_properties::script::ScriptWithExtensions);

    #[diplomat::opaque]
    /// A slightly faster ScriptWithExtensions object
    #[diplomat::rust_link(icu::properties::script::ScriptWithExtensionsBorrowed, Struct)]
    pub struct ScriptWithExtensionsBorrowed<'a>(
        pub icu_properties::script::ScriptWithExtensionsBorrowed<'a>,
    );
    #[diplomat::opaque]
    /// An object that represents the Script_Extensions property for a single character
    #[diplomat::rust_link(icu::properties::script::ScriptExtensionsSet, Struct)]
    pub struct ScriptExtensionsSet<'a>(pub icu_properties::script::ScriptExtensionsSet<'a>);

    impl ScriptWithExtensions {
        /// Create a map for the `Script`/`Script_Extensions` properties, using compiled data.
        #[diplomat::rust_link(icu::properties::script::ScriptWithExtensions::new, FnInStruct)]
        #[diplomat::rust_link(
            icu::properties::script::ScriptWithExtensionsBorrowed::new,
            FnInStruct,
            hidden
        )]
        #[diplomat::attr(auto, constructor)]
        #[cfg(feature = "compiled_data")]
        pub fn create() -> Box<ScriptWithExtensions> {
            Box::new(ScriptWithExtensions(
                icu_properties::script::ScriptWithExtensions::new().static_to_owned(),
            ))
        }

        /// Create a map for the `Script`/`Script_Extensions` properties, using compiled data.
        #[diplomat::rust_link(icu::properties::script::ScriptWithExtensions::new, FnInStruct)]
        #[diplomat::rust_link(
            icu::properties::script::ScriptWithExtensionsBorrowed::new,
            FnInStruct,
            hidden
        )]
        #[diplomat::attr(supports = fallible_constructors, named_constructor = "with_provider")]
        #[cfg(feature = "buffer_provider")]
        pub fn create_with_provider(
            provider: &DataProvider,
        ) -> Result<Box<ScriptWithExtensions>, DataError> {
            Ok(Box::new(ScriptWithExtensions(provider.call_constructor(
                |provider| {
                    icu_properties::script::ScriptWithExtensions::try_new_with_buffer_provider(
                        provider,
                    )
                },
            )?)))
        }

        /// Get the Script property value for a code point
        #[diplomat::rust_link(
            icu::properties::script::ScriptWithExtensionsBorrowed::get_script_val,
            FnInStruct
        )]
        #[diplomat::rust_link(
            icu::properties::script::ScriptWithExtensionsBorrowed::get_script_val32,
            FnInStruct,
            hidden
        )]
        pub fn get_script_val(&self, ch: DiplomatChar) -> u16 {
            self.0.as_borrowed().get_script_val32(ch).0
        }

        /// Check if the Script_Extensions property of the given code point covers the given script
        #[diplomat::rust_link(
            icu::properties::script::ScriptWithExtensionsBorrowed::has_script,
            FnInStruct
        )]
        #[diplomat::rust_link(
            icu::properties::script::ScriptWithExtensionsBorrowed::has_script32,
            FnInStruct,
            hidden
        )]
        pub fn has_script(&self, ch: DiplomatChar, script: u16) -> bool {
            self.0
                .as_borrowed()
                .has_script32(ch, icu_properties::props::Script(script))
        }

        /// Borrow this object for a slightly faster variant with more operations
        #[diplomat::rust_link(
            icu::properties::script::ScriptWithExtensions::as_borrowed,
            FnInStruct
        )]
        #[diplomat::attr(auto, getter)]
        pub fn as_borrowed<'a>(&'a self) -> Box<ScriptWithExtensionsBorrowed<'a>> {
            Box::new(ScriptWithExtensionsBorrowed(self.0.as_borrowed()))
        }

        /// Get a list of ranges of code points that contain this script in their Script_Extensions values
        #[diplomat::rust_link(
            icu::properties::script::ScriptWithExtensionsBorrowed::get_script_extensions_ranges,
            FnInStruct
        )]
        pub fn iter_ranges_for_script<'a>(
            &'a self,
            script: u16,
        ) -> Box<CodePointRangeIterator<'a>> {
            Box::new(CodePointRangeIterator(Box::new(
                self.0
                    .as_borrowed()
                    .get_script_extensions_ranges(icu_properties::props::Script(script)),
            )))
        }
    }

    impl<'a> ScriptWithExtensionsBorrowed<'a> {
        /// Get the Script property value for a code point
        #[diplomat::rust_link(
            icu::properties::script::ScriptWithExtensionsBorrowed::get_script_val,
            FnInStruct
        )]
        /// Get the Script property value for a code point
        #[diplomat::rust_link(
            icu::properties::script::ScriptWithExtensionsBorrowed::get_script_val32,
            FnInStruct,
            hidden
        )]
        pub fn get_script_val(&self, ch: DiplomatChar) -> u16 {
            self.0.get_script_val32(ch).0
        }
        /// Get the Script property value for a code point
        #[diplomat::rust_link(
            icu::properties::script::ScriptWithExtensionsBorrowed::get_script_extensions_val,
            FnInStruct
        )]
        #[diplomat::rust_link(
            icu::properties::script::ScriptWithExtensionsBorrowed::get_script_extensions_val32,
            FnInStruct,
            hidden
        )]
        pub fn get_script_extensions_val(&self, ch: DiplomatChar) -> Box<ScriptExtensionsSet<'a>> {
            Box::new(ScriptExtensionsSet(self.0.get_script_extensions_val32(ch)))
        }
        /// Check if the Script_Extensions property of the given code point covers the given script
        #[diplomat::rust_link(
            icu::properties::script::ScriptWithExtensionsBorrowed::has_script,
            FnInStruct
        )]
        #[diplomat::rust_link(
            icu::properties::script::ScriptWithExtensionsBorrowed::has_script32,
            FnInStruct,
            hidden
        )]
        pub fn has_script(&self, ch: DiplomatChar, script: u16) -> bool {
            self.0
                .has_script32(ch, icu_properties::props::Script(script))
        }

        /// Build the CodePointSetData corresponding to a codepoints matching a particular script
        /// in their Script_Extensions
        #[diplomat::rust_link(
            icu::properties::script::ScriptWithExtensionsBorrowed::get_script_extensions_set,
            FnInStruct
        )]
        pub fn get_script_extensions_set(&self, script: u16) -> Box<CodePointSetData> {
            let list = self
                .0
                .get_script_extensions_set(icu_properties::props::Script(script))
                .into_owned();
            let set = icu_properties::CodePointSetData::from_code_point_inversion_list(list);
            Box::new(CodePointSetData(set))
        }
    }
    impl<'a> ScriptExtensionsSet<'a> {
        /// Check if the Script_Extensions property of the given code point covers the given script
        #[diplomat::rust_link(icu::properties::script::ScriptExtensionsSet::contains, FnInStruct)]
        pub fn contains(&self, script: u16) -> bool {
            self.0.contains(&icu_properties::props::Script(script))
        }

        /// Get the number of scripts contained in here
        #[diplomat::rust_link(icu::properties::script::ScriptExtensionsSet::iter, FnInStruct)]
        #[diplomat::attr(auto, getter)]
        pub fn count(&self) -> usize {
            self.0.array_len()
        }

        /// Get script at index
        #[diplomat::rust_link(icu::properties::script::ScriptExtensionsSet::iter, FnInStruct)]
        pub fn script_at(&self, index: usize) -> Option<u16> {
            self.0.array_get(index).map(|x| x.0)
        }
    }
}