fallow_types/extract_css_analytics.rs
1//! CSS rule metrics, declaration blocks, raw style values, custom property
2//! definitions, and CSS analytics.
3
4/// Structural CSS metrics for one style rule of the parsed CSS. Only a rule
5/// over a structural floor (an id selector, a complex selector, `!important`,
6/// or deep nesting) is recorded, so the vector stays bounded. Not cached:
7/// `fallow health` computes these from the CSS source (no `bitcode` derive).
8#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
9#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
10pub struct CssRuleMetric {
11 /// 1-based line of the rule's first selector.
12 pub line: u32,
13 /// 1-based column of the rule's first selector.
14 pub col: u32,
15 /// Specificity component `a` (id selectors), max across the rule's selectors.
16 pub specificity_a: u16,
17 /// Specificity component `b` (class / attribute / pseudo-class selectors).
18 pub specificity_b: u16,
19 /// Specificity component `c` (type / pseudo-element selectors).
20 pub specificity_c: u16,
21 /// Largest selector component count across the rule's selector list.
22 pub complexity: u16,
23 /// Declaration count in the rule (normal plus `!important`).
24 pub declaration_count: u16,
25 /// `!important` declaration count in the rule.
26 pub important_count: u16,
27 /// Style-rule nesting depth (0 = top level).
28 pub nesting_depth: u8,
29 /// Selector list of a notable rule, for audit keys. Never serialized.
30 #[serde(skip)]
31 pub selector: String,
32}
33
34/// A style rule's declaration-block fingerprint and location, for cross-file
35/// duplicate-block detection. Only rules with a meaningful number of
36/// declarations are recorded (small blocks repeat legitimately). Internal
37/// staging only: this is consumed in-process by the health layer to build the
38/// grouped `duplicate_declaration_blocks` output and is never serialized.
39#[derive(Debug, Clone)]
40pub struct CssDeclarationBlock {
41 /// xxh3 fingerprint over the rule's normalized (sorted, `!important`-tagged)
42 /// declaration set.
43 pub fingerprint: u64,
44 /// 1-based line of the rule's first selector.
45 pub line: u32,
46 /// Declaration count in the rule (normal plus `!important`).
47 pub declaration_count: u16,
48}
49
50/// Located raw styling value authored directly in CSS rather than via a
51/// custom property or design-token helper. Internal staging for the health
52/// layer; public output adds actions and confidence.
53#[derive(Debug, Clone, PartialEq, Eq)]
54pub struct CssRawStyleValue {
55 /// Value axis, e.g. `color`, `font-size`, `line-height`, `radius`, or `shadow`.
56 pub axis: String,
57 /// CSS property where the value appears.
58 pub property: String,
59 /// Rendered declaration value.
60 pub value: String,
61 /// 1-based line of the containing style rule.
62 pub line: u32,
63}
64
65/// Located CSS custom-property definition with its rendered value. Internal
66/// staging for design-token reuse suggestions in the health layer.
67#[derive(Debug, Clone, PartialEq, Eq)]
68pub struct CssCustomPropertyDefinition {
69 /// Custom property name, including the leading `--`.
70 pub name: String,
71 /// Rendered custom property value.
72 pub value: String,
73 /// 1-based line of the containing style rule.
74 pub line: u32,
75}
76
77/// Stylesheet-level structural CSS analytics, computed from the parsed CSS
78/// syntax tree. Feeds `fallow health` penalty weights and located findings,
79/// never a standalone CSS score.
80#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
81#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
82pub struct CssAnalytics {
83 /// Total declarations across every style rule (normal plus `!important`).
84 pub total_declarations: u32,
85 /// Total `!important` declarations across every style rule.
86 pub important_declarations: u32,
87 /// Number of style rules.
88 pub rule_count: u32,
89 /// Number of style rules with no declarations.
90 pub empty_rule_count: u32,
91 /// Deepest style-rule nesting depth observed (0 = no nesting).
92 pub max_nesting_depth: u8,
93 /// Rules that crossed the structural floor, in source order. Bounded; see
94 /// [`Self::notable_truncated`]. The scalar aggregates above always reflect
95 /// the full stylesheet regardless of truncation.
96 pub notable_rules: Vec<CssRuleMetric>,
97 /// `true` when more rules crossed the structural floor than `notable_rules`
98 /// retains (compiled utility CSS can emit thousands of `!important` rules),
99 /// so consumers can note that per-rule findings were capped.
100 pub notable_truncated: bool,
101 /// Distinct color values in the stylesheet, sorted (a palette-size /
102 /// design-token-sprawl signal). The parser canonicalizes notation, so the
103 /// authored format is NOT preserved: `red`, `#f00`, `#ff0000`, and
104 /// `rgb(255,0,0)` all collapse to one entry, and every legacy sRGB notation
105 /// renders as hex. Notation-MIXING (hex vs rgb vs hsl) is therefore not
106 /// detectable from this set; it would need a separate raw-token pass.
107 pub colors: Vec<String>,
108 /// Distinct `font-size` declaration values in the stylesheet, sorted.
109 pub font_sizes: Vec<String>,
110 /// Distinct `z-index` declaration values in the stylesheet, sorted.
111 pub z_indexes: Vec<String>,
112 /// Distinct `box-shadow` declaration values in the stylesheet, sorted. A
113 /// high count signals an uncontrolled shadow scale (design-token sprawl).
114 pub box_shadows: Vec<String>,
115 /// Distinct `border-radius` declaration values in the stylesheet, sorted.
116 pub border_radii: Vec<String>,
117 /// Distinct `line-height` declaration values in the stylesheet, sorted.
118 pub line_heights: Vec<String>,
119 /// Bounded located raw styling values that bypass custom properties or
120 /// token helpers. These are conservative declaration-level candidates for
121 /// audit introduced-vs-base gating.
122 #[serde(skip)]
123 #[cfg_attr(feature = "schema", schemars(skip))]
124 pub raw_style_values: Vec<CssRawStyleValue>,
125 /// Located custom-property definitions with values. Internal staging
126 /// consumed by the health layer for nearest-token suggestions.
127 #[serde(skip)]
128 #[cfg_attr(feature = "schema", schemars(skip))]
129 pub custom_property_definitions: Vec<CssCustomPropertyDefinition>,
130 /// Distinct custom properties (`--x`) defined in the stylesheet, sorted.
131 pub defined_custom_properties: Vec<String>,
132 /// Distinct custom properties referenced via `var()` in the stylesheet.
133 pub referenced_custom_properties: Vec<String>,
134 /// Distinct `@keyframes` names defined in the stylesheet, sorted.
135 pub defined_keyframes: Vec<String>,
136 /// Distinct `@keyframes` names referenced via `animation` / `animation-name`.
137 pub referenced_keyframes: Vec<String>,
138 /// First 1-based rule line per `referenced_keyframes` name, sorted (internal staging).
139 #[serde(skip)]
140 pub keyframe_reference_lines: Vec<(String, u32)>,
141 /// Distinct custom properties registered via an `@property` rule, sorted.
142 pub registered_custom_properties: Vec<String>,
143 /// Distinct cascade layers declared (via `@layer a, b;` statements or named
144 /// `@layer a { }` blocks), sorted.
145 pub declared_layers: Vec<String>,
146 /// Distinct cascade layers populated by a named `@layer a { }` block, sorted.
147 /// A layer declared but never populated (and not imported into) is a
148 /// cleanup candidate.
149 pub populated_layers: Vec<String>,
150 /// Distinct font families declared by an `@font-face` rule in the stylesheet,
151 /// sorted. A declared family referenced by no `font-family` anywhere is a
152 /// dead web-font payload (cleanup candidate).
153 pub defined_font_faces: Vec<String>,
154 /// Distinct font families referenced via `font-family` / `font` in the
155 /// stylesheet, sorted (generic keywords like `serif` excluded).
156 pub referenced_font_families: Vec<String>,
157 /// Declaration-block fingerprints of rules at or above the minimum block size.
158 /// Internal staging for duplicate-block detection, never serialized.
159 #[serde(skip)]
160 pub declaration_blocks: Vec<CssDeclarationBlock>,
161}