Skip to main content

css_parse/syntax/
unknown_rule_block.rs

1use crate::{
2	ComponentValues, Cursor, CursorSink, DeclarationValue, KindSet, NodeMetadata, NodeWithMetadata, Parse, Parser,
3	Peek, Result, SemanticEq, Span, ToCursors, ToSpan,
4};
5use csskit_proc_macro::node;
6
7/// Wrapper type for using ComponentValues as a rule type parameter in unknown rules.
8/// This implements RuleVariants to allow ComponentValues to be used as the block type
9/// for unknown qualified rules, where the rule structure is not recognized.
10#[node]
11#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(transparent))]
13pub struct UnknownRuleBlock<'a, D = ComponentValues<'a>, M = ()> {
14	pub values: ComponentValues<'a>,
15	#[cfg_attr(feature = "serde", serde(skip))]
16	_phantom: std::marker::PhantomData<(D, M)>,
17}
18
19impl<'a, D, M> Parse<'a> for UnknownRuleBlock<'a, D, M> {
20	fn parse<Iter>(p: &mut Parser<'a, Iter>) -> Result<Self>
21	where
22		Iter: Iterator<Item = Cursor> + Clone,
23	{
24		ComponentValues::parse(p).map(|values| Self { values, _phantom: std::marker::PhantomData })
25	}
26}
27
28impl<'a, D, M> Peek<'a> for UnknownRuleBlock<'a, D, M> {
29	const PEEK_KINDSET: KindSet = ComponentValues::PEEK_KINDSET;
30}
31
32impl<'a, D, M> ToCursors for UnknownRuleBlock<'a, D, M> {
33	fn to_cursors(&self, s: &mut impl CursorSink) {
34		self.values.to_cursors(s)
35	}
36}
37
38impl<'a, D, M> ToSpan for UnknownRuleBlock<'a, D, M> {
39	fn to_span(&self) -> Span {
40		self.values.to_span()
41	}
42}
43
44impl<'a, D, M> SemanticEq for UnknownRuleBlock<'a, D, M> {
45	fn semantic_eq(&self, other: &Self) -> bool {
46		self.values.semantic_eq(&other.values)
47	}
48}
49
50impl<'a, D, M: NodeMetadata> NodeWithMetadata<M> for UnknownRuleBlock<'a, D, M> {
51	fn metadata(&self) -> M {
52		self.values.metadata()
53	}
54}
55
56impl<'a, D, M> crate::RuleVariants<'a> for UnknownRuleBlock<'a, D, M>
57where
58	D: DeclarationValue<'a, M>,
59	M: NodeMetadata,
60{
61	type DeclarationValue = D;
62	type Metadata = M;
63
64	fn is_unknown(&self) -> bool {
65		// ComponentValues is a generic fallback container, not a specific rule type.
66		// It should be treated as unknown so it doesn't override actual declarations.
67		true
68	}
69}