use {
crate::{
domain::{Atom, CustomIdent},
CustomParseError,
},
cssparser::{
BasicParseError,
BasicParseErrorKind,
ParseError,
Parser,
ToCss,
Token,
},
std::{
fmt,
hash::{Hash, Hasher},
},
};
#[derive(Debug, Clone)]
pub enum KeyframesName {
Ident(CustomIdent),
QuotedString(Atom),
}
impl KeyframesName {
pub fn from_ident(value: &str) -> Self {
let custom_ident =
CustomIdent::from_ident(&value.into(), &["none"]).ok();
match custom_ident {
Some(ident) => KeyframesName::Ident(ident),
None => KeyframesName::QuotedString(value.into()),
}
}
pub fn as_atom(&self) -> &Atom {
match *self {
KeyframesName::Ident(ref ident) => &ident.0,
KeyframesName::QuotedString(ref atom) => atom,
}
}
}
impl PartialEq for KeyframesName {
fn eq(&self, other: &Self) -> bool {
self.as_atom() == other.as_atom()
}
}
impl Eq for KeyframesName {}
impl Hash for KeyframesName {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_atom().hash(state)
}
}
impl ToCss for KeyframesName {
fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
match *self {
KeyframesName::Ident(ref ident) => ident.to_css(dest),
KeyframesName::QuotedString(ref atom) => atom.to_css(dest),
}
}
}
impl KeyframesName {
pub(crate) fn parse<'i, 't>(
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i, CustomParseError<'i>>> {
match input.next() {
Ok(&Token::Ident(ref s)) => {
Ok(KeyframesName::Ident(CustomIdent::from_ident(s, &["none"])?))
}
Ok(&Token::QuotedString(ref s)) => {
Ok(KeyframesName::QuotedString(Atom::from(s.as_ref())))
}
Ok(t) => Err(ParseError::from(BasicParseError {
kind: BasicParseErrorKind::UnexpectedToken(t.clone()),
location: input.state().source_location(),
})),
Err(e) => Err(e.into()),
}
}
}