Bug 1910110 - Return early when channel URI is void r=rpl a=Aryx
[gecko.git] / intl / icu_capi / src / normalizer_properties.rs
blob09dc3ddc7b8c049b5eb0762c9237ff12903a400a
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 ).
5 #[diplomat::bridge]
6 pub mod ffi {
7     use crate::{errors::ffi::ICU4XError, provider::ffi::ICU4XDataProvider};
8     use alloc::boxed::Box;
9     use icu_normalizer::properties::{
10         CanonicalCombiningClassMap, CanonicalComposition, CanonicalDecomposition, Decomposed,
11     };
13     /// Lookup of the Canonical_Combining_Class Unicode property
14     #[diplomat::opaque]
15     #[diplomat::rust_link(icu::normalizer::properties::CanonicalCombiningClassMap, Struct)]
16     pub struct ICU4XCanonicalCombiningClassMap(pub CanonicalCombiningClassMap);
18     impl ICU4XCanonicalCombiningClassMap {
19         /// Construct a new ICU4XCanonicalCombiningClassMap instance for NFC
20         #[diplomat::rust_link(
21             icu::normalizer::properties::CanonicalCombiningClassMap::new,
22             FnInStruct
23         )]
24         #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
25         pub fn create(
26             provider: &ICU4XDataProvider,
27         ) -> Result<Box<ICU4XCanonicalCombiningClassMap>, ICU4XError> {
28             Ok(Box::new(ICU4XCanonicalCombiningClassMap(
29                 call_constructor!(
30                     CanonicalCombiningClassMap::new [r => Ok(r)],
31                     CanonicalCombiningClassMap::try_new_with_any_provider,
32                     CanonicalCombiningClassMap::try_new_with_buffer_provider,
33                     provider
34                 )?,
35             )))
36         }
38         #[diplomat::rust_link(
39             icu::normalizer::properties::CanonicalCombiningClassMap::get,
40             FnInStruct
41         )]
42         #[diplomat::rust_link(
43             icu::properties::properties::CanonicalCombiningClass,
44             Struct,
45             compact
46         )]
47         #[diplomat::attr(supports = indexing, indexer)]
48         pub fn get(&self, ch: DiplomatChar) -> u8 {
49             self.0.get32(ch).0
50         }
51         #[diplomat::rust_link(
52             icu::normalizer::properties::CanonicalCombiningClassMap::get32,
53             FnInStruct
54         )]
55         #[diplomat::rust_link(
56             icu::properties::properties::CanonicalCombiningClass,
57             Struct,
58             compact
59         )]
60         #[diplomat::attr(dart, disable)]
61         pub fn get32(&self, ch: u32) -> u8 {
62             self.0.get32(ch).0
63         }
64     }
66     /// The raw canonical composition operation.
67     ///
68     /// Callers should generally use ICU4XComposingNormalizer unless they specifically need raw composition operations
69     #[diplomat::opaque]
70     #[diplomat::rust_link(icu::normalizer::properties::CanonicalComposition, Struct)]
71     pub struct ICU4XCanonicalComposition(pub CanonicalComposition);
73     impl ICU4XCanonicalComposition {
74         /// Construct a new ICU4XCanonicalComposition instance for NFC
75         #[diplomat::rust_link(icu::normalizer::properties::CanonicalComposition::new, FnInStruct)]
76         #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
77         pub fn create(
78             provider: &ICU4XDataProvider,
79         ) -> Result<Box<ICU4XCanonicalComposition>, ICU4XError> {
80             Ok(Box::new(ICU4XCanonicalComposition(call_constructor!(
81                 CanonicalComposition::new [r => Ok(r)],
82                 CanonicalComposition::try_new_with_any_provider,
83                 CanonicalComposition::try_new_with_buffer_provider,
84                 provider,
85             )?)))
86         }
88         /// Performs canonical composition (including Hangul) on a pair of characters
89         /// or returns NUL if these characters don’t compose. Composition exclusions are taken into account.
90         #[diplomat::rust_link(
91             icu::normalizer::properties::CanonicalComposition::compose,
92             FnInStruct
93         )]
94         pub fn compose(&self, starter: DiplomatChar, second: DiplomatChar) -> DiplomatChar {
95             match (char::from_u32(starter), char::from_u32(second)) {
96                 (Some(starter), Some(second)) => self.0.compose(starter, second),
97                 _ => None,
98             }
99             .unwrap_or('\0') as DiplomatChar
100         }
101     }
103     /// The outcome of non-recursive canonical decomposition of a character.
104     /// `second` will be NUL when the decomposition expands to a single character
105     /// (which may or may not be the original one)
106     #[diplomat::rust_link(icu::normalizer::properties::Decomposed, Enum)]
107     #[diplomat::out]
108     pub struct ICU4XDecomposed {
109         first: DiplomatChar,
110         second: DiplomatChar,
111     }
113     /// The raw (non-recursive) canonical decomposition operation.
114     ///
115     /// Callers should generally use ICU4XDecomposingNormalizer unless they specifically need raw composition operations
116     #[diplomat::opaque]
117     #[diplomat::rust_link(icu::normalizer::properties::CanonicalDecomposition, Struct)]
118     pub struct ICU4XCanonicalDecomposition(pub CanonicalDecomposition);
120     impl ICU4XCanonicalDecomposition {
121         /// Construct a new ICU4XCanonicalDecomposition instance for NFC
122         #[diplomat::rust_link(icu::normalizer::properties::CanonicalDecomposition::new, FnInStruct)]
123         #[diplomat::attr(all(supports = constructors, supports = fallible_constructors), constructor)]
124         pub fn create(
125             provider: &ICU4XDataProvider,
126         ) -> Result<Box<ICU4XCanonicalDecomposition>, ICU4XError> {
127             Ok(Box::new(ICU4XCanonicalDecomposition(call_constructor!(
128                 CanonicalDecomposition::new [r => Ok(r)],
129                 CanonicalDecomposition::try_new_with_any_provider,
130                 CanonicalDecomposition::try_new_with_buffer_provider,
131                 provider,
132             )?)))
133         }
135         /// Performs non-recursive canonical decomposition (including for Hangul).
136         #[diplomat::rust_link(
137             icu::normalizer::properties::CanonicalDecomposition::decompose,
138             FnInStruct
139         )]
140         pub fn decompose(&self, c: DiplomatChar) -> ICU4XDecomposed {
141             match char::from_u32(c) {
142                 Some(c) => match self.0.decompose(c) {
143                     Decomposed::Default => ICU4XDecomposed {
144                         first: c as DiplomatChar,
145                         second: '\0' as DiplomatChar,
146                     },
147                     Decomposed::Singleton(s) => ICU4XDecomposed {
148                         first: s as DiplomatChar,
149                         second: '\0' as DiplomatChar,
150                     },
151                     Decomposed::Expansion(first, second) => ICU4XDecomposed {
152                         first: first as DiplomatChar,
153                         second: second as DiplomatChar,
154                     },
155                 },
156                 _ => ICU4XDecomposed {
157                     first: c,
158                     second: '\0' as DiplomatChar,
159                 },
160             }
161         }
162     }