Skip to main content

oxc_css_parser/
config.rs

1#[cfg(feature = "config_serde")]
2use serde::{Deserialize, Serialize};
3
4/// Supported syntax.
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
6#[cfg_attr(feature = "config_serde", derive(Serialize, Deserialize))]
7#[cfg_attr(feature = "config_serde", serde(rename_all = "camelCase"))]
8pub enum Syntax {
9    #[default]
10    Css,
11    Scss,
12    /// Indented Sass Syntax
13    Sass,
14    Less,
15}
16
17/// Configuration for a backtick-delimited template placeholder.
18///
19/// A placeholder has the shape `` `<prefix><decimal index>` `` (e.g. with
20/// `prefix = "PLACEHOLDER-"`: `` `PLACEHOLDER-0` ``). The opening and closing
21/// backticks are fixed; only `prefix` is supplied by the downstream consumer, so
22/// oxc-css-parser stays agnostic to its content and only uses it to recognize and index
23/// the token.
24///
25/// Backtick is not valid CSS/SCSS/Sass syntax (it is only Less's inline-JS
26/// delimiter), so this is intended for SCSS parsing only; see
27/// [`ParserOptions::template_placeholder`].
28#[derive(Clone, Copy, Debug, PartialEq, Eq)]
29pub struct TemplatePlaceholder {
30    /// Text after the opening backtick that marks the start of a placeholder,
31    /// before its decimal index (e.g. `"PLACEHOLDER-"`).
32    pub prefix: &'static str,
33}
34
35/// Parser options for customizing parser behaviors.
36#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
37#[cfg_attr(feature = "config_serde", derive(Serialize, Deserialize))]
38#[cfg_attr(feature = "config_serde", serde(rename_all = "camelCase"))]
39pub struct ParserOptions {
40    /// If set, a backtick-delimited token of the shape `` `<prefix><decimal index>` ``
41    /// (see [`TemplatePlaceholder`]) is tokenized as an atomic
42    /// [`Token::Placeholder`](crate::token::Token) carrying the parsed index.
43    /// The token terminates at the closing backtick, so an immediately following
44    /// identifier (e.g. `` `PLACEHOLDER-0`px ``) is lexed as a separate suffix.
45    ///
46    /// This is designed for downstream formatters that substitute template
47    /// interpolations (e.g. CSS-in-JS `${expr}`) with such placeholders
48    /// before parsing, then re-substitute them in the output.
49    ///
50    /// Backtick is not valid CSS/SCSS/Sass syntax, so this MUST be used with
51    /// [`Syntax::Scss`] (the parser builder asserts this); in Less, backtick is
52    /// the inline-JS delimiter and would conflict.
53    ///
54    /// Not serialized: the affix is a `&'static str` supplied programmatically,
55    /// not loadable from a config file.
56    #[cfg_attr(feature = "config_serde", serde(skip))]
57    pub template_placeholder: Option<TemplatePlaceholder>,
58}