diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/unic-langid-impl/src/subtags/script.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/unic-langid-impl/src/subtags/script.rs')
-rw-r--r-- | vendor/unic-langid-impl/src/subtags/script.rs | 62 |
1 files changed, 62 insertions, 0 deletions
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 + } +} |