use {
crate::{
parsers::{Parse, ParserContext},
CustomParseError,
},
cssparser::{
serialize_identifier,
serialize_string,
BasicParseError,
BasicParseErrorKind,
ParseError,
Parser,
ToCss,
Token,
},
std::fmt,
};
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Symbol {
String(String),
Ident(String),
}
impl Parse for Symbol {
fn parse<'i, 't>(
_context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i, CustomParseError<'i>>> {
use self::Symbol::*;
match input.next() {
Ok(&Token::QuotedString(ref s)) => {
Ok(String(s.as_ref().to_owned()))
}
Ok(&Token::Ident(ref s)) => Ok(Ident(s.as_ref().to_owned())),
Ok(token) => Err(ParseError::from(BasicParseError {
kind: BasicParseErrorKind::UnexpectedToken(token.clone()),
location: input.state().source_location(),
})),
Err(e) => Err(e.into()),
}
}
}
impl ToCss for Symbol {
fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
use self::Symbol::*;
match *self {
String(ref string) => serialize_string(string, dest),
Ident(ref string) => serialize_identifier(string, dest),
}
}
}
impl Symbol {
pub fn is_allowed_in_symbols(&self) -> bool {
use self::Symbol::*;
match self {
&Ident(_) => false,
_ => true,
}
}
}