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#[derive(IntoCursor, ToCursors, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
9#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
10#[derive(csskit_derives::NodeWithMetadata)]
11pub struct CustomIdent(T![Ident]);
12
13impl<'a> Peek<'a> for CustomIdent {
14	fn peek<I>(p: &Parser<'a, I>, c: Cursor) -> bool
15	where
16		I: Iterator<Item = Cursor> + Clone,
17	{
18		if !<T![Ident]>::peek(p, c) {
19			return false;
20		}
21		!matches!(
22			p.to_atom::<CssAtomSet>(c),
23			CssAtomSet::Initial
24				| CssAtomSet::Inherit
25				| CssAtomSet::Unset
26				| CssAtomSet::Revert
27				| CssAtomSet::RevertLayer
28				| CssAtomSet::Default
29		)
30	}
31}
32
33impl<'a> Parse<'a> for CustomIdent {
34	fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
35	where
36		I: Iterator<Item = Cursor> + Clone,
37	{
38		let c = p.peek_n(1);
39		if !Self::peek(p, c) {
40			return Err(Diagnostic::new(c, Diagnostic::unexpected_ident));
41		}
42		Ok(Self(p.parse::<T![Ident]>()?))
43	}
44}