rspack-lightningcss 0.1.2

A CSS parser, transformer, and minifier
Documentation
//! An unknown at-rule.

use super::Location;
use crate::error::PrinterError;
use crate::properties::custom::TokenList;
use crate::traits::ToCss;
use crate::values::string::CowArcStr;
#[cfg(feature = "visitor")]
use crate::visitor::Visit;

/// An unknown at-rule, stored as raw tokens.
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub struct UnknownAtRule<'i> {
  /// The name of the at-rule (without the @).
  #[cfg_attr(feature = "serde", serde(borrow))]
  #[cfg_attr(feature = "visitor", skip_visit)]
  pub name: CowArcStr<'i>,
  /// The prelude of the rule.
  pub prelude: TokenList<'i>,
  /// The contents of the block, if any.
  pub block: Option<TokenList<'i>>,
  /// The location of the rule in the source file.
  #[cfg_attr(feature = "visitor", skip_visit)]
  pub loc: Location,
}

impl<'i> ToCss for UnknownAtRule<'i> {
  fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
    dest.add_mapping(self.loc);
    dest.write_char('@')?;
    dest.write_str(&self.name)?;

    if !self.prelude.0.is_empty() {
      dest.write_char(' ')?;
      self.prelude.to_css(dest, false)?;
    }

    if let Some(block) = &self.block {
      dest.whitespace()?;
      dest.write_char('{')?;
      dest.indent();
      dest.newline()?;
      block.to_css(dest, false)?;
      dest.dedent();
      dest.newline()?;
      dest.write_char('}')?;
      Ok(())
    } else {
      dest.write_char(';')?;
      Ok(())
    }
  }
}