tailwind-rs-core 0.15.4

Core types and utilities for tailwind-rs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! Performance optimization system for tailwind-rs
//!
//! This module provides caching and performance optimization functionality
//! for class generation and CSS optimization.

use crate::error::Result;
use lru::LruCache;
use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// Performance metrics for tracking optimization effectiveness
#[derive(Debug, Clone)]
pub struct PerformanceMetric {
    pub operation: String,
    pub duration: Duration,
    pub timestamp: Instant,
    pub success: bool,
}

/// Error metrics for tracking validation and processing errors
#[derive(Debug, Clone)]
pub struct ErrorMetric {
    pub error_type: String,
    pub error_message: String,
    pub timestamp: Instant,
    pub count: u64,
}

/// Usage metrics for tracking class generation patterns
#[derive(Debug, Clone)]
pub struct UsageMetric {
    pub class_pattern: String,
    pub usage_count: u64,
    pub last_used: Instant,
    pub average_generation_time: Duration,
}

/// Caches generated classes for performance
#[derive(Debug)]
pub struct ClassCache {
    cache: Arc<RwLock<LruCache<String, String>>>,
    hit_rate: AtomicU64,
    miss_rate: AtomicU64,
    total_requests: AtomicU64,
}

impl ClassCache {
    /// Create a new cache with specified capacity
    pub fn new(capacity: usize) -> Self {
        Self {
            cache: Arc::new(RwLock::new(LruCache::new(
                std::num::NonZeroUsize::new(capacity)
                    .unwrap_or(std::num::NonZeroUsize::new(100).unwrap()),
            ))),
            hit_rate: AtomicU64::new(0),
            miss_rate: AtomicU64::new(0),
            total_requests: AtomicU64::new(0),
        }
    }

    /// Retrieve a cached class string
    pub fn get(&self, key: &str) -> Option<String> {
        self.total_requests.fetch_add(1, Ordering::Relaxed);

        let mut cache = self.cache.write();
        if let Some(value) = cache.get(key) {
            self.hit_rate.fetch_add(1, Ordering::Relaxed);
            Some(value.clone())
        } else {
            self.miss_rate.fetch_add(1, Ordering::Relaxed);
            None
        }
    }

    /// Store a class string in the cache
    pub fn put(&self, key: String, value: String) {
        let mut cache = self.cache.write();
        cache.put(key, value);
    }

    /// Get cache hit rate (0.0 to 1.0)
    pub fn hit_rate(&self) -> f64 {
        let hits = self.hit_rate.load(Ordering::Relaxed) as f64;
        let total = self.total_requests.load(Ordering::Relaxed) as f64;

        if total == 0.0 {
            0.0
        } else {
            hits / total
        }
    }

    /// Get cache miss rate (0.0 to 1.0)
    pub fn miss_rate(&self) -> f64 {
        let misses = self.miss_rate.load(Ordering::Relaxed) as f64;
        let total = self.total_requests.load(Ordering::Relaxed) as f64;

        if total == 0.0 {
            0.0
        } else {
            misses / total
        }
    }

    /// Get total number of requests
    pub fn total_requests(&self) -> u64 {
        self.total_requests.load(Ordering::Relaxed)
    }

    /// Clear the cache
    pub fn clear(&self) {
        let mut cache = self.cache.write();
        cache.clear();
    }

    /// Get cache size
    pub fn len(&self) -> usize {
        let cache = self.cache.read();
        cache.len()
    }

    /// Check if cache is empty
    pub fn is_empty(&self) -> bool {
        let cache = self.cache.read();
        cache.is_empty()
    }
}

/// Performance optimization levels
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OptimizationLevel {
    None,
    Low,
    Medium,
    High,
    Maximum,
}

impl OptimizationLevel {
    /// Get the cache capacity for this optimization level
    pub fn cache_capacity(&self) -> usize {
        match self {
            OptimizationLevel::None => 0,
            OptimizationLevel::Low => 100,
            OptimizationLevel::Medium => 500,
            OptimizationLevel::High => 1000,
            OptimizationLevel::Maximum => 5000,
        }
    }

