icu_locale_core/extensions/unicode/
mod.rs1mod attribute;
30mod attributes;
31mod key;
32mod keywords;
33mod subdivision;
34mod value;
35
36use core::cmp::Ordering;
37#[cfg(feature = "alloc")]
38use core::str::FromStr;
39
40#[doc(inline)]
41pub use attribute::{attribute, Attribute};
42pub use attributes::Attributes;
43#[doc(inline)]
44pub use key::{key, Key};
45pub use keywords::Keywords;
46#[doc(inline)]
47pub use subdivision::{subdivision_suffix, SubdivisionId, SubdivisionSuffix};
48#[doc(inline)]
49pub use value::{value, Value};
50
51#[cfg(feature = "alloc")]
52use super::ExtensionType;
53#[cfg(feature = "alloc")]
54use crate::parser::ParseError;
55#[cfg(feature = "alloc")]
56use crate::parser::SubtagIterator;
57
58pub(crate) const UNICODE_EXT_CHAR: char = 'u';
59pub(crate) const UNICODE_EXT_STR: &str = "u";
60
61#[derive(#[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl ::core::clone::Clone for Unicode {
#[inline]
fn clone(&self) -> Unicode {
Unicode {
keywords: ::core::clone::Clone::clone(&self.keywords),
attributes: ::core::clone::Clone::clone(&self.attributes),
}
}
}Clone, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl ::core::cmp::PartialEq for Unicode {
#[inline]
fn eq(&self, other: &Unicode) -> bool {
self.keywords == other.keywords && self.attributes == other.attributes
}
}PartialEq, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl ::core::cmp::Eq for Unicode {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) -> () {
let _: ::core::cmp::AssertParamIsEq<Keywords>;
let _: ::core::cmp::AssertParamIsEq<Attributes>;
}
}Eq, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl ::core::fmt::Debug for Unicode {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "Unicode",
"keywords", &self.keywords, "attributes", &&self.attributes)
}
}Debug, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl ::core::default::Default for Unicode {
#[inline]
fn default() -> Unicode {
Unicode {
keywords: ::core::default::Default::default(),
attributes: ::core::default::Default::default(),
}
}
}Default, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl ::core::hash::Hash for Unicode {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
::core::hash::Hash::hash(&self.keywords, state);
::core::hash::Hash::hash(&self.attributes, state)
}
}Hash)]
89#[allow(clippy::exhaustive_structs)] pub struct Unicode {
91 pub keywords: Keywords,
94 pub attributes: Attributes,
96}
97
98impl Unicode {
99 #[inline]
109 pub const fn new() -> Self {
110 Self {
111 keywords: Keywords::new(),
112 attributes: Attributes::new(),
113 }
114 }
115
116 #[inline]
121 #[cfg(feature = "alloc")]
122 pub fn try_from_str(s: &str) -> Result<Self, ParseError> {
123 Self::try_from_utf8(s.as_bytes())
124 }
125
126 #[cfg(feature = "alloc")]
130 pub fn try_from_utf8(code_units: &[u8]) -> Result<Self, ParseError> {
131 let mut iter = SubtagIterator::new(code_units);
132
133 let ext = iter.next().ok_or(ParseError::InvalidExtension)?;
134 if let ExtensionType::Unicode = ExtensionType::try_from_byte_slice(ext)? {
135 return Self::try_from_iter(&mut iter);
136 }
137
138 Err(ParseError::InvalidExtension)
139 }
140
141 pub fn is_empty(&self) -> bool {
153 self.keywords.is_empty() && self.attributes.is_empty()
154 }
155
156 pub fn clear(&mut self) {
170 self.keywords.clear();
171 self.attributes.clear();
172 }
173
174 pub(crate) fn as_tuple(&self) -> (&Attributes, &Keywords) {
175 (&self.attributes, &self.keywords)
176 }
177
178 pub fn total_cmp(&self, other: &Self) -> Ordering {
185 self.as_tuple().cmp(&other.as_tuple())
186 }
187
188 #[cfg(feature = "alloc")]
189 pub(crate) fn try_from_iter(iter: &mut SubtagIterator) -> Result<Self, ParseError> {
190 let attributes = Attributes::try_from_iter(iter)?;
191 let keywords = Keywords::try_from_iter(iter)?;
192
193 if attributes.is_empty() && keywords.is_empty() {
195 return Err(ParseError::InvalidExtension);
196 }
197
198 Ok(Self {
199 keywords,
200 attributes,
201 })
202 }
203
204 pub(crate) fn for_each_subtag_str<E, F>(&self, f: &mut F, with_ext: bool) -> Result<(), E>
205 where
206 F: FnMut(&str) -> Result<(), E>,
207 {
208 if !self.is_empty() {
209 if with_ext {
210 f(UNICODE_EXT_STR)?;
211 }
212 self.attributes.for_each_subtag_str(f)?;
213 self.keywords.for_each_subtag_str(f)?;
214 }
215 Ok(())
216 }
217
218 #[cfg(feature = "alloc")]
233 pub fn extend(&mut self, other: Unicode) {
234 self.keywords.extend_from_keywords(other.keywords);
235 self.attributes.extend_from_attributes(other.attributes);
236 }
237}
238
239#[cfg(feature = "alloc")]
241impl FromStr for Unicode {
242 type Err = ParseError;
243
244 #[inline]
245 fn from_str(s: &str) -> Result<Self, Self::Err> {
246 Self::try_from_str(s)
247 }
248}
249
250impl core::fmt::Display for Unicode {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
::writeable::Writeable::write_to(&self, f)
}
}writeable::impl_display_with_writeable!(Unicode, #[cfg(feature = "alloc")]);
251
252impl writeable::Writeable for Unicode {
253 fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result {
254 sink.write_char(UNICODE_EXT_CHAR)?;
255
256 if !self.attributes.is_empty() {
257 sink.write_char('-')?;
258 writeable::Writeable::write_to(&self.attributes, sink)?;
259 }
260 if !self.keywords.is_empty() {
261 sink.write_char('-')?;
262 writeable::Writeable::write_to(&self.keywords, sink)?;
263 }
264 Ok(())
265 }
266
267 fn writeable_length_hint(&self) -> writeable::LengthHint {
268 if self.is_empty() {
269 return writeable::LengthHint::exact(0);
270 }
271 let mut result = writeable::LengthHint::exact(1);
272 if !self.attributes.is_empty() {
273 result += writeable::Writeable::writeable_length_hint(&self.attributes) + 1;
274 }
275 if !self.keywords.is_empty() {
276 result += writeable::Writeable::writeable_length_hint(&self.keywords) + 1;
277 }
278 result
279 }
280}
281
282#[cfg(test)]
283mod tests {
284 use super::*;
285
286 #[test]
287 fn test_unicode_extension_fromstr() {
288 let ue: Unicode = "u-foo-hc-h12".parse().expect("Failed to parse Unicode");
289 assert_eq!(ue.to_string(), "u-foo-hc-h12");
290
291 let ue: Result<Unicode, _> = "u".parse();
292 assert!(ue.is_err());
293 }
294}