use crate::error::{ParserError, PrinterError};
use crate::printer::Printer;
use crate::properties::css_modules::Specifier;
use crate::traits::{Parse, ParseWithOptions, ToCss};
use crate::values::string::CowArcStr;
use crate::visitor::Visit;
use cssparser::*;
use smallvec::SmallVec;
use std::borrow::Borrow;
use std::ops::Deref;
use super::string::impl_string_type;
#[derive(Debug, Clone, Eq, Hash, Visit)]
#[visit(visit_custom_ident, CUSTOM_IDENTS)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CustomIdent<'i>(#[cfg_attr(feature = "serde", serde(borrow))] pub CowArcStr<'i>);
impl<'i> Parse<'i> for CustomIdent<'i> {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let location = input.current_source_location();
let ident = input.expect_ident()?;
let valid = match_ignore_ascii_case! { &ident,
"initial" | "inherit" | "unset" | "default" | "revert" | "revert-layer" => false,
_ => true
};
if !valid {
return Err(location.new_unexpected_token_error(Token::Ident(ident.clone())));
}
Ok(CustomIdent(ident.into()))
}
}
impl<'i> ToCss for CustomIdent<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
dest.write_ident(&self.0)
}
}
pub type CustomIdentList<'i> = SmallVec<[CustomIdent<'i>; 1]>;
#[derive(Debug, Clone, Eq, Hash, Visit)]
#[visit(visit_dashed_ident, DASHED_IDENTS)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DashedIdent<'i>(#[cfg_attr(feature = "serde", serde(borrow))] pub CowArcStr<'i>);
impl<'i> Parse<'i> for DashedIdent<'i> {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let location = input.current_source_location();
let ident = input.expect_ident()?;
if !ident.starts_with("--") {
return Err(location.new_unexpected_token_error(Token::Ident(ident.clone())));
}
Ok(DashedIdent(ident.into()))
}
}
impl<'i> ToCss for DashedIdent<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
dest.write_dashed_ident(&self.0, true)
}
}
#[derive(Debug, Clone, PartialEq, Visit)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DashedIdentReference<'i> {
#[cfg_attr(feature = "serde", serde(borrow))]
pub ident: DashedIdent<'i>,
pub from: Option<Specifier<'i>>,
}
impl<'i, T> ParseWithOptions<'i, T> for DashedIdentReference<'i> {
fn parse_with_options<'t>(
input: &mut Parser<'i, 't>,
options: &crate::stylesheet::ParserOptions<T>,
) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let ident = DashedIdent::parse(input)?;
let from = match &options.css_modules {
Some(config) if config.dashed_idents => {
if input.try_parse(|input| input.expect_ident_matching("from")).is_ok() {
Some(Specifier::parse(input)?)
} else {
None
}
}
_ => None,
};
Ok(DashedIdentReference { ident, from })
}
}
impl<'i> ToCss for DashedIdentReference<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
match &mut dest.css_module {
Some(css_module) if css_module.config.dashed_idents => {
if let Some(name) = css_module.reference_dashed(&self.ident.0, &self.from, dest.loc.source_index) {
dest.write_str("--")?;
serialize_name(&name, dest)?;
return Ok(());
}
}
_ => {}
}
dest.write_dashed_ident(&self.ident.0, false)
}
}
#[derive(Debug, Clone, Eq, Hash, Default, Visit)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Ident<'i>(#[cfg_attr(feature = "serde", serde(borrow))] pub CowArcStr<'i>);
impl<'i> Parse<'i> for Ident<'i> {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let ident = input.expect_ident()?;
Ok(Ident(ident.into()))
}
}
impl<'i> ToCss for Ident<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
serialize_identifier(&self.0, dest)?;
Ok(())
}
}
impl<'i> cssparser::ToCss for Ident<'i> {
fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result
where
W: std::fmt::Write,
{
serialize_identifier(&self.0, dest)
}
}
impl_string_type!(Ident);
impl_string_type!(CustomIdent);
impl_string_type!(DashedIdent);