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)]
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]
119 #[cfg(feature = "alloc")]
120 pub fn try_from_str(s: &str) -> Result<Self, ParseError> {
121 Self::try_from_utf8(s.as_bytes())
122 }
123
124 #[cfg(feature = "alloc")]
126 pub fn try_from_utf8(code_units: &[u8]) -> Result<Self, ParseError> {
127 let mut iter = SubtagIterator::new(code_units);
128
129 let ext = iter.next().ok_or(ParseError::InvalidExtension)?;
130 if let ExtensionType::Unicode = ExtensionType::try_from_byte_slice(ext)? {
131 return Self::try_from_iter(&mut iter);
132 }
133
134 Err(ParseError::InvalidExtension)
135 }
136
137 pub fn is_empty(&self) -> bool {
149 self.keywords.is_empty() && self.attributes.is_empty()
150 }
151
152 pub fn clear(&mut self) {
166 self.keywords.clear();
167 self.attributes.clear();
168 }
169
170 pub(crate) fn as_tuple(&self) -> (&Attributes, &Keywords) {
171 (&self.attributes, &self.keywords)
172 }
173
174 pub fn total_cmp(&self, other: &Self) -> Ordering {
181 self.as_tuple().cmp(&other.as_tuple())
182 }
183
184 #[cfg(feature = "alloc")]
185 pub(crate) fn try_from_iter(iter: &mut SubtagIterator) -> Result<Self, ParseError> {
186 let attributes = Attributes::try_from_iter(iter)?;
187 let keywords = Keywords::try_from_iter(iter)?;
188
189 if attributes.is_empty() && keywords.is_empty() {
191 return Err(ParseError::InvalidExtension);
192 }
193
194 Ok(Self {
195 keywords,
196 attributes,
197 })
198 }
199
200 pub(crate) fn for_each_subtag_str<E, F>(&self, f: &mut F, with_ext: bool) -> Result<(), E>
201 where
202 F: FnMut(&str) -> Result<(), E>,
203 {
204 if !self.is_empty() {
205 if with_ext {
206 f(UNICODE_EXT_STR)?;
207 }
208 self.attributes.for_each_subtag_str(f)?;
209 self.keywords.for_each_subtag_str(f)?;
210 }
211 Ok(())
212 }
213}
214
215#[cfg(feature = "alloc")]
216impl FromStr for Unicode {
217 type Err = ParseError;
218
219 #[inline]
220 fn from_str(s: &str) -> Result<Self, Self::Err> {
221 Self::try_from_str(s)
222 }
223}
224
225impl core::fmt::Display for Unicode {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
::writeable::Writeable::write_to(&self, f)
}
}
impl Unicode {
pub fn to_string(&self) -> ::writeable::_internal::String {
::writeable::Writeable::write_to_string(self).into_owned()
}
}writeable::impl_display_with_writeable!(Unicode);
226
227impl writeable::Writeable for Unicode {
228 fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result {
229 sink.write_char(UNICODE_EXT_CHAR)?;
230
231 if !self.attributes.is_empty() {
232 sink.write_char('-')?;
233 writeable::Writeable::write_to(&self.attributes, sink)?;
234 }
235 if !self.keywords.is_empty() {
236 sink.write_char('-')?;
237 writeable::Writeable::write_to(&self.keywords, sink)?;
238 }
239 Ok(())
240 }
241
242 fn writeable_length_hint(&self) -> writeable::LengthHint {
243 if self.is_empty() {
244 return writeable::LengthHint::exact(0);
245 }
246 let mut result = writeable::LengthHint::exact(1);
247 if !self.attributes.is_empty() {
248 result += writeable::Writeable::writeable_length_hint(&self.attributes) + 1;
249 }
250 if !self.keywords.is_empty() {
251 result += writeable::Writeable::writeable_length_hint(&self.keywords) + 1;
252 }
253 result
254 }
255}
256
257#[cfg(test)]
258mod tests {
259 use super::*;
260
261 #[test]
262 fn test_unicode_extension_fromstr() {
263 let ue: Unicode = "u-foo-hc-h12".parse().expect("Failed to parse Unicode");
264 assert_eq!(ue.to_string(), "u-foo-hc-h12");
265
266 let ue: Result<Unicode, _> = "u".parse();
267 assert!(ue.is_err());
268 }
269}