    /// Get the optimization factor (1.0 = no optimization, higher = more optimization)
    pub fn optimization_factor(&self) -> f64 {
        match self {
            OptimizationLevel::None => 1.0,
            OptimizationLevel::Low => 1.2,
            OptimizationLevel::Medium => 1.5,
            OptimizationLevel::High => 2.0,
            OptimizationLevel::Maximum => 3.0,
        }
    }
}

/// Optimizes class generation performance
#[derive(Debug)]
pub struct PerformanceOptimizer {
    class_cache: ClassCache,
    css_cache: ClassCache,
    optimization_level: OptimizationLevel,
    performance_metrics: Arc<RwLock<Vec<PerformanceMetric>>>,
    error_metrics: Arc<RwLock<Vec<ErrorMetric>>>,
    usage_metrics: Arc<RwLock<HashMap<String, UsageMetric>>>,
}

impl PerformanceOptimizer {
    /// Create a new optimizer instance
    pub fn new() -> Self {
        Self::with_optimization_level(OptimizationLevel::Medium)
    }

    /// Create a new optimizer with specific optimization level
    pub fn with_optimization_level(level: OptimizationLevel) -> Self {
        let capacity = level.cache_capacity();
        Self {
            class_cache: ClassCache::new(capacity),
            css_cache: ClassCache::new(capacity),
            optimization_level: level,
            performance_metrics: Arc::new(RwLock::new(Vec::new())),
            error_metrics: Arc::new(RwLock::new(Vec::new())),
            usage_metrics: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Optimize class generation with caching
    pub fn optimize_class_generation(&mut self, classes: &[String]) -> Result<String> {
        let start = Instant::now();
        let cache_key = self.generate_cache_key(classes);

        // Try to get from cache first
        if let Some(cached_result) = self.class_cache.get(&cache_key) {
            self.record_performance("class_generation_cached", start.elapsed(), true);
            return Ok(cached_result);
        }

        // Generate classes
        let result = self.generate_classes(classes)?;

        // Cache the result
        self.class_cache.put(cache_key, result.clone());

        self.record_performance("class_generation", start.elapsed(), true);
        Ok(result)
    }

    /// Optimize CSS generation with caching
    pub fn optimize_css_generation(&mut self, css: &str) -> Result<String> {
        let start = Instant::now();
        let cache_key = format!("css:{}", css);

        // Try to get from cache first
        if let Some(cached_result) = self.css_cache.get(&cache_key) {
            self.record_performance("css_generation_cached", start.elapsed(), true);
            return Ok(cached_result);
        }

        // Generate optimized CSS
        let result = self.optimize_css(css)?;

        // Cache the result
        self.css_cache.put(cache_key, result.clone());

        self.record_performance("css_generation", start.elapsed(), true);
        Ok(result)
    }

    /// Generate cache key for classes
    fn generate_cache_key(&self, classes: &[String]) -> String {
        let mut key = String::new();
        for class in classes {
            key.push_str(class);
            key.push('|');
        }
        key
    }

    /// Generate classes with optimization
    fn generate_classes(&self, classes: &[String]) -> Result<String> {
        // Apply optimization based on level
        let optimized_classes = match self.optimization_level {
            OptimizationLevel::None => classes.to_vec(),
            OptimizationLevel::Low => self.optimize_classes_low(classes),
            OptimizationLevel::Medium => self.optimize_classes_medium(classes),
            OptimizationLevel::High => self.optimize_classes_high(classes),
            OptimizationLevel::Maximum => self.optimize_classes_maximum(classes),
        };

        Ok(optimized_classes.join(" "))
    }

    /// Low-level optimization
    fn optimize_classes_low(&self, classes: &[String]) -> Vec<String> {
        // Remove duplicates
        let mut unique_classes: Vec<String> = classes.to_vec();
        unique_classes.sort();
        unique_classes.dedup();
        unique_classes
    }

    /// Medium-level optimization
    fn optimize_classes_medium(&self, classes: &[String]) -> Vec<String> {
        let mut optimized = self.optimize_classes_low(classes);

        // Remove conflicting classes
        optimized = self.remove_conflicting_classes(optimized);

        optimized
    }

    /// High-level optimization
    fn optimize_classes_high(&self, classes: &[String]) -> Vec<String> {
        let mut optimized = self.optimize_classes_medium(classes);

        // Merge similar classes
        optimized = self.merge_similar_classes(optimized);

        optimized
    }

    /// Maximum-level optimization
    fn optimize_classes_maximum(&self, classes: &[String]) -> Vec<String> {
        let mut optimized = self.optimize_classes_high(classes);

        // Apply advanced optimizations
        optimized = self.apply_advanced_optimizations(optimized);

        optimized
    }

    /// Remove conflicting classes
    fn remove_conflicting_classes(&self, classes: Vec<String>) -> Vec<String> {
        let mut result = Vec::new();
        let mut seen_groups: HashMap<String, String> = HashMap::new();

        for class in classes {
            let group = self.get_class_group(&class);
            if let Some(existing) = seen_groups.get(&group) {
                // Keep the more specific class
                if self.is_more_specific(&class, existing) {
                    if let Some(pos) = result.iter().position(|c| c == existing) {
                        result.remove(pos);
                    }
                    result.push(class.clone());
                    seen_groups.insert(group, class);
                }
            } else {
                result.push(class.clone());
                seen_groups.insert(group, class);
            }
        }

        result
    }

    /// Merge similar classes
    fn merge_similar_classes(&self, classes: Vec<String>) -> Vec<String> {
        // This is a simplified implementation
        // In a real implementation, you would merge classes like:
        // "px-2", "px-4" -> "px-4" (keep the larger value)
        classes
    }

    /// Apply advanced optimizations
    fn apply_advanced_optimizations(&self, classes: Vec<String>) -> Vec<String> {
        // This is a placeholder for advanced optimizations
        // In a real implementation, you might:
        // - Combine responsive classes
        // - Optimize color classes
        // - Merge spacing classes
        classes
    }

    /// Get class group for conflict detection
    fn get_class_group(&self, class: &str) -> String {
        if class.starts_with("bg-") {
            "background".to_string()
        } else if class.starts_with("text-") {
            "text".to_string()
        } else if class.starts_with("border-") {
            "border".to_string()
        } else if class.starts_with("p-") || class.starts_with("px-") || class.starts_with("py-") {
            "padding".to_string()
        } else if class.starts_with("m-") || class.starts_with("mx-") || class.starts_with("my-") {
            "margin".to_string()
        } else {
            "other".to_string()
        }
    }

    /// Check if one class is more specific than another
    fn is_more_specific(&self, class1: &str, class2: &str) -> bool {
        // Simple heuristic: longer class names are more specific
        class1.len() > class2.len()
    }

    /// Optimize CSS
    fn optimize_css(&self, css: &str) -> Result<String> {
        let mut optimized = css.to_string();

        // Remove unnecessary whitespace
        optimized = optimized.replace("  ", " ");
        optimized = optimized.replace("\n", "");
        optimized = optimized.replace("\t", "");

        // Remove trailing semicolons
        optimized = optimized.replace(";}", "}");

        Ok(optimized)
    }

    /// Record performance metrics
    pub fn record_performance(&self, operation: &str, duration: Duration, success: bool) {
        let metric = PerformanceMetric {
            operation: operation.to_string(),
            duration,
            timestamp: Instant::now(),
            success,
        };

        let mut metrics = self.performance_metrics.write();
        metrics.push(metric);

        // Keep only the last 1000 metrics
        let len = metrics.len();
        if len > 1000 {
            metrics.drain(0..len - 1000);
        }
    }

    /// Record error metrics
    pub fn record_error(&self, error_type: &str, error: &dyn std::error::Error) {
        let metric = ErrorMetric {
            error_type: error_type.to_string(),
            error_message: error.to_string(),
            timestamp: Instant::now(),
            count: 1,
        };

        let mut metrics = self.error_metrics.write();
        metrics.push(metric);

        // Keep only the last 1000 error metrics
        let len = metrics.len();
        if len > 1000 {
            metrics.drain(0..len - 1000);
        }
    }

    /// Record usage metrics
    pub fn record_usage(&self, class_pattern: &str, generation_time: Duration) {
        let mut metrics = self.usage_metrics.write();

        if let Some(usage) = metrics.get_mut(class_pattern) {
            usage.usage_count += 1;
            usage.last_used = Instant::now();
            usage.average_generation_time = Duration::from_nanos(
                ((usage.average_generation_time.as_nanos() + generation_time.as_nanos()) / 2)
                    as u64,
            );
        } else {
            metrics.insert(
                class_pattern.to_string(),
                UsageMetric {
                    class_pattern: class_pattern.to_string(),
                    usage_count: 1,
                    last_used: Instant::now(),
                    average_generation_time: generation_time,
                },
            );
        }
    }

    /// Get performance metrics
    pub fn get_performance_metrics(&self) -> Vec<PerformanceMetric> {
        let metrics = self.performance_metrics.read();
        metrics.clone()
    }

    /// Get error metrics
    pub fn get_error_metrics(&self) -> Vec<ErrorMetric> {
        let metrics = self.error_metrics.read();
        metrics.clone()
    }

    /// Get usage metrics
    pub fn get_usage_metrics(&self) -> HashMap<String, UsageMetric> {
        let metrics = self.usage_metrics.read();
        metrics.clone()
    }

    /// Get cache statistics
    pub fn get_cache_stats(&self) -> CacheStats {
        CacheStats {
            class_cache_hit_rate: self.class_cache.hit_rate(),
            class_cache_miss_rate: self.class_cache.miss_rate(),
            class_cache_total_requests: self.class_cache.total_requests(),
            class_cache_size: self.class_cache.len(),
            css_cache_hit_rate: self.css_cache.hit_rate(),
            css_cache_miss_rate: self.css_cache.miss_rate(),
            css_cache_total_requests: self.css_cache.total_requests(),
            css_cache_size: self.css_cache.len(),
        }
    }

    /// Set optimization level
    pub fn set_optimization_level(&mut self, level: OptimizationLevel) {
        self.optimization_level = level;
    }

    /// Get optimization level
    pub fn optimization_level(&self) -> OptimizationLevel {
        self.optimization_level
    }
}

impl Default for PerformanceOptimizer {
    fn default() -> Self {
        Self::new()
    }
}

/// Cache statistics
#[derive(Debug, Clone)]
pub struct CacheStats {
    pub class_cache_hit_rate: f64,
    pub class_cache_miss_rate: f64,
    pub class_cache_total_requests: u64,
    pub class_cache_size: usize,
    pub css_cache_hit_rate: f64,
    pub css_cache_miss_rate: f64,
    pub css_cache_total_requests: u64,
    pub css_cache_size: usize,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_optimization_level() {
        assert_eq!(OptimizationLevel::None.cache_capacity(), 0);
        assert_eq!(OptimizationLevel::Low.cache_capacity(), 100);
        assert_eq!(OptimizationLevel::Medium.cache_capacity(), 500);
        assert_eq!(OptimizationLevel::High.cache_capacity(), 1000);
        assert_eq!(OptimizationLevel::Maximum.cache_capacity(), 5000);

        assert_eq!(OptimizationLevel::None.optimization_factor(), 1.0);
        assert_eq!(OptimizationLevel::Low.optimization_factor(), 1.2);
        assert_eq!(OptimizationLevel::Medium.optimization_factor(), 1.5);
        assert_eq!(OptimizationLevel::High.optimization_factor(), 2.0);
        assert_eq!(OptimizationLevel::Maximum.optimization_factor(), 3.0);
    }

    #[test]
    fn test_performance_optimizer_creation() {
        let optimizer = PerformanceOptimizer::new();
        assert_eq!(optimizer.optimization_level(), OptimizationLevel::Medium);
    }

    #[test]
    fn test_performance_optimizer_with_level() {
        let optimizer = PerformanceOptimizer::with_optimization_level(OptimizationLevel::High);
        assert_eq!(optimizer.optimization_level(), OptimizationLevel::High);
    }
}