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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// 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 ).

//! This module provides APIs for getting exemplar characters for a locale.
//!
//! Exemplars are characters used by a language, separated into different sets.
//! The sets are: main, auxiliary, punctuation, numbers, and index.
//!
//! The sets define, according to typical usage in the language,
//! which characters occur in which contexts with which frequency.
//! For more information, see the documentation in the
//! [Exemplars section in Unicode Technical Standard #35](https://unicode.org/reports/tr35/tr35-general.html#Exemplars)
//! of the LDML specification.
//!
//! # Examples
//!
//! ```
//! use icu::locale::exemplar_chars::ExemplarCharacters;
//! use icu::locale::locale;
//!
//! let locale = locale!("en-001").into();
//! let exemplars_main = ExemplarCharacters::try_new_main(&locale)
//!     .expect("locale should be present");
//!
//! assert!(exemplars_main.contains('a'));
//! assert!(exemplars_main.contains('z'));
//! assert!(exemplars_main.contains_str("a"));
//! assert!(!exemplars_main.contains_str("รค"));
//! assert!(!exemplars_main.contains_str("ng"));
//! ```

use crate::provider::*;
use core::ops::Deref;
use icu_collections::codepointinvliststringlist::CodePointInversionListAndStringList;
use icu_provider::{marker::ErasedMarker, prelude::*};

/// A wrapper around `UnicodeSet` data (characters and strings)
#[derive(Debug)]
pub struct ExemplarCharacters {
    data: DataPayload<ErasedMarker<ExemplarCharactersV1<'static>>>,
}

impl ExemplarCharacters {
    /// Construct a borrowed version of this type that can be queried.
    ///
    /// This avoids a potential small underlying cost per API call (ex: `contains()`) by consolidating it
    /// up front.
    #[inline]
    pub fn as_borrowed(&self) -> ExemplarCharactersBorrowed<'_> {
        ExemplarCharactersBorrowed {
            data: self.data.get(),
        }
    }
}

/// A borrowed wrapper around code point set data, returned by
/// [`ExemplarCharacters::as_borrowed()`]. More efficient to query.
#[derive(Clone, Copy, Debug)]
pub struct ExemplarCharactersBorrowed<'a> {
    data: &'a ExemplarCharactersV1<'a>,
}

impl<'a> Deref for ExemplarCharactersBorrowed<'a> {
    type Target = CodePointInversionListAndStringList<'a>;

    fn deref(&self) -> &Self::Target {
        &self.data.0
    }
}

impl ExemplarCharactersBorrowed<'static> {
    /// Cheaply converts a [`ExemplarCharactersBorrowed<'static>`] into a [`ExemplarCharacters`].
    ///
    /// Note: Due to branching and indirection, using [`ExemplarCharacters`] might inhibit some
    /// compile-time optimizations that are possible with [`ExemplarCharactersBorrowed`].
    pub const fn static_to_owned(self) -> ExemplarCharacters {
        ExemplarCharacters {
            data: DataPayload::from_static_ref(self.data),
        }
    }
}

