summaryrefslogtreecommitdiffstats
path: root/intl/icu_capi/src/normalizer.rs
blob: 13a1cf0292f9ed7d8ea28979bc3ca49a04b50f6a (plain)
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
// 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]
pub mod ffi {
    use crate::{errors::ffi::ICU4XError, provider::ffi::ICU4XDataProvider};
    use alloc::boxed::Box;
    use diplomat_runtime::DiplomatWriteable;
    use icu_normalizer::{ComposingNormalizer, DecomposingNormalizer};

    #[diplomat::opaque]
    #[diplomat::rust_link(icu::normalizer::ComposingNormalizer, Struct)]
    pub struct ICU4XComposingNormalizer(pub ComposingNormalizer);

    impl ICU4XComposingNormalizer {
        /// Construct a new ICU4XComposingNormalizer instance for NFC
        #[diplomat::rust_link(icu::normalizer::ComposingNormalizer::new_nfc, FnInStruct)]
        pub fn create_nfc(
            provider: &ICU4XDataProvider,
        ) -> Result<Box<ICU4XComposingNormalizer>, ICU4XError> {
            Ok(Box::new(ICU4XComposingNormalizer(call_constructor!(
                ComposingNormalizer::new_nfc [r => Ok(r)],
                ComposingNormalizer::try_new_nfc_with_any_provider,
                ComposingNormalizer::try_new_nfc_with_buffer_provider,
                provider,
            )?)))
        }

        /// Construct a new ICU4XComposingNormalizer instance for NFKC
        #[diplomat::rust_link(icu::normalizer::ComposingNormalizer::new_nfkc, FnInStruct)]
        pub fn create_nfkc(
            provider: &ICU4XDataProvider,
        ) -> Result<Box<ICU4XComposingNormalizer>, ICU4XError> {
            Ok(Box::new(ICU4XComposingNormalizer(call_constructor!(
                ComposingNormalizer::new_nfkc [r => Ok(r)],
                ComposingNormalizer::try_new_nfkc_with_any_provider,
                ComposingNormalizer::try_new_nfkc_with_buffer_provider,
                provider,
            )?)))
        }

        /// Normalize a (potentially ill-formed) UTF8 string
        ///
        /// Errors are mapped to REPLACEMENT CHARACTER
        #[diplomat::rust_link(icu::normalizer::ComposingNormalizer::normalize_utf8, FnInStruct)]
        #[diplomat::rust_link(icu::normalizer::ComposingNormalizer::normalize, FnInStruct, hidden)]
        #[diplomat::rust_link(
            icu::normalizer::ComposingNormalizer::normalize_to,
            FnInStruct,
            hidden
        )]
        #[diplomat::rust_link(
            icu::normalizer::ComposingNormalizer::normalize_utf8_to,
            FnInStruct,
            hidden
        )]
        pub fn normalize(&self, s: &str, write: &mut DiplomatWriteable) -> Result<(), ICU4XError> {
            let s = s.as_bytes(); // #2520
            self.0.normalize_utf8_to(s, write)?;
            Ok(())
        }

        /// Check if a (potentially ill-formed) UTF8 string is normalized
        ///
        /// Errors are mapped to REPLACEMENT CHARACTER
        #[diplomat::rust_link(icu::normalizer::ComposingNormalizer::is_normalized_utf8, FnInStruct)]
        #[diplomat::rust_link(
            icu::normalizer::ComposingNormalizer::is_normalized,
            FnInStruct,
            hidden
        )]
        pub fn is_normalized(&self, s: &str) -> bool {
            let s = s.as_bytes(); // #2520
            self.0.is_normalized_utf8(s)
        }
    }

    #[diplomat::opaque]
    #[diplomat::rust_link(icu::normalizer::DecomposingNormalizer, Struct)]
    pub struct ICU4XDecomposingNormalizer(pub DecomposingNormalizer);

    impl ICU4XDecomposingNormalizer {
        /// Construct a new ICU4XDecomposingNormalizer instance for NFC
        #[diplomat::rust_link(icu::normalizer::DecomposingNormalizer::new_nfd, FnInStruct)]
        pub fn create_nfd(
            provider: &ICU4XDataProvider,
        ) -> Result<Box<ICU4XDecomposingNormalizer>, ICU4XError> {
            Ok(Box::new(ICU4XDecomposingNormalizer(call_constructor!(
                DecomposingNormalizer::new_nfd [r => Ok(r)],
                DecomposingNormalizer::try_new_nfd_with_any_provider,
                DecomposingNormalizer::try_new_nfd_with_buffer_provider,
                provider,
            )?)))
        }

        /// Construct a new ICU4XDecomposingNormalizer instance for NFKC
        #[diplomat::rust_link(icu::normalizer::DecomposingNormalizer::new_nfkd, FnInStruct)]
        pub fn create_nfkd(
            provider: &ICU4XDataProvider,
        ) -> Result<Box<ICU4XDecomposingNormalizer>, ICU4XError> {
            Ok(Box::new(ICU4XDecomposingNormalizer(call_constructor!(
                DecomposingNormalizer::new_nfkd [r => Ok(r)],
                DecomposingNormalizer::try_new_nfkd_with_any_provider,
                DecomposingNormalizer::try_new_nfkd_with_buffer_provider,
                provider,
            )?)))
        }

        /// Normalize a (potentially ill-formed) UTF8 string
        ///
        /// Errors are mapped to REPLACEMENT CHARACTER
        #[diplomat::rust_link(icu::normalizer::DecomposingNormalizer::normalize_utf8, FnInStruct)]
        #[diplomat::rust_link(
            icu::normalizer::DecomposingNormalizer::normalize,
            FnInStruct,
            hidden
        )]
        #[diplomat::rust_link(
            icu::normalizer::DecomposingNormalizer::normalize_to,
            FnInStruct,
            hidden
        )]
        #[diplomat::rust_link(
            icu::normalizer::DecomposingNormalizer::normalize_utf8_to,
            FnInStruct,
            hidden
        )]
        pub fn normalize(&self, s: &str, write: &mut DiplomatWriteable) -> Result<(), ICU4XError> {
            let s = s.as_bytes(); // #2520
            self.0.normalize_utf8_to(s, write)?;
            Ok(())
        }

        /// Check if a (potentially ill-formed) UTF8 string is normalized
        ///
        /// Errors are mapped to REPLACEMENT CHARACTER
        #[diplomat::rust_link(
            icu::normalizer::DecomposingNormalizer::is_normalized_utf8,
            FnInStruct
        )]
        #[diplomat::rust_link(
            icu::normalizer::DecomposingNormalizer::is_normalized,
            FnInStruct,
            hidden
        )]
        pub fn is_normalized(&self, s: &str) -> bool {
            let s = s.as_bytes(); // #2520
            self.0.is_normalized_utf8(s)
        }
    }
}