summaryrefslogtreecommitdiffstats
path: root/vendor/unic-langid-impl/src/subtags
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/unic-langid-impl/src/subtags')
-rw-r--r--vendor/unic-langid-impl/src/subtags/language.rs108
-rw-r--r--vendor/unic-langid-impl/src/subtags/mod.rs9
-rw-r--r--vendor/unic-langid-impl/src/subtags/region.rs74
-rw-r--r--vendor/unic-langid-impl/src/subtags/script.rs62
-rw-r--r--vendor/unic-langid-impl/src/subtags/variant.rs78
5 files changed, 331 insertions, 0 deletions
diff --git a/vendor/unic-langid-impl/src/subtags/language.rs b/vendor/unic-langid-impl/src/subtags/language.rs
new file mode 100644
index 000000000..3703475e2
--- /dev/null
+++ b/vendor/unic-langid-impl/src/subtags/language.rs
@@ -0,0 +1,108 @@
+use crate::parser::errors::ParserError;
+use std::borrow::Borrow;
+use std::convert::TryFrom;
+use std::str::FromStr;
+use tinystr::TinyStr8;
+
+#[derive(Default, Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord, Copy)]
+pub struct Language(Option<TinyStr8>);
+
+impl Language {
+ pub fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
+ let slen = v.len();
+
+ let s = TinyStr8::from_bytes(v).map_err(|_| ParserError::InvalidLanguage)?;
+ if slen < 2 || slen > 8 || slen == 4 || !s.is_ascii_alphabetic() {
+ return Err(ParserError::InvalidLanguage);
+ }
+
+ let value = s.to_ascii_lowercase();
+
+ if value == "und" {
+ Ok(Self(None))
+ } else {
+ Ok(Self(Some(value)))
+ }
+ }
+
+ pub fn as_str(&self) -> &str {
+ self.0.as_deref().unwrap_or("und")
+ }
+
+ /// # Safety
+ ///
+ /// This function accepts any u64 that is exected to be a valid
+ /// `TinyStr8` and a valid `Language` subtag.
+ pub const unsafe fn from_raw_unchecked(v: u64) -> Self {
+ Self(Some(TinyStr8::new_unchecked(v)))
+ }
+
+ pub fn matches<O: Borrow<Self>>(
+ self,
+ other: O,
+ self_as_range: bool,
+ other_as_range: bool,
+ ) -> bool {
+ (self_as_range && self.0.is_none())
+ || (other_as_range && other.borrow().0.is_none())
+ || self == *other.borrow()
+ }
+
+ pub fn clear(&mut self) {
+ self.0 = None;
+ }
+
+ pub fn is_empty(self) -> bool {
+ self.0.is_none()
+ }
+}
+
+impl From<Language> for Option<u64> {
+ fn from(input: Language) -> Self {
+ input.0.map(|i| i.into())
+ }
+}
+
+impl From<&Language> for Option<u64> {
+ fn from(input: &Language) -> Self {
+ input.0.map(|i| i.into())
+ }
+}
+
+impl<T> TryFrom<Option<T>> for Language
+where
+ T: AsRef<[u8]>,
+{
+ type Error = ParserError;
+
+ fn try_from(v: Option<T>) -> Result<Self, Self::Error> {
+ match v {
+ Some(l) => Ok(Self::from_bytes(l.as_ref())?),
+ None => Ok(Self(None)),
+ }
+ }
+}
+
+impl FromStr for Language {
+ type Err = ParserError;
+
+ fn from_str(source: &str) -> Result<Self, Self::Err> {
+ Self::from_bytes(source.as_bytes())
+ }
+}
+
+impl std::fmt::Display for Language {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+ if let Some(ref lang) = self.0 {
+ f.write_str(lang)
+ } else {
+ f.write_str("und")
+ }
+ }
+}
+
+impl PartialEq<&str> for Language {
+ fn eq(&self, other: &&str) -> bool {
+ self.as_str() == *other
+ }
+}
diff --git a/vendor/unic-langid-impl/src/subtags/mod.rs b/vendor/unic-langid-impl/src/subtags/mod.rs
new file mode 100644
index 000000000..2d0a7e3a4
--- /dev/null
+++ b/vendor/unic-langid-impl/src/subtags/mod.rs
@@ -0,0 +1,9 @@
+mod language;
+mod region;
+mod script;
+mod variant;
+
+pub use language::Language;
+pub use region::Region;
+pub use script::Script;
+pub use variant::Variant;
diff --git a/vendor/unic-langid-impl/src/subtags/region.rs b/vendor/unic-langid-impl/src/subtags/region.rs
new file mode 100644
index 000000000..f14ac21bf
--- /dev/null
+++ b/vendor/unic-langid-impl/src/subtags/region.rs
@@ -0,0 +1,74 @@
+use crate::parser::errors::ParserError;
+use std::str::FromStr;
+use tinystr::TinyStr4;
+
+#[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord, Copy)]
+pub struct Region(TinyStr4);
+
+impl Region {
+ pub fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
+ let slen = v.len();
+
+ match slen {
+ 2 => {
+ let s = TinyStr4::from_bytes(v).map_err(|_| ParserError::InvalidSubtag)?;
+ if !s.is_ascii_alphabetic() {
+ return Err(ParserError::InvalidSubtag);
+ }
+ Ok(Self(s.to_ascii_uppercase()))
+ }
+ 3 => {
+ let s = TinyStr4::from_bytes(v).map_err(|_| ParserError::InvalidSubtag)?;
+ if !s.is_ascii_numeric() {
+ return Err(ParserError::InvalidSubtag);
+ }
+ Ok(Self(s))
+ }
+ _ => Err(ParserError::InvalidSubtag),
+ }
+ }
+
+ pub fn as_str(&self) -> &str {
+ self.0.as_str()
+ }
+
+ /// # Safety
+ ///
+ /// This function accepts any u64 that is exected to be a valid
+ /// `TinyStr4` and a valid `Region` subtag.
+ pub const unsafe fn from_raw_unchecked(v: u32) -> Self {
+ Self(TinyStr4::new_unchecked(v))
+ }
+}
+
+impl From<Region> for u32 {
+ fn from(input: Region) -> Self {
+ input.0.into()
+ }
+}
+
+impl<'l> From<&'l Region> for &'l str {
+ fn from(input: &'l Region) -> Self {
+ input.0.as_str()
+ }
+}
+
+impl FromStr for Region {
+ type Err = ParserError;
+
+ fn from_str(source: &str) -> Result<Self, Self::Err> {
+ Self::from_bytes(source.as_bytes())
+ }
+}
+
+impl std::fmt::Display for Region {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+ f.write_str(&self.0)
+ }
+}
+
+impl PartialEq<&str> for Region {
+ fn eq(&self, other: &&str) -> bool {
+ self.as_str() == *other
+ }
+}
diff --git a/vendor/unic-langid-impl/src/subtags/script.rs b/vendor/unic-langid-impl/src/subtags/script.rs
new file mode 100644
index 000000000..63780e510
--- /dev/null
+++ b/vendor/unic-langid-impl/src/subtags/script.rs
@@ -0,0 +1,62 @@
+use crate::parser::errors::ParserError;
+use std::str::FromStr;
+use tinystr::TinyStr4;
+
+#[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord, Copy)]
+pub struct Script(TinyStr4);
+
+impl Script {
+ pub fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
+ let slen = v.len();
+
+ let s = TinyStr4::from_bytes(v).map_err(|_| ParserError::InvalidSubtag)?;
+ if slen != 4 || !s.is_ascii_alphabetic() {
+ return Err(ParserError::InvalidSubtag);
+ }
+ Ok(Self(s.to_ascii_titlecase()))
+ }
+
+ pub fn as_str(&self) -> &str {
+ self.0.as_str()
+ }
+
+ /// # Safety
+ ///
+ /// This function accepts any u64 that is exected to be a valid
+ /// `TinyStr4` and a valid `Script` subtag.
+ pub const unsafe fn from_raw_unchecked(v: u32) -> Self {
+ Self(TinyStr4::new_unchecked(v))
+ }
+}
+
+impl From<Script> for u32 {
+ fn from(input: Script) -> Self {
+ input.0.into()
+ }
+}
+
+impl<'l> From<&'l Script> for &'l str {
+ fn from(input: &'l Script) -> Self {
+ input.0.as_str()
+ }
+}
+
+impl FromStr for Script {
+ type Err = ParserError;
+
+ fn from_str(source: &str) -> Result<Self, Self::Err> {
+ Self::from_bytes(source.as_bytes())
+ }
+}
+
+impl std::fmt::Display for Script {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+ f.write_str(&self.0)
+ }
+}
+
+impl PartialEq<&str> for Script {
+ fn eq(&self, other: &&str) -> bool {
+ self.as_str() == *other
+ }
+}
diff --git a/vendor/unic-langid-impl/src/subtags/variant.rs b/vendor/unic-langid-impl/src/subtags/variant.rs
new file mode 100644
index 000000000..064ff3bb9
--- /dev/null
+++ b/vendor/unic-langid-impl/src/subtags/variant.rs
@@ -0,0 +1,78 @@
+use crate::parser::errors::ParserError;
+use std::str::FromStr;
+use tinystr::TinyStr8;
+
+#[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord, Copy)]
+pub struct Variant(TinyStr8);
+
+impl Variant {
+ pub fn from_bytes(v: &[u8]) -> Result<Self, ParserError> {
+ let slen = v.len();
+
+ if slen < 4 || slen > 8 {
+ return Err(ParserError::InvalidSubtag);
+ }
+
+ let s = TinyStr8::from_bytes(v).map_err(|_| ParserError::InvalidSubtag)?;
+
+ if (slen >= 5 && !s.is_ascii_alphanumeric())
+ || (slen == 4
+ && !v[0].is_ascii_digit()
+ && v[1..].iter().any(|c: &u8| !c.is_ascii_alphanumeric()))
+ {
+ return Err(ParserError::InvalidSubtag);
+ }
+
+ Ok(Self(s.to_ascii_lowercase()))
+ }
+
+ pub fn as_str(&self) -> &str {
+ self.0.as_str()
+ }
+
+ /// # Safety
+ ///
+ /// This function accepts any u64 that is exected to be a valid
+ /// `TinyStr8` and a valid `Variant` subtag.
+ pub const unsafe fn from_raw_unchecked(v: u64) -> Self {
+ Self(TinyStr8::new_unchecked(v))
+ }
+}
+
+impl From<Variant> for u64 {
+ fn from(input: Variant) -> Self {
+ input.0.into()
+ }
+}
+
+impl From<&Variant> for u64 {
+ fn from(input: &Variant) -> Self {
+ input.0.into()
+ }
+}
+
+impl FromStr for Variant {
+ type Err = ParserError;
+
+ fn from_str(source: &str) -> Result<Self, Self::Err> {
+ Self::from_bytes(source.as_bytes())
+ }
+}
+
+impl std::fmt::Display for Variant {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+ f.write_str(&self.0)
+ }
+}
+
+impl PartialEq<&str> for Variant {
+ fn eq(&self, other: &&str) -> bool {
+ self.as_str() == *other
+ }
+}
+
+impl PartialEq<str> for Variant {
+ fn eq(&self, other: &str) -> bool {
+ self.as_str() == other
+ }
+}