Backed out 2 changesets (bug 903746) for causing non-unified build bustages on nsIPri...
[gecko.git] / intl / icu_capi / src / normalizer_properties.rs
blobdbb1dfbdbc1fffbf5639de449cb88727ffc0dba6
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         pub fn create(
25             provider: &ICU4XDataProvider,
26         ) -> Result<Box<ICU4XCanonicalCombiningClassMap>, ICU4XError> {
27             Ok(Box::new(ICU4XCanonicalCombiningClassMap(
28                 call_constructor!(
29                     CanonicalCombiningClassMap::new [r => Ok(r)],
30                     CanonicalCombiningClassMap::try_new_with_any_provider,
31                     CanonicalCombiningClassMap::try_new_with_buffer_provider,
32                     provider
33                 )?,
34             )))
35         }
37         #[diplomat::rust_link(
38             icu::normalizer::properties::CanonicalCombiningClassMap::get,
39             FnInStruct
40         )]
41         #[diplomat::rust_link(
42             icu::properties::properties::CanonicalCombiningClass,
43             Struct,
44             compact
45         )]
46         pub fn get(&self, ch: char) -> u8 {
47             self.0.get(ch).0
48         }
49         #[diplomat::rust_link(
50             icu::normalizer::properties::CanonicalCombiningClassMap::get32,
51             FnInStruct
52         )]
53         #[diplomat::rust_link(
54             icu::properties::properties::CanonicalCombiningClass,
55             Struct,
56             compact
57         )]
58         pub fn get32(&self, ch: u32) -> u8 {
59             self.0.get32(ch).0
60         }
61     }
63     /// The raw canonical composition operation.
64     ///
65     /// Callers should generally use ICU4XComposingNormalizer unless they specifically need raw composition operations
66     #[diplomat::opaque]
67     #[diplomat::rust_link(icu::normalizer::properties::CanonicalComposition, Struct)]
68     pub struct ICU4XCanonicalComposition(pub CanonicalComposition);
70     impl ICU4XCanonicalComposition {
71         /// Construct a new ICU4XCanonicalComposition instance for NFC
72         #[diplomat::rust_link(icu::normalizer::properties::CanonicalComposition::new, FnInStruct)]
73         pub fn create(
74             provider: &ICU4XDataProvider,
75         ) -> Result<Box<ICU4XCanonicalComposition>, ICU4XError> {
76             Ok(Box::new(ICU4XCanonicalComposition(call_constructor!(
77                 CanonicalComposition::new [r => Ok(r)],
78                 CanonicalComposition::try_new_with_any_provider,
79                 CanonicalComposition::try_new_with_buffer_provider,
80                 provider,
81             )?)))
82         }
84         /// Performs canonical composition (including Hangul) on a pair of characters
85         /// or returns NUL if these characters don’t compose. Composition exclusions are taken into account.
86         #[diplomat::rust_link(
87             icu::normalizer::properties::CanonicalComposition::compose,
88             FnInStruct
89         )]
90         pub fn compose(&self, starter: char, second: char) -> char {
91             self.0.compose(starter, second).unwrap_or('\0')
92         }
93     }
95     /// The outcome of non-recursive canonical decomposition of a character.
96     /// `second` will be NUL when the decomposition expands to a single character
97     /// (which may or may not be the original one)
98     #[diplomat::rust_link(icu::normalizer::properties::Decomposed, Enum)]
99     pub struct ICU4XDecomposed {
100         first: char,
101         second: char,
102     }
104     /// The raw (non-recursive) canonical decomposition operation.
105     ///
106     /// Callers should generally use ICU4XDecomposingNormalizer unless they specifically need raw composition operations
107     #[diplomat::opaque]
108     #[diplomat::rust_link(icu::normalizer::properties::CanonicalDecomposition, Struct)]
109     pub struct ICU4XCanonicalDecomposition(pub CanonicalDecomposition);
111     impl ICU4XCanonicalDecomposition {
112         /// Construct a new ICU4XCanonicalDecomposition instance for NFC
113         #[diplomat::rust_link(icu::normalizer::properties::CanonicalDecomposition::new, FnInStruct)]
114         pub fn create(
115             provider: &ICU4XDataProvider,
116         ) -> Result<Box<ICU4XCanonicalDecomposition>, ICU4XError> {
117             Ok(Box::new(ICU4XCanonicalDecomposition(call_constructor!(
118                 CanonicalDecomposition::new [r => Ok(r)],
119                 CanonicalDecomposition::try_new_with_any_provider,
120                 CanonicalDecomposition::try_new_with_buffer_provider,
121                 provider,
122             )?)))
123         }
125         /// Performs non-recursive canonical decomposition (including for Hangul).
126         #[diplomat::rust_link(
127             icu::normalizer::properties::CanonicalDecomposition::decompose,
128             FnInStruct
129         )]
130         pub fn decompose(&self, c: char) -> ICU4XDecomposed {
131             match self.0.decompose(c) {
132                 Decomposed::Default => ICU4XDecomposed {
133                     first: c,
134                     second: '\0',
135                 },
136                 Decomposed::Singleton(s) => ICU4XDecomposed {
137                     first: s,
138                     second: '\0',
139                 },
140                 Decomposed::Expansion(first, second) => ICU4XDecomposed { first, second },
141             }
142         }
143     }