Skip to main content

rspack_lightningcss/rules/
unknown.rs

1//! An unknown at-rule.
2
3use super::Location;
4use crate::error::PrinterError;
5use crate::properties::custom::TokenList;
6use crate::traits::ToCss;
7use crate::values::string::CowArcStr;
8#[cfg(feature = "visitor")]
9use crate::visitor::Visit;
10
11/// An unknown at-rule, stored as raw tokens.
12#[derive(Debug, PartialEq, Clone)]
13#[cfg_attr(feature = "visitor", derive(Visit))]
14#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
17pub struct UnknownAtRule<'i> {
18  /// The name of the at-rule (without the @).
19  #[cfg_attr(feature = "serde", serde(borrow))]
20  #[cfg_attr(feature = "visitor", skip_visit)]
21  pub name: CowArcStr<'i>,
22  /// The prelude of the rule.
23  pub prelude: TokenList<'i>,
24  /// The contents of the block, if any.
25  pub block: Option<TokenList<'i>>,
26  /// The location of the rule in the source file.
27  #[cfg_attr(feature = "visitor", skip_visit)]
28  pub loc: Location,
29}
30
31impl<'i> ToCss for UnknownAtRule<'i> {
32  fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
33    #[cfg(feature = "custom_sourcemap")]
34    dest.add_mapping(self.loc);
35    dest.write_char('@')?;
36    dest.write_str(&self.name)?;
37
38    if !self.prelude.0.is_empty() {
39      dest.write_char(' ')?;
40      self.prelude.to_css(dest, false)?;
41    }
42
43    if let Some(block) = &self.block {
44      dest.whitespace()?;
45      dest.write_char('{')?;
46      dest.indent();
47      dest.newline()?;
48      block.to_css(dest, false)?;
49      dest.dedent();
50      dest.newline()?;
51      dest.write_char('}')?;
52      Ok(())
53    } else {
54      dest.write_char(';')?;
55      Ok(())
56    }
57  }
58}