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
// 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/master/LICENSE ). use std::ops::RangeInclusive; use std::str::FromStr; use crate::parser::errors::ParserError; use tinystr::TinyStr8; /// A single item used in a list of [`Private`] extensions. /// /// The key has to be an ASCII alphanumerical string no shorter than /// one character and no longer than eight. /// /// # Examples /// /// ``` /// use icu_locid::extensions::private::Key; /// /// let key1: Key = "Foo".parse() /// .expect("Failed to parse a Key."); /// /// assert_eq!(key1.as_str(), "foo"); /// ``` /// [`Private`]: ./struct.Private.html #[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord, Copy)] pub struct Key(TinyStr8); const KEY_LENGTH: RangeInclusive<usize> = 1..=8; impl Key { /// A constructor which takes a utf8 slice, parses it and /// produces a well-formed `Key`. /// /// # Examples /// /// ``` /// use icu_locid::extensions::private::Key; /// /// let key = Key::from_bytes(b"foobar") /// .expect("Parsing failed."); /// /// assert_eq!(key.as_str(), "foobar"); /// ``` pub fn from_bytes(v: &[u8]) -> Result<Self, ParserError> { if !KEY_LENGTH.contains(&v.len()) { return Err(ParserError::InvalidExtension); } let s = TinyStr8::from_bytes(v).map_err(|_| ParserError::InvalidExtension)?; if !s.is_ascii_alphanumeric() { return Err(ParserError::InvalidExtension); } Ok(Self(s.to_ascii_lowercase())) } /// A helper function for displaying /// a `Key` as a `&str`. /// /// # Examples /// /// ``` /// use icu_locid::extensions::private::Key; /// /// let key = Key::from_bytes(b"foobar") /// .expect("Parsing failed."); /// /// assert_eq!(key.as_str(), "foobar"); /// ``` /// /// `Notice`: For many use cases, such as comparison, /// `Key` implements `PartialEq<&str>` which allows for direct comparisons. pub fn as_str(&self) -> &str { self.0.as_str() } } impl FromStr for Key { type Err = ParserError; fn from_str(source: &str) -> Result<Self, Self::Err> { Self::from_bytes(source.as_bytes()) } } impl std::fmt::Display for Key { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { f.write_str(&self.0) } }