rspack_lightningcss/values/
ident.rs1use crate::error::{ParserError, PrinterError};
4use crate::properties::css_modules::Specifier;
5use crate::traits::{Parse, ParseWithOptions, ToCss};
6use crate::values::string::CowArcStr;
7#[cfg(feature = "visitor")]
8use crate::visitor::Visit;
9use cssparser::*;
10use smallvec::SmallVec;
11use std::borrow::Borrow;
12use std::ops::Deref;
13
14use super::string::impl_string_type;
15
16#[derive(Debug, Clone, Eq, Hash)]
22#[cfg_attr(feature = "visitor", derive(Visit))]
23#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
24#[cfg_attr(feature = "visitor", visit(visit_custom_ident, CUSTOM_IDENTS))]
25#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(transparent))]
26#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
27pub struct CustomIdent<'i>(#[cfg_attr(feature = "serde", serde(borrow))] pub CowArcStr<'i>);
28
29impl<'i> Parse<'i> for CustomIdent<'i> {
30 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
31 let location = input.current_source_location();
32 let ident = input.expect_ident()?;
33 let valid = match_ignore_ascii_case! { &ident,
34 "initial" | "inherit" | "unset" | "default" | "revert" | "revert-layer" => false,
35 _ => true
36 };
37
38 if !valid {
39 return Err(location.new_unexpected_token_error(Token::Ident(ident.clone())));
40 }
41
42 Ok(CustomIdent(ident.into()))
43 }
44}
45
46impl<'i> ToCss for CustomIdent<'i> {
47 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
48 self.to_css_with_options(dest, true)
49 }
50}
51
52impl<'i> CustomIdent<'i> {
53 pub(crate) fn to_css_with_options<PrinterT: crate::printer::PrinterTrait>(
55 &self,
56 dest: &mut PrinterT,
57 enabled_css_modules: bool,
58 ) -> Result<(), PrinterError> {
59 let css_module_custom_idents_enabled =
60 enabled_css_modules && dest.css_module_config().is_some_and(|config| config.custom_idents);
61 dest.write_ident(&self.0, css_module_custom_idents_enabled)
62 }
63}
64
65pub type CustomIdentList<'i> = SmallVec<[CustomIdent<'i>; 1]>;
67
68#[derive(Debug, Clone, PartialEq, Default)]
70#[cfg_attr(feature = "visitor", derive(Visit))]
71#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
72#[cfg_attr(
73 feature = "serde",
74 derive(serde::Serialize, serde::Deserialize),
75 serde(rename_all = "kebab-case")
76)]
77#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
78pub enum NoneOrCustomIdentList<'i> {
79 #[default]
81 None,
82 #[cfg_attr(feature = "serde", serde(borrow, untagged))]
84 Idents(SmallVec<[CustomIdent<'i>; 1]>),
85}
86
87impl<'i> Parse<'i> for NoneOrCustomIdentList<'i> {
88 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
89 let mut types = SmallVec::new();
90 loop {
91 if let Ok(ident) = input.try_parse(CustomIdent::parse) {
92 if ident == "none" {
93 if types.is_empty() {
94 return Ok(NoneOrCustomIdentList::None);
95 } else {
96 return Err(input.new_custom_error(ParserError::InvalidValue));
97 }
98 }
99
100 types.push(ident);
101 } else {
102 return Ok(NoneOrCustomIdentList::Idents(types));
103 }
104 }
105 }
106}
107
108impl<'i> ToCss for NoneOrCustomIdentList<'i> {
109 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
110 match self {
111 NoneOrCustomIdentList::None => {
112 dest.write_str("none")?;
113 Ok(())
114 }
115 NoneOrCustomIdentList::Idents(types) => {
116 let mut first = true;
117 for ident in types {
118 if !first {
119 dest.write_char(' ')?;
120 } else {
121 first = false;
122 }
123 ident.to_css(dest)?;
124 }
125 Ok(())
126 }
127 }
128 }
129}
130
131#[derive(Debug, Clone, Eq, Hash)]
136#[cfg_attr(feature = "visitor", derive(Visit))]
137#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
138#[cfg_attr(feature = "visitor", visit(visit_dashed_ident, DASHED_IDENTS))]
139#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(transparent))]
140#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
141pub struct DashedIdent<'i>(#[cfg_attr(feature = "serde", serde(borrow))] pub CowArcStr<'i>);
142
143impl<'i> Parse<'i> for DashedIdent<'i> {
144 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
145 let location = input.current_source_location();
146 let ident = input.expect_ident()?;
147 if !ident.starts_with("--") {
148 return Err(location.new_unexpected_token_error(Token::Ident(ident.clone())));
149 }
150
151 Ok(DashedIdent(ident.into()))
152 }
153}
154
155impl<'i> ToCss for DashedIdent<'i> {
156 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
157 dest.write_dashed_ident(&self.0, true)
158 }
159}
160
161#[cfg(feature = "serde")]
162impl<'i, 'de: 'i> serde::Deserialize<'de> for DashedIdent<'i> {
163 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
164 where
165 D: serde::Deserializer<'de>,
166 {
167 let ident = CowArcStr::deserialize(deserializer)?;
168 if !ident.starts_with("--") {
169 return Err(serde::de::Error::custom("Dashed idents must start with --"));
170 }
171
172 Ok(DashedIdent(ident))
173 }
174}
175
176#[derive(Debug, Clone, PartialEq)]
184#[cfg_attr(feature = "visitor", derive(Visit))]
185#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
186#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
187#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
188pub struct DashedIdentReference<'i> {
189 #[cfg_attr(feature = "serde", serde(borrow))]
191 pub ident: DashedIdent<'i>,
192 pub from: Option<Specifier<'i>>,
195}
196
197impl<'i> ParseWithOptions<'i> for DashedIdentReference<'i> {
198 fn parse_with_options<'t>(
199 input: &mut Parser<'i, 't>,
200 options: &crate::stylesheet::ParserOptions,
201 ) -> Result<Self, ParseError<'i, ParserError<'i>>> {
202 let ident = DashedIdent::parse(input)?;
203
204 let from = match &options.css_modules {
205 Some(config) if config.dashed_idents => {
206 if input.try_parse(|input| input.expect_ident_matching("from")).is_ok() {
207 Some(Specifier::parse(input)?)
208 } else {
209 None
210 }
211 }
212 _ => None,
213 };
214
215 Ok(DashedIdentReference { ident, from })
216 }
217}
218
219impl<'i> ToCss for DashedIdentReference<'i> {
220 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
221 let source_index = dest.loc().source_index;
222 if dest.css_module_config().is_some_and(|config| config.dashed_idents) {
223 if let Some(name) = dest
224 .with_css_module_mut(|css_module| css_module.reference_dashed(&self.ident.0, &self.from, source_index))
225 .flatten()
226 {
227 dest.write_str("--")?;
228 serialize_name(&name, dest)?;
229 return Ok(());
230 }
231 }
232
233 dest.write_dashed_ident(&self.ident.0, false)
234 }
235}
236
237#[derive(Debug, Clone, Eq, Hash, Default)]
239#[cfg_attr(feature = "visitor", derive(Visit))]
240#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
241#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(transparent))]
242#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
243pub struct Ident<'i>(#[cfg_attr(feature = "serde", serde(borrow))] pub CowArcStr<'i>);
244
245impl<'i> Parse<'i> for Ident<'i> {
246 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
247 let ident = input.expect_ident()?;
248 Ok(Ident(ident.into()))
249 }
250}
251
252impl<'i> ToCss for Ident<'i> {
253 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
254 serialize_identifier(&self.0, dest)?;
255 Ok(())
256 }
257}
258
259impl<'i> cssparser::ToCss for Ident<'i> {
260 fn to_css<W>(&self, dest: &mut W) -> std::fmt::Result
261 where
262 W: std::fmt::Write,
263 {
264 serialize_identifier(&self.0, dest)
265 }
266}
267
268impl_string_type!(Ident);
269impl_string_type!(CustomIdent);
270impl_string_type!(DashedIdent);