mod attribute;
mod attributes;
mod key;
mod keywords;
mod subdivision;
mod value;
use core::cmp::Ordering;
#[cfg(feature = "alloc")]
use core::str::FromStr;
#[doc(inline)]
pub use attribute::{attribute, Attribute};
pub use attributes::Attributes;
#[doc(inline)]
pub use key::{key, Key};
pub use keywords::Keywords;
#[doc(inline)]
pub use subdivision::{subdivision_suffix, SubdivisionId, SubdivisionSuffix};
#[doc(inline)]
pub use value::{value, Value};
#[cfg(feature = "alloc")]
use super::ExtensionType;
#[cfg(feature = "alloc")]
use crate::parser::ParseError;
#[cfg(feature = "alloc")]
use crate::parser::SubtagIterator;
pub(crate) const UNICODE_EXT_CHAR: char = 'u';
pub(crate) const UNICODE_EXT_STR: &str = "u";
#[derive(Clone, PartialEq, Eq, Debug, Default, Hash)]
#[allow(clippy::exhaustive_structs)] pub struct Unicode {
pub keywords: Keywords,
pub attributes: Attributes,
}
impl Unicode {
#[inline]
pub const fn new() -> Self {
Self {
keywords: Keywords::new(),
attributes: Attributes::new(),
}
}
#[inline]
#[cfg(feature = "alloc")]
pub fn try_from_str(s: &str) -> Result<Self, ParseError> {
Self::try_from_utf8(s.as_bytes())
}
#[cfg(feature = "alloc")]
pub fn try_from_utf8(code_units: &[u8]) -> Result<Self, ParseError> {
let mut iter = SubtagIterator::new(code_units);
let ext = iter.next().ok_or(ParseError::InvalidExtension)?;
if let ExtensionType::Unicode = ExtensionType::try_from_byte_slice(ext)? {
return Self::try_from_iter(&mut iter);
}
Err(ParseError::InvalidExtension)
}
pub fn is_empty(&self) -> bool {
self.keywords.is_empty() && self.attributes.is_empty()
}
pub fn clear(&mut self) {
self.keywords.clear();
self.attributes.clear();
}
pub(crate) fn as_tuple(&self) -> (&Attributes, &Keywords) {
(&self.attributes, &self.keywords)
}
pub fn total_cmp(&self, other: &Self) -> Ordering {
self.as_tuple().cmp(&other.as_tuple())
}
#[cfg(feature = "alloc")]
pub(crate) fn try_from_iter(iter: &mut SubtagIterator) -> Result<Self, ParseError> {
let attributes = Attributes::from_iter(iter);
let keywords = Keywords::try_from_iter(iter)?;
if attributes.is_empty() && keywords.is_empty() {
return Err(ParseError::InvalidExtension);
}
Ok(Self {
keywords,
attributes,
})
}
pub(crate) fn for_each_subtag_str<E, F>(&self, f: &mut F, with_ext: bool) -> Result<(), E>
where
F: FnMut(&str) -> Result<(), E>,
{
if !self.is_empty() {
if with_ext {
f(UNICODE_EXT_STR)?;
}
self.attributes.for_each_subtag_str(f)?;
self.keywords.for_each_subtag_str(f)?;
}
Ok(())
}
#[cfg(feature = "alloc")]
pub fn extend(&mut self, other: Unicode) {
self.keywords.extend_from_keywords(other.keywords);
self.attributes.extend_from_attributes(other.attributes);
}
}
#[cfg(feature = "alloc")]
impl FromStr for Unicode {
type Err = ParseError;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::try_from_str(s)
}
}
writeable::impl_display_with_writeable!(Unicode, #[cfg(feature = "alloc")]);
impl writeable::Writeable for Unicode {
fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result {
sink.write_char(UNICODE_EXT_CHAR)?;
if !self.attributes.is_empty() {
sink.write_char('-')?;
writeable::Writeable::write_to(&self.attributes, sink)?;
}
if !self.keywords.is_empty() {
sink.write_char('-')?;
writeable::Writeable::write_to(&self.keywords, sink)?;
}
Ok(())
}
fn writeable_length_hint(&self) -> writeable::LengthHint {
if self.is_empty() {
return writeable::LengthHint::exact(0);
}
let mut result = writeable::LengthHint::exact(1);
if !self.attributes.is_empty() {
result += writeable::Writeable::writeable_length_hint(&self.attributes) + 1;
}
if !self.keywords.is_empty() {
result += writeable::Writeable::writeable_length_hint(&self.keywords) + 1;
}
result
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_unicode_extension_fromstr() {
let ue: Unicode = "u-foo-hc-h12".parse().expect("Failed to parse Unicode");
assert_eq!(ue.to_string(), "u-foo-hc-h12");
let ue: Result<Unicode, _> = "u".parse();
assert!(ue.is_err());
}
}