Skip to main content

ass_core/analysis/styles/analyzer/
checks.rs

1//! Style extraction, conflict detection, and validation checks.
2//!
3//! Provides helpers to locate the styles section, flag duplicate style names,
4//! and validate individual resolved styles against correctness and performance
5//! criteria.
6
7use super::{AnalysisOptions, StyleAnalyzer};
8use crate::{
9    analysis::styles::{
10        resolved_style::ResolvedStyle,
11        validation::{StyleConflict, StyleValidationIssue},
12    },
13    parser::{Section, Style},
14};
15use alloc::{collections::BTreeMap, vec::Vec};
16
17impl<'a> StyleAnalyzer<'a> {
18    /// Extract styles from script sections
19    #[must_use]
20    pub fn extract_styles(&self) -> Option<&[Style<'a>]> {
21        for section in self.script.sections() {
22            if let Section::Styles(styles) = section {
23                return Some(styles);
24            }
25        }
26        None
27    }
28
29    /// Detect conflicts between styles in a section
30    pub(super) fn detect_style_conflicts_from_section(&mut self, styles: &[Style<'a>]) {
31        let mut name_counts: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
32
33        for style in styles {
34            name_counts.entry(style.name).or_default().push(style.name);
35        }
36
37        for (_name, instances) in name_counts {
38            if instances.len() > 1 {
39                self.conflicts
40                    .push(StyleConflict::duplicate_name(instances));
41            }
42        }
43    }
44
45    /// Validate style properties
46    pub(super) fn validate_style_properties(
47        &self,
48        style: &ResolvedStyle<'a>,
49    ) -> Vec<StyleValidationIssue> {
50        let mut issues = Vec::new();
51
52        if style.font_size() <= 0.0 {
53            issues.push(StyleValidationIssue::error(
54                "font_size",
55                "Font size must be positive",
56            ));
57        }
58
59        if self
60            .config
61            .options
62            .contains(AnalysisOptions::STRICT_VALIDATION)
63            && style.font_size() > 200.0
64        {
65            issues.push(StyleValidationIssue::warning(
66                "font_size",
67                "Very large font size may cause performance issues",
68            ));
69        }
70
71        issues
72    }
73
74    /// Analyze style performance impact
75    pub(super) fn analyze_style_performance(
76        &self,
77        style: &ResolvedStyle<'a>,
78    ) -> Vec<StyleValidationIssue> {
79        let mut issues = Vec::new();
80        let thresholds = &self.config.performance_thresholds;
81
82        if style.font_size() > thresholds.large_font_threshold {
83            issues.push(StyleValidationIssue::info_with_suggestion(
84                "font_size",
85                "Large font size detected",
86                "Consider reducing font size for better performance",
87            ));
88        }
89
90        if style.has_performance_issues() {
91            issues.push(StyleValidationIssue::warning(
92                "complexity",
93                "Style has high rendering complexity",
94            ));
95        }
96
97        issues
98    }
99}