Skip to main content

css_ast/types/
custom_ident.rs

1use super::prelude::*;
2
3/// <https://drafts.csswg.org/css-values/#custom-idents>
4///
5/// Wraps `T![Ident]`, but exists for the purposes of Visitable/VisitableMut.
6/// Excludes CSS-wide keywords: `initial`, `inherit`, `unset`, `revert`, `revert-layer`, `default`.
7#[node]
8#[derive(IntoCursor, ToSpan, SemanticEq, ToCursors, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
10#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
11#[derive(csskit_derives::NodeWithMetadata)]
12pub struct CustomIdent(T![Ident]);
13
14impl<'a> Peek<'a> for CustomIdent {
15	const PEEK_KINDSET: KindSet = KindSet::new(&[Kind::Ident]);
16
17	#[inline(always)]
18	fn peek<I>(p: &Parser<'a, I>, c: Cursor) -> bool
19	where
20		I: Iterator<Item = Cursor> + Clone,
21	{
22		if !<T![Ident]>::peek(p, c) {
23			return false;
24		}
25		!matches!(
26			p.to_atom::<CssAtomSet>(c),
27			CssAtomSet::Initial
28				| CssAtomSet::Inherit
29				| CssAtomSet::Unset
30				| CssAtomSet::Revert
31				| CssAtomSet::RevertLayer
32				| CssAtomSet::Default
33		)
34	}
35}
36
37impl<'a> Parse<'a> for CustomIdent {
38	fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
39	where
40		I: Iterator<Item = Cursor> + Clone,
41	{
42		let c = p.peek_n(1);
43		if !Self::peek(p, c) {
44			return Err(Diagnostic::new(c, Diagnostic::unexpected_ident));
45		}
46		Ok(Self(p.parse::<T![Ident]>()?))
47	}
48}