macro_rules! make_exemplar_chars_unicode_set_property {
    (
        // currently unused
        dyn_data_marker: $d:ident;
        data_marker: $data_marker:ty;
        func:
        pub fn $unstable:ident();
        $(#[$attr:meta])*
        pub fn $compiled:ident();
    ) => {
        $(#[$attr])*
        #[cfg(feature = "compiled_data")]
        pub fn $compiled(
            locale: &DataLocale,
        ) -> Result<ExemplarCharactersBorrowed<'static>, DataError> {
            Ok(ExemplarCharactersBorrowed {
                data: DataProvider::<$data_marker>::load(
                    &crate::provider::Baked,
                    DataRequest {
                        id: DataIdentifierBorrowed::for_locale(locale),
                        ..Default::default()
                    })?
                .payload
                .get_static()
                .ok_or_else(|| DataError::custom("Baked provider didn't return static payload"))?
            })
        }

        #[doc = concat!("A version of [`Self::", stringify!($compiled), "()`] that uses custom data provided by a [`DataProvider`].")]
        ///
        /// [๐Ÿ“š Help choosing a constructor](icu_provider::constructors)
        pub fn $unstable(
            provider: &(impl DataProvider<$data_marker> + ?Sized),
            locale: &DataLocale,
        ) -> Result<Self, DataError> {
            Ok(Self {
                data:
                provider.load(
                    DataRequest {
                        id: DataIdentifierBorrowed::for_locale(locale),
                        ..Default::default()
                })?
                .payload
                .cast()
            })
        }
    }
}

impl ExemplarCharacters {
    make_exemplar_chars_unicode_set_property!(
        dyn_data_marker: ExemplarCharactersMain;
        data_marker: ExemplarCharactersMainV1Marker;
        func:
        pub fn try_new_main_unstable();

        /// Get the "main" set of exemplar characters.
        ///
        /// โœจ *Enabled with the `compiled_data` Cargo feature.*
        ///
        /// [๐Ÿ“š Help choosing a constructor](icu_provider::constructors)
        ///
        /// # Examples
        ///
        /// ```
        /// use icu::locale::locale;
        /// use icu::locale::exemplar_chars::ExemplarCharacters;
        ///
        /// let exemplars_main = ExemplarCharacters::try_new_main(&locale!("en").into())
        ///     .expect("locale should be present");
        ///
        /// assert!(exemplars_main.contains('a'));
        /// assert!(exemplars_main.contains('z'));
        /// assert!(exemplars_main.contains_str("a"));
        /// assert!(!exemplars_main.contains_str("รค"));
        /// assert!(!exemplars_main.contains_str("ng"));
        /// assert!(!exemplars_main.contains_str("A"));
        /// ```
        pub fn try_new_main();
    );

    make_exemplar_chars_unicode_set_property!(
        dyn_data_marker: ExemplarCharactersAuxiliary;
        data_marker: ExemplarCharactersAuxiliaryV1Marker;
        func:
        pub fn try_new_auxiliary_unstable();

        /// Get the "auxiliary" set of exemplar characters.
        ///
        /// โœจ *Enabled with the `compiled_data` Cargo feature.*
        ///
        /// [๐Ÿ“š Help choosing a constructor](icu_provider::constructors)
        ///
        /// # Examples
        ///
        /// ```
        /// use icu::locale::locale;
        /// use icu::locale::exemplar_chars::ExemplarCharacters;
        ///
        /// let exemplars_auxiliary =
        ///     ExemplarCharacters::try_new_auxiliary(&locale!("en").into())
        ///     .expect("locale should be present");
        ///
        /// assert!(!exemplars_auxiliary.contains('a'));
        /// assert!(!exemplars_auxiliary.contains('z'));
        /// assert!(!exemplars_auxiliary.contains_str("a"));
        /// assert!(exemplars_auxiliary.contains_str("รค"));
        /// assert!(!exemplars_auxiliary.contains_str("ng"));
        /// assert!(!exemplars_auxiliary.contains_str("A"));
        /// ```
        pub fn try_new_auxiliary();
    );

    make_exemplar_chars_unicode_set_property!(
        dyn_data_marker: ExemplarCharactersPunctuation;
        data_marker: ExemplarCharactersPunctuationV1Marker;
        func:
        pub fn try_new_punctuation_unstable();

        /// Get the "punctuation" set of exemplar characters.
        ///
        /// โœจ *Enabled with the `compiled_data` Cargo feature.*
        ///
        /// [๐Ÿ“š Help choosing a constructor](icu_provider::constructors)
        ///
        /// # Examples
        ///
        /// ```
        /// use icu::locale::locale;
        /// use icu::locale::exemplar_chars::ExemplarCharacters;
        ///
        /// let exemplars_punctuation =
        ///     ExemplarCharacters::try_new_punctuation(&locale!("en").into())
        ///     .expect("locale should be present");
        ///
        /// assert!(!exemplars_punctuation.contains('0'));
        /// assert!(!exemplars_punctuation.contains('9'));
        /// assert!(!exemplars_punctuation.contains('%'));
        /// assert!(exemplars_punctuation.contains(','));
        /// assert!(exemplars_punctuation.contains('.'));
        /// assert!(exemplars_punctuation.contains('!'));
        /// assert!(exemplars_punctuation.contains('?'));
        /// ```
        pub fn try_new_punctuation();
    );

    make_exemplar_chars_unicode_set_property!(
        dyn_data_marker: ExemplarCharactersNumbers;
        data_marker: ExemplarCharactersNumbersV1Marker;
        func:
        pub fn try_new_numbers_unstable();

        /// Get the "numbers" set of exemplar characters.
        ///
        /// โœจ *Enabled with the `compiled_data` Cargo feature.*
        ///
        /// [๐Ÿ“š Help choosing a constructor](icu_provider::constructors)
        ///
        /// # Examples
        ///
        /// ```
        /// use icu::locale::locale;
        /// use icu::locale::exemplar_chars::ExemplarCharacters;
        ///
        /// let exemplars_numbers =
        ///     ExemplarCharacters::try_new_numbers(&locale!("en").into())
        ///     .expect("locale should be present");
        ///
        /// assert!(exemplars_numbers.contains('0'));
        /// assert!(exemplars_numbers.contains('9'));
        /// assert!(exemplars_numbers.contains('%'));
        /// assert!(exemplars_numbers.contains(','));
        /// assert!(exemplars_numbers.contains('.'));
        /// assert!(!exemplars_numbers.contains('!'));
        /// assert!(!exemplars_numbers.contains('?'));
        /// ```
        pub fn try_new_numbers();
    );

    make_exemplar_chars_unicode_set_property!(
        dyn_data_marker: ExemplarCharactersIndex;
        data_marker: ExemplarCharactersIndexV1Marker;
        func:
        pub fn try_new_index_unstable();

        /// Get the "index" set of exemplar characters.
        ///
        /// โœจ *Enabled with the `compiled_data` Cargo feature.*
        ///
        /// [๐Ÿ“š Help choosing a constructor](icu_provider::constructors)
        ///
        /// # Examples
        ///
        /// ```
        /// use icu::locale::locale;
        /// use icu::locale::exemplar_chars::ExemplarCharacters;
        ///
        /// let exemplars_index =
        ///     ExemplarCharacters::try_new_index(&locale!("en").into())
        ///     .expect("locale should be present");
        ///
        /// assert!(!exemplars_index.contains('a'));
        /// assert!(!exemplars_index.contains('z'));
        /// assert!(!exemplars_index.contains_str("a"));
        /// assert!(!exemplars_index.contains_str("รค"));
        /// assert!(!exemplars_index.contains_str("ng"));
        /// assert!(exemplars_index.contains_str("A"));
        /// ```
        pub fn try_new_index();
    );
}