icu_properties/exemplar_chars.rs
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//! This module provides APIs for getting exemplar characters for a locale.
6//!
7//! Exemplars are characters used by a language, separated into different sets.
8//! The sets are: main, auxiliary, punctuation, numbers, and index.
9//!
10//! The sets define, according to typical usage in the language,
11//! which characters occur in which contexts with which frequency.
12//! For more information, see the documentation in the
13//! [Exemplars section in Unicode Technical Standard #35](https://unicode.org/reports/tr35/tr35-general.html#Exemplars)
14//! of the LDML specification.
15//!
16//! # Examples
17//!
18//! ```
19//! use icu::locid::locale;
20//! use icu::properties::exemplar_chars;
21//!
22//! let locale = locale!("en-001").into();
23//! let data = exemplar_chars::exemplars_main(&locale)
24//! .expect("locale should be present");
25//! let exemplars_main = data.as_borrowed();
26//!
27//! assert!(exemplars_main.contains_char('a'));
28//! assert!(exemplars_main.contains_char('z'));
29//! assert!(exemplars_main.contains("a"));
30//! assert!(!exemplars_main.contains("ä"));
31//! assert!(!exemplars_main.contains("ng"));
32//! ```
33
34use crate::provider::*;
35use crate::sets::UnicodeSetData;
36use crate::PropertiesError;
37use icu_provider::prelude::*;
38
39macro_rules! make_exemplar_chars_unicode_set_property {
40 (
41 // currently unused
42 marker: $marker_name:ident;
43 keyed_data_marker: $keyed_data_marker:ty;
44 func:
45 $vis:vis fn $funcname:ident();
46 $(#[$attr:meta])*
47 $vis2:vis fn $constname:ident();
48 ) => {
49 #[doc = concat!("A version of [`", stringify!($constname), "()`] that uses custom data provided by a [`DataProvider`].")]
50 ///
51 /// [📚 Help choosing a constructor](icu_provider::constructors)
52 $vis fn $funcname(
53 provider: &(impl DataProvider<$keyed_data_marker> + ?Sized),
54 locale: &DataLocale,
55 ) -> Result<UnicodeSetData, PropertiesError> {
56 Ok(provider.load(
57 DataRequest {
58 locale,
59 metadata: Default::default(),
60 })
61 .and_then(DataResponse::take_payload)
62 .map(UnicodeSetData::from_data)?
63 )
64 }
65 $(#[$attr])*
66 #[cfg(feature = "compiled_data")]
67 $vis2 fn $constname(
68 locale: &DataLocale,
69 ) -> Result<UnicodeSetData, PropertiesError> {
70 Ok(UnicodeSetData::from_data(
71 DataProvider::<$keyed_data_marker>::load(
72 &crate::provider::Baked,
73 DataRequest {
74 locale,
75 metadata: Default::default(),
76 })
77 .and_then(DataResponse::take_payload)?
78 ))
79 }
80 }
81}
82
83make_exemplar_chars_unicode_set_property!(
84 marker: ExemplarCharactersMain;
85 keyed_data_marker: ExemplarCharactersMainV1Marker;
86 func:
87 pub fn load_exemplars_main();
88
89 /// Get the "main" set of exemplar characters.
90 ///
91 /// ✨ *Enabled with the `compiled_data` Cargo feature.*
92 ///
93 /// [📚 Help choosing a constructor](icu_provider::constructors)
94 ///
95 /// # Examples
96 ///
97 /// ```
98 /// use icu::locid::locale;
99 /// use icu::properties::exemplar_chars;
100 ///
101 /// let data = exemplar_chars::exemplars_main(&locale!("en").into())
102 /// .expect("locale should be present");
103 /// let exemplars_main = data.as_borrowed();
104 ///
105 /// assert!(exemplars_main.contains_char('a'));
106 /// assert!(exemplars_main.contains_char('z'));
107 /// assert!(exemplars_main.contains("a"));
108 /// assert!(!exemplars_main.contains("ä"));
109 /// assert!(!exemplars_main.contains("ng"));
110 /// assert!(!exemplars_main.contains("A"));
111 /// ```
112 pub fn exemplars_main();
113);
114
115make_exemplar_chars_unicode_set_property!(
116 marker: ExemplarCharactersAuxiliary;
117 keyed_data_marker: ExemplarCharactersAuxiliaryV1Marker;
118 func:
119 pub fn load_exemplars_auxiliary();
120
121 /// Get the "auxiliary" set of exemplar characters.
122 ///
123 /// ✨ *Enabled with the `compiled_data` Cargo feature.*
124 ///
125 /// [📚 Help choosing a constructor](icu_provider::constructors)
126 ///
127 /// # Examples
128 ///
129 /// ```
130 /// use icu::locid::locale;
131 /// use icu::properties::exemplar_chars;
132 ///
133 /// let data =
134 /// exemplar_chars::exemplars_auxiliary(&locale!("en").into())
135 /// .expect("locale should be present");
136 /// let exemplars_auxiliary = data.as_borrowed();
137 ///
138 /// assert!(!exemplars_auxiliary.contains_char('a'));
139 /// assert!(!exemplars_auxiliary.contains_char('z'));
140 /// assert!(!exemplars_auxiliary.contains("a"));
141 /// assert!(exemplars_auxiliary.contains("ä"));
142 /// assert!(!exemplars_auxiliary.contains("ng"));
143 /// assert!(!exemplars_auxiliary.contains("A"));
144 /// ```
145 pub fn exemplars_auxiliary();
146);
147
148make_exemplar_chars_unicode_set_property!(
149 marker: ExemplarCharactersPunctuation;
150 keyed_data_marker: ExemplarCharactersPunctuationV1Marker;
151 func:
152 pub fn load_exemplars_punctuation();
153
154 /// Get the "punctuation" set of exemplar characters.
155 ///
156 /// ✨ *Enabled with the `compiled_data` Cargo feature.*
157 ///
158 /// [📚 Help choosing a constructor](icu_provider::constructors)
159 ///
160 /// # Examples
161 ///
162 /// ```
163 /// use icu::locid::locale;
164 /// use icu::properties::exemplar_chars;
165 ///
166 /// let data =
167 /// exemplar_chars::exemplars_punctuation(&locale!("en").into())
168 /// .expect("locale should be present");
169 /// let exemplars_punctuation = data.as_borrowed();
170 ///
171 /// assert!(!exemplars_punctuation.contains_char('0'));
172 /// assert!(!exemplars_punctuation.contains_char('9'));
173 /// assert!(!exemplars_punctuation.contains_char('%'));
174 /// assert!(exemplars_punctuation.contains_char(','));
175 /// assert!(exemplars_punctuation.contains_char('.'));
176 /// assert!(exemplars_punctuation.contains_char('!'));
177 /// assert!(exemplars_punctuation.contains_char('?'));
178 /// ```
179 pub fn exemplars_punctuation();
180);
181
182make_exemplar_chars_unicode_set_property!(
183 marker: ExemplarCharactersNumbers;
184 keyed_data_marker: ExemplarCharactersNumbersV1Marker;
185 func:
186 pub fn load_exemplars_numbers();
187
188 /// Get the "numbers" set of exemplar characters.
189 ///
190 /// ✨ *Enabled with the `compiled_data` Cargo feature.*
191 ///
192 /// [📚 Help choosing a constructor](icu_provider::constructors)
193 ///
194 /// # Examples
195 ///
196 /// ```
197 /// use icu::locid::locale;
198 /// use icu::properties::exemplar_chars;
199 ///
200 /// let data =
201 /// exemplar_chars::exemplars_numbers(&locale!("en").into())
202 /// .expect("locale should be present");
203 /// let exemplars_numbers = data.as_borrowed();
204 ///
205 /// assert!(exemplars_numbers.contains_char('0'));
206 /// assert!(exemplars_numbers.contains_char('9'));
207 /// assert!(exemplars_numbers.contains_char('%'));
208 /// assert!(exemplars_numbers.contains_char(','));
209 /// assert!(exemplars_numbers.contains_char('.'));
210 /// assert!(!exemplars_numbers.contains_char('!'));
211 /// assert!(!exemplars_numbers.contains_char('?'));
212 /// ```
213 pub fn exemplars_numbers();
214);
215
216make_exemplar_chars_unicode_set_property!(
217 marker: ExemplarCharactersIndex;
218 keyed_data_marker: ExemplarCharactersIndexV1Marker;
219 func:
220 pub fn load_exemplars_index();
221
222 /// Get the "index" set of exemplar characters.
223 ///
224 /// ✨ *Enabled with the `compiled_data` Cargo feature.*
225 ///
226 /// [📚 Help choosing a constructor](icu_provider::constructors)
227 ///
228 /// # Examples
229 ///
230 /// ```
231 /// use icu::locid::locale;
232 /// use icu::properties::exemplar_chars;
233 ///
234 /// let data =
235 /// exemplar_chars::exemplars_index(&locale!("en").into())
236 /// .expect("locale should be present");
237 /// let exemplars_index = data.as_borrowed();
238 ///
239 /// assert!(!exemplars_index.contains_char('a'));
240 /// assert!(!exemplars_index.contains_char('z'));
241 /// assert!(!exemplars_index.contains("a"));
242 /// assert!(!exemplars_index.contains("ä"));
243 /// assert!(!exemplars_index.contains("ng"));
244 /// assert!(exemplars_index.contains("A"));
245 /// ```
246 pub fn exemplars_index();
247);