/rust/registry/src/index.crates.io-1949cf8c6b5b557f/icu_capi-1.5.1/src/script.rs
Line | Count | Source |
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::provider::ffi::ICU4XDataProvider; |
8 | | use alloc::boxed::Box; |
9 | | use icu_properties::{script, sets::CodePointSetData, Script}; |
10 | | |
11 | | use crate::errors::ffi::ICU4XError; |
12 | | use crate::properties_iter::ffi::CodePointRangeIterator; |
13 | | use crate::properties_sets::ffi::ICU4XCodePointSetData; |
14 | | |
15 | | #[diplomat::opaque] |
16 | | /// An ICU4X ScriptWithExtensions map object, capable of holding a map of codepoints to scriptextensions values |
17 | | #[diplomat::rust_link(icu::properties::script::ScriptWithExtensions, Struct)] |
18 | | #[diplomat::rust_link( |
19 | | icu::properties::script::ScriptWithExtensions::from_data, |
20 | | FnInStruct, |
21 | | hidden |
22 | | )] |
23 | | pub struct ICU4XScriptWithExtensions(pub script::ScriptWithExtensions); |
24 | | |
25 | | #[diplomat::opaque] |
26 | | /// A slightly faster ICU4XScriptWithExtensions object |
27 | | #[diplomat::rust_link(icu::properties::script::ScriptWithExtensionsBorrowed, Struct)] |
28 | | pub struct ICU4XScriptWithExtensionsBorrowed<'a>(pub script::ScriptWithExtensionsBorrowed<'a>); |
29 | | #[diplomat::opaque] |
30 | | /// An object that represents the Script_Extensions property for a single character |
31 | | #[diplomat::rust_link(icu::properties::script::ScriptExtensionsSet, Struct)] |
32 | | pub struct ICU4XScriptExtensionsSet<'a>(pub script::ScriptExtensionsSet<'a>); |
33 | | |
34 | | impl ICU4XScriptWithExtensions { |
35 | | #[diplomat::rust_link(icu::properties::script::script_with_extensions, Fn)] |
36 | | #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)] |
37 | 0 | pub fn create( |
38 | 0 | provider: &ICU4XDataProvider, |
39 | 0 | ) -> Result<Box<ICU4XScriptWithExtensions>, ICU4XError> { |
40 | 0 | Ok(Box::new(ICU4XScriptWithExtensions(call_constructor!( |
41 | 0 | script::script_with_extensions [r => Ok(r.static_to_owned())], |
42 | | script::load_script_with_extensions_with_any_provider, |
43 | | script::load_script_with_extensions_with_buffer_provider, |
44 | | provider |
45 | 0 | )?))) |
46 | 0 | } |
47 | | |
48 | | /// Get the Script property value for a code point |
49 | | #[diplomat::rust_link( |
50 | | icu::properties::script::ScriptWithExtensionsBorrowed::get_script_val, |
51 | | FnInStruct |
52 | | )] |
53 | 0 | pub fn get_script_val(&self, code_point: u32) -> u16 { |
54 | 0 | self.0.as_borrowed().get_script_val(code_point).0 |
55 | 0 | } |
56 | | |
57 | | /// Check if the Script_Extensions property of the given code point covers the given script |
58 | | #[diplomat::rust_link( |
59 | | icu::properties::script::ScriptWithExtensionsBorrowed::has_script, |
60 | | FnInStruct |
61 | | )] |
62 | 0 | pub fn has_script(&self, code_point: u32, script: u16) -> bool { |
63 | 0 | self.0.as_borrowed().has_script(code_point, Script(script)) |
64 | 0 | } |
65 | | |
66 | | /// Borrow this object for a slightly faster variant with more operations |
67 | | #[diplomat::rust_link( |
68 | | icu::properties::script::ScriptWithExtensions::as_borrowed, |
69 | | FnInStruct |
70 | | )] |
71 | | #[diplomat::attr(supports = accessors, getter)] |
72 | 0 | pub fn as_borrowed<'a>(&'a self) -> Box<ICU4XScriptWithExtensionsBorrowed<'a>> { |
73 | 0 | Box::new(ICU4XScriptWithExtensionsBorrowed(self.0.as_borrowed())) |
74 | 0 | } |
75 | | |
76 | | /// Get a list of ranges of code points that contain this script in their Script_Extensions values |
77 | | #[diplomat::rust_link( |
78 | | icu::properties::script::ScriptWithExtensionsBorrowed::get_script_extensions_ranges, |
79 | | FnInStruct |
80 | | )] |
81 | 0 | pub fn iter_ranges_for_script<'a>( |
82 | 0 | &'a self, |
83 | 0 | script: u16, |
84 | 0 | ) -> Box<CodePointRangeIterator<'a>> { |
85 | 0 | Box::new(CodePointRangeIterator(Box::new( |
86 | 0 | self.0 |
87 | 0 | .as_borrowed() |
88 | 0 | .get_script_extensions_ranges(Script(script)), |
89 | 0 | ))) |
90 | 0 | } |
91 | | } |
92 | | |
93 | | impl<'a> ICU4XScriptWithExtensionsBorrowed<'a> { |
94 | | /// Get the Script property value for a code point |
95 | | #[diplomat::rust_link( |
96 | | icu::properties::script::ScriptWithExtensionsBorrowed::get_script_val, |
97 | | FnInStruct |
98 | | )] |
99 | 0 | pub fn get_script_val(&self, code_point: u32) -> u16 { |
100 | 0 | self.0.get_script_val(code_point).0 |
101 | 0 | } |
102 | | /// Get the Script property value for a code point |
103 | | #[diplomat::rust_link( |
104 | | icu::properties::script::ScriptWithExtensionsBorrowed::get_script_extensions_val, |
105 | | FnInStruct |
106 | | )] |
107 | 0 | pub fn get_script_extensions_val( |
108 | 0 | &self, |
109 | 0 | code_point: u32, |
110 | 0 | ) -> Box<ICU4XScriptExtensionsSet<'a>> { |
111 | 0 | Box::new(ICU4XScriptExtensionsSet( |
112 | 0 | self.0.get_script_extensions_val(code_point), |
113 | 0 | )) |
114 | 0 | } |
115 | | /// Check if the Script_Extensions property of the given code point covers the given script |
116 | | #[diplomat::rust_link( |
117 | | icu::properties::script::ScriptWithExtensionsBorrowed::has_script, |
118 | | FnInStruct |
119 | | )] |
120 | 0 | pub fn has_script(&self, code_point: u32, script: u16) -> bool { |
121 | 0 | self.0.has_script(code_point, Script(script)) |
122 | 0 | } |
123 | | |
124 | | /// Build the CodePointSetData corresponding to a codepoints matching a particular script |
125 | | /// in their Script_Extensions |
126 | | #[diplomat::rust_link( |
127 | | icu::properties::script::ScriptWithExtensionsBorrowed::get_script_extensions_set, |
128 | | FnInStruct |
129 | | )] |
130 | 0 | pub fn get_script_extensions_set(&self, script: u16) -> Box<ICU4XCodePointSetData> { |
131 | 0 | let list = self |
132 | 0 | .0 |
133 | 0 | .get_script_extensions_set(Script(script)) |
134 | 0 | .into_owned(); |
135 | 0 | let set = CodePointSetData::from_code_point_inversion_list(list); |
136 | 0 | Box::new(ICU4XCodePointSetData(set)) |
137 | 0 | } |
138 | | } |
139 | | impl<'a> ICU4XScriptExtensionsSet<'a> { |
140 | | /// Check if the Script_Extensions property of the given code point covers the given script |
141 | | #[diplomat::rust_link(icu::properties::script::ScriptExtensionsSet::contains, FnInStruct)] |
142 | 0 | pub fn contains(&self, script: u16) -> bool { |
143 | 0 | self.0.contains(&Script(script)) |
144 | 0 | } |
145 | | |
146 | | /// Get the number of scripts contained in here |
147 | | #[diplomat::rust_link(icu::properties::script::ScriptExtensionsSet::iter, FnInStruct)] |
148 | | #[diplomat::attr(supports = accessors, getter)] |
149 | 0 | pub fn count(&self) -> usize { |
150 | 0 | self.0.array_len() |
151 | 0 | } |
152 | | |
153 | | /// Get script at index |
154 | | #[diplomat::rust_link(icu::properties::script::ScriptExtensionsSet::iter, FnInStruct)] |
155 | 0 | pub fn script_at(&self, index: usize) -> Option<u16> { |
156 | 0 | self.0.array_get(index).map(|x| x.0) |
157 | 0 | } |
158 | | } |
159 | | } |