use thiserror::Error;
#[derive(Debug, Clone)]
pub struct OptimizationConfig {
pub merge_rules: bool,
pub optimize_properties: bool,
pub optimize_selectors: bool,
pub minify: bool,
pub level: OptimizationLevel,
}
impl Default for OptimizationConfig {
fn default() -> Self {
Self {
merge_rules: true,
optimize_properties: true,
optimize_selectors: true,
minify: true,
level: OptimizationLevel::Standard,
}
}
}
#[derive(Debug, Clone)]
pub enum OptimizationLevel {
Minimal,
Standard,
Aggressive,
}
#[derive(Debug, Clone)]
pub struct OptimizationResult {
pub optimized_css: String,
pub metrics: OptimizationMetrics,
pub analysis: AnalysisReport,
}
#[derive(Debug, Clone)]
pub struct OptimizationMetrics {
pub original_size: usize,
pub optimized_size: usize,
pub size_reduction_percentage: f64,
pub rules_merged: usize,
pub properties_optimized: usize,
pub duplicates_removed: usize,
pub selectors_optimized: usize,
pub processing_time: std::time::Duration,
}
impl OptimizationMetrics {
pub fn new() -> Self {
Self {
original_size: 0,
optimized_size: 0,
size_reduction_percentage: 0.0,
rules_merged: 0,
properties_optimized: 0,
duplicates_removed: 0,
selectors_optimized: 0,
processing_time: std::time::Duration::from_secs(0),
}
}
}
#[derive(Debug, Clone)]
pub struct AnalysisReport {
pub rules: RuleAnalysis,
pub properties: PropertyAnalysis,
pub selectors: SelectorAnalysis,
pub analysis_time: std::time::Duration,
}
#[derive(Debug, Clone)]
pub struct RuleAnalysis {
pub total_rules: usize,
pub empty_rules: Vec<String>,
pub duplicate_rules: Vec<DuplicateRule>,
pub mergeable_rules: Vec<MergeableRule>,
}
#[derive(Debug, Clone)]
pub struct PropertyAnalysis {
pub total_properties: usize,
pub duplicate_properties: Vec<DuplicateProperty>,
pub redundant_properties: Vec<RedundantProperty>,
pub optimizable_properties: Vec<OptimizableProperty>,
}
#[derive(Debug, Clone)]
pub struct SelectorAnalysis {
pub total_selectors: usize,
pub redundant_selectors: Vec<String>,
pub optimizable_selectors: Vec<OptimizableSelector>,
pub complex_selectors: Vec<ComplexSelector>,
}
#[derive(Debug, Clone)]
pub struct CSSRule {
pub selector: String,
pub properties: Vec<CSSProperty>,
}
#[derive(Debug, Clone)]
pub struct CSSProperty {
pub name: String,
pub value: String,
}
#[derive(Debug, Clone)]
pub struct DuplicateRule {
pub selector: String,
pub count: usize,
}
#[derive(Debug, Clone)]
pub struct MergeableRule {
pub selectors: Vec<String>,
pub properties: Vec<CSSProperty>,
}
#[derive(Debug, Clone)]
pub struct DuplicateProperty {
pub name: String,
pub values: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct RedundantProperty {
pub name: String,
pub reason: String,
}
#[derive(Debug, Clone)]
pub struct OptimizableProperty {
pub name: String,
pub current_value: String,
pub optimized_value: String,
}
#[derive(Debug, Clone)]
pub struct OptimizableSelector {
pub current: String,
pub optimized: String,
}
#[derive(Debug, Clone)]
pub struct ComplexSelector {
pub selector: String,
pub complexity_score: usize,
}
#[derive(Debug, Clone)]
pub struct PropertyRule {
pub name: String,
pub shorthand: bool,
pub longhand: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct SelectorRule {
pub name: String,
pub pattern: String,
pub optimization: SelectorOptimization,
}
#[derive(Debug, Clone)]
pub enum SelectorOptimization {
Remove,
Simplify,
Merge,
}
#[derive(Debug, Clone)]
pub struct RuleMergeResult {
pub optimized_css: String,
pub rules_merged: usize,
pub merge_time: std::time::Duration,
}
#[derive(Debug, Clone)]
pub struct PropertyOptimizationResult {
pub optimized_css: String,
pub properties_optimized: usize,
pub duplicates_removed: usize,
pub optimization_time: std::time::Duration,
}
#[derive(Debug, Clone)]
pub struct SelectorOptimizationResult {
pub optimized_css: String,
pub selectors_optimized: usize,
pub optimization_time: std::time::Duration,
}
#[derive(Debug, Clone)]
pub struct OptimizationStatistics {
pub total_optimizations: usize,
pub average_size_reduction: f64,
pub processing_time: std::time::Duration,
}
#[derive(Debug, Error)]
pub enum OptimizationError {
#[error("CSS parsing failed: {error}")]
ParsingFailed { error: String },
#[error("Optimization failed: {error}")]
OptimizationFailed { error: String },
#[error("Invalid CSS syntax: {error}")]
InvalidCSS { error: String },
#[error("Memory limit exceeded")]
MemoryLimitExceeded,
#[error("Processing timeout")]
ProcessingTimeout,
}