| 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 |
pub fn create( |
| 38 |
provider: &ICU4XDataProvider, |
| 39 |
) -> Result<Box<ICU4XScriptWithExtensions>, ICU4XError> { |
| 40 |
Ok(Box::new(ICU4XScriptWithExtensions(call_constructor!( |
| 41 |
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 |
)?))) |
| 46 |
} |
| 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 |
pub fn get_script_val(&self, code_point: u32) -> u16 { |
| 54 |
self.0.as_borrowed().get_script_val(code_point).0 |
| 55 |
} |
| 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 |
pub fn has_script(&self, code_point: u32, script: u16) -> bool { |
| 63 |
self.0.as_borrowed().has_script(code_point, Script(script)) |
| 64 |
} |
| 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 |
pub fn as_borrowed<'a>(&'a self) -> Box<ICU4XScriptWithExtensionsBorrowed<'a>> { |
| 73 |
Box::new(ICU4XScriptWithExtensionsBorrowed(self.0.as_borrowed())) |
| 74 |
} |
| 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 |
pub fn iter_ranges_for_script<'a>( |
| 82 |
&'a self, |
| 83 |
script: u16, |
| 84 |
) -> Box<CodePointRangeIterator<'a>> { |
| 85 |
Box::new(CodePointRangeIterator(Box::new( |
| 86 |
self.0 |
| 87 |
.as_borrowed() |
| 88 |
.get_script_extensions_ranges(Script(script)), |
| 89 |
))) |
| 90 |
} |
| 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 |
pub fn get_script_val(&self, code_point: u32) -> u16 { |
| 100 |
self.0.get_script_val(code_point).0 |
| 101 |
} |
| 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 |
pub fn get_script_extensions_val( |
| 108 |
&self, |
| 109 |
code_point: u32, |
| 110 |
) -> Box<ICU4XScriptExtensionsSet<'a>> { |
| 111 |
Box::new(ICU4XScriptExtensionsSet( |
| 112 |
self.0.get_script_extensions_val(code_point), |
| 113 |
)) |
| 114 |
} |
| 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 |
pub fn has_script(&self, code_point: u32, script: u16) -> bool { |
| 121 |
self.0.has_script(code_point, Script(script)) |
| 122 |
} |
| 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 |
pub fn get_script_extensions_set(&self, script: u16) -> Box<ICU4XCodePointSetData> { |
| 131 |
let list = self |
| 132 |
.0 |
| 133 |
.get_script_extensions_set(Script(script)) |
| 134 |
.into_owned(); |
| 135 |
let set = CodePointSetData::from_code_point_inversion_list(list); |
| 136 |
Box::new(ICU4XCodePointSetData(set)) |
| 137 |
} |
| 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 |
pub fn contains(&self, script: u16) -> bool { |
| 143 |
self.0.contains(&Script(script)) |
| 144 |
} |
| 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 |
pub fn count(&self) -> usize { |
| 150 |
self.0.array_len() |
| 151 |
} |
| 152 |
|
| 153 |
/// Get script at index |
| 154 |
#[diplomat::rust_link(icu::properties::script::ScriptExtensionsSet::iter, FnInStruct)] |
| 155 |
pub fn script_at(&self, index: usize) -> Option<u16> { |
| 156 |
self.0.array_get(index).map(|x| x.0) |
| 157 |
} |
| 158 |
} |
| 159 |
} |