css_parse/traits/node_metadata.rs
1/// Aggregated metadata for nodes, that can propagate up a node tree.
2pub trait NodeMetadata: Sized + Copy + Default {
3 /// Merges another NodeMetadata into this one, returning the result.
4 fn merge(self, other: Self) -> Self;
5
6 /// Sets the size of this metadata (e.g., number of declarations, selector list length).
7 /// Default implementation is a no-op for metadata types that don't track size.
8 fn with_size(self, _size: u16) -> Self {
9 self
10 }
11}
12
13/// A Node that has NodeMetadata
14pub trait NodeWithMetadata<M: NodeMetadata> {
15 /// Returns the metadata contributed by this node itself, not including children.
16 /// Most nodes don't contribute metadata, so can simply return `M::default()`.
17 /// Nodes like StyleRule or AtRules should return their own node kind flags here.
18 fn self_metadata(&self) -> M {
19 M::default()
20 }
21
22 /// Returns the complete aggregated metadata for this node (self + children).
23 /// Default implementation merges children's metadata with self_metadata().
24 fn metadata(&self) -> M;
25}
26
27// Stub implementation allowing tests to use () for M
28impl NodeMetadata for () {
29 fn merge(self, _: Self) -> Self {}
30}
31
32// Blanket implementation for Option<T> where T: NodeWithMetadata<M>
33// Returns default metadata when None, or delegates to the inner value when Some
34impl<M: NodeMetadata, T: NodeWithMetadata<M>> NodeWithMetadata<M> for Option<T> {
35 fn self_metadata(&self) -> M {
36 match self {
37 Some(inner) => inner.self_metadata(),
38 None => M::default(),
39 }
40 }
41
42 fn metadata(&self) -> M {
43 match self {
44 Some(inner) => inner.metadata(),
45 None => M::default(),
46 }
47 }
48}