mod attribute;
mod attributes;
mod key;
mod keywords;
mod value;
use alloc::vec;
pub use attribute::Attribute;
pub use attributes::Attributes;
pub use key::Key;
pub use keywords::Keywords;
pub use value::Value;
use crate::parser::ParserError;
use crate::parser::SubtagIterator;
use litemap::LiteMap;
#[derive(Clone, PartialEq, Eq, Debug, Default, Hash, PartialOrd, Ord)]
#[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(),
}
}
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 try_from_iter(iter: &mut SubtagIterator) -> Result<Self, ParserError> {
let mut attributes = vec![];
let mut keywords = LiteMap::new();
let mut current_keyword = None;
let mut current_type = vec![];
while let Some(subtag) = iter.peek() {
if let Ok(attr) = Attribute::try_from_bytes(subtag) {
if let Err(idx) = attributes.binary_search(&attr) {
attributes.insert(idx, attr);
}
} else {
break;
}
iter.next();
}
while let Some(subtag) = iter.peek() {
let slen = subtag.len();
if slen == 2 {
if let Some(kw) = current_keyword.take() {
keywords.try_insert(kw, Value::from_vec_unchecked(current_type));
current_type = vec![];
}
current_keyword = Some(Key::try_from_bytes(subtag)?);
} else if current_keyword.is_some() {
match Value::parse_subtag(subtag) {
Ok(Some(t)) => current_type.push(t),
Ok(None) => {}
Err(_) => break,
}
} else {
break;
}
iter.next();
}
if let Some(kw) = current_keyword.take() {
keywords.try_insert(kw, Value::from_vec_unchecked(current_type));
}
if attributes.is_empty() && keywords.is_empty() {
return Err(ParserError::InvalidExtension);
}
Ok(Self {
keywords: keywords.into(),
attributes: Attributes::from_vec_unchecked(attributes),
})
}
pub(crate) fn for_each_subtag_str<E, F>(&self, f: &mut F) -> Result<(), E>
where
F: FnMut(&str) -> Result<(), E>,
{
if self.is_empty() {
return Ok(());
}
f("u")?;
self.attributes.for_each_subtag_str(f)?;
self.keywords.for_each_subtag_str(f)?;
Ok(())
}
}
writeable::impl_display_with_writeable!(Unicode);
impl writeable::Writeable for Unicode {
fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result {
if self.is_empty() {
return Ok(());
}
sink.write_str("u")?;
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
}
}