tailwind-rs-postcss 0.15.4

PostCSS integration for Tailwind-RS Core
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
//! PostCSS Engine implementation
//!
//! This module provides the core PostCSS processing engine with plugin support,
//! AST manipulation, and source map generation.

use crate::ast::{CSSNode, CSSRule};
use crate::error::{PostCSSError, Result};
use crate::js_bridge::JSBridge;
use crate::parser::{CSSParser, ParseOptions};
use crate::plugin_loader::{PluginConfig, PluginLoader, PluginResult};
use crate::source_map::{SourceMap, SourceMapGenerator};
use crate::transformer::{CSSTransformer, TransformOptions};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

/// PostCSS processing engine
#[derive(Debug)]
pub struct PostCSSEngine {
    config: PostCSSConfig,
    parser: CSSParser,
    transformer: CSSTransformer,
    js_bridge: Option<JSBridge>,
    plugin_loader: PluginLoader,
    source_map_generator: SourceMapGenerator,
    cache: Arc<RwLock<HashMap<String, ProcessedCSS>>>,
}

/// PostCSS configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PostCSSConfig {
    /// Plugins to use for processing
    pub plugins: Vec<PluginConfig>,
    /// Source map generation
    pub source_map: bool,
    /// Source map options
    pub source_map_options: SourceMapOptions,
    /// Parser options
    pub parser_options: ParseOptions,
    /// Transformer options
    pub transform_options: TransformOptions,
    /// Performance options
    pub performance: PerformanceOptions,
}

/// Source map configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceMapOptions {
    /// Generate inline source maps
    pub inline: bool,
    /// Source map file path
    pub file: Option<String>,
    /// Source root
    pub source_root: Option<String>,
    /// Include sources content
    pub sources_content: bool,
}

/// Performance configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceOptions {
    /// Enable caching
    pub enable_cache: bool,
    /// Cache size limit
    pub cache_size_limit: usize,
    /// Parallel processing
    pub parallel_processing: bool,
    /// Memory optimization
    pub memory_optimization: bool,
}

/// Processed CSS result
#[derive(Debug, Clone)]
pub struct ProcessedCSS {
    /// Generated CSS
    pub css: String,
    /// Source map (if enabled)
    pub source_map: Option<SourceMap>,
    /// Processing warnings
    pub warnings: Vec<ProcessingWarning>,
    /// Processing metrics
    pub metrics: ProcessingMetrics,
}

/// Processing warning
#[derive(Debug, Clone)]
pub struct ProcessingWarning {
    pub message: String,
    pub line: Option<usize>,
    pub column: Option<usize>,
    pub severity: WarningSeverity,
}

/// Warning severity levels
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WarningSeverity {
    Info,
    Warning,
    Error,
}

/// Processing metrics
#[derive(Debug, Clone)]
pub struct ProcessingMetrics {
    pub parse_time: std::time::Duration,
    pub transform_time: std::time::Duration,
    pub generate_time: std::time::Duration,
    pub total_time: std::time::Duration,
    pub memory_usage: usize,
    pub rules_processed: usize,
    pub plugins_executed: usize,
}

impl Default for PostCSSConfig {
    fn default() -> Self {
        Self {
            plugins: Vec::new(),
            source_map: true,
            source_map_options: SourceMapOptions::default(),
            parser_options: ParseOptions::default(),
            transform_options: TransformOptions::default(),
            performance: PerformanceOptions::default(),
        }
    }
}

impl Default for SourceMapOptions {
    fn default() -> Self {
        Self {
            inline: false,
            file: None,
            source_root: None,
            sources_content: true,
        }
    }
}

impl Default for PerformanceOptions {
    fn default() -> Self {
        Self {
            enable_cache: true,
            cache_size_limit: 1000,
            parallel_processing: true,
            memory_optimization: true,
        }
    }
}

impl PostCSSEngine {
    /// Create a new PostCSS engine with configuration
    pub fn new(config: PostCSSConfig) -> Result<Self> {
        let parser = CSSParser::new(config.parser_options.clone());
        let transformer = CSSTransformer::new(config.transform_options.clone());
        let plugin_loader = PluginLoader::new();
        let source_map_generator = SourceMapGenerator::new();

        // Initialize JavaScript bridge if needed
        let js_bridge = if config.plugins.iter().any(|p| p.requires_js()) {
            Some(JSBridge::new()?)
        } else {
            None
        };

        Ok(Self {
            config,
            parser,
            transformer,
            js_bridge,
            plugin_loader,
            source_map_generator,
            cache: Arc::new(RwLock::new(HashMap::new())),
        })
    }

    /// Process CSS input through PostCSS pipeline
    pub async fn process_css(&self, input: &str) -> Result<ProcessedCSS> {
        let start_time = std::time::Instant::now();

        // Check cache first
        if self.config.performance.enable_cache {
            if let Some(cached) = self.get_cached_result(input).await {
                return Ok(cached);
            }
        }

        // Parse CSS into AST
        let parse_start = std::time::Instant::now();
        let ast = self.parser.parse(input)?;
        let parse_time = parse_start.elapsed();

        // Apply transformations
        let transform_start = std::time::Instant::now();
        let transformed_ast = self.apply_transformations(ast).await?;
        let transform_time = transform_start.elapsed();

        // Generate output CSS
        let generate_start = std::time::Instant::now();
        let css = self.generate_css(&transformed_ast)?;
        let generate_time = generate_start.elapsed();

        // Generate source map if enabled
        let source_map = if self.config.source_map {
            let source_map_options = crate::source_map::SourceMapOptions {
                inline: self.config.source_map_options.inline,
                file: self.config.source_map_options.file.clone(),
                source_root: self.config.source_map_options.source_root.clone(),
                sources_content: self.config.source_map_options.sources_content,
            };
            Some(
                self.source_map_generator
                    .generate(input, &css, &source_map_options)?,
            )
        } else {
            None
        };

        let total_time = start_time.elapsed();
        let metrics = ProcessingMetrics {
            parse_time,
            transform_time,
            generate_time,
            total_time,
            memory_usage: self.get_memory_usage(),
            rules_processed: self.count_rules(&transformed_ast),
            plugins_executed: self.config.plugins.len(),
        };

        let result = ProcessedCSS {
            css,
            source_map,
            warnings: Vec::new(), // TODO: Collect warnings during processing
            metrics,
        };

        // Cache result if enabled
        if self.config.performance.enable_cache {
            self.cache_result(input, &result).await;
        }

        Ok(result)
    }

    /// Apply all configured transformations to the AST
    async fn apply_transformations(&self, mut ast: CSSNode) -> Result<CSSNode> {
        for plugin_config in &self.config.plugins {
            let plugin_result = self.plugin_loader.load_plugin(plugin_config).await?;

            match plugin_result {
                PluginResult::Native(plugin) => {
                    ast = plugin.transform(ast)?;
                }
                PluginResult::JavaScript(js_plugin) => {
                    if let Some(js_bridge) = &self.js_bridge {
                        ast = js_bridge.execute_plugin(&js_plugin.name, ast).await?;
                    } else {
                        return Err(PostCSSError::JavaScriptBridgeNotAvailable);
                    }
                }
            }
        }

        // Apply built-in transformations
        ast = self.transformer.transform(ast)?;

        Ok(ast)
    }

    /// Generate CSS from transformed AST
    fn generate_css(&self, ast: &CSSNode) -> Result<String> {
        match ast {
            CSSNode::Stylesheet(rules) => {
                let mut css = String::new();
                for rule in rules {
                    css.push_str(&self.rule_to_css(rule)?);
                    css.push('\n');
                }
                Ok(css)
            }
            _ => Err(PostCSSError::InvalidAST("Expected stylesheet".to_string())),
        }
    }

    /// Convert a CSS rule to CSS string
    fn rule_to_css(&self, rule: &CSSRule) -> Result<String> {
        let mut css = String::new();

        // Add selector
        css.push_str(&rule.selector);
        css.push_str(" {\n");

        // Add declarations
        for declaration in &rule.declarations {
            css.push_str("  ");
            css.push_str(&declaration.property);
            css.push_str(": ");
            css.push_str(&declaration.value);
            if declaration.important {
                css.push_str(" !important");
            }
            css.push_str(";\n");
        }

        css.push('}');
        Ok(css)
    }

    /// Get cached result if available
    async fn get_cached_result(&self, input: &str) -> Option<ProcessedCSS> {
        let cache = self.cache.read().await;
        cache.get(input).cloned()
    }

    /// Cache processing result
    async fn cache_result(&self, input: &str, result: &ProcessedCSS) {
        let mut cache = self.cache.write().await;

        // Check cache size limit
        if cache.len() >= self.config.performance.cache_size_limit {
            // Remove oldest entries (simple LRU)
            let keys_to_remove: Vec<String> = cache.keys().take(cache.len() / 2).cloned().collect();
            for key in keys_to_remove {
                cache.remove(&key);
            }
        }

        cache.insert(input.to_string(), result.clone());
    }

    /// Get current memory usage
    fn get_memory_usage(&self) -> usize {
        // Simple memory usage estimation
        // In a real implementation, this would use proper memory profiling
        std::mem::size_of::<Self>() + self.cache.try_read().map(|c| c.len() * 1024).unwrap_or(0)
    }

    /// Count rules in AST
    fn count_rules(&self, ast: &CSSNode) -> usize {
        match ast {
            CSSNode::Stylesheet(rules) => rules.len(),
            _ => 0,
        }
    }

    /// Get engine metrics
    pub async fn get_metrics(&self) -> EngineMetrics {
        let cache = self.cache.read().await;
        EngineMetrics {
            cache_size: cache.len(),
            memory_usage: self.get_memory_usage(),
            plugins_loaded: self.config.plugins.len(),
            js_bridge_available: self.js_bridge.is_some(),
        }
    }

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

/// Engine metrics
#[derive(Debug, Clone)]
pub struct EngineMetrics {
    pub cache_size: usize,
    pub memory_usage: usize,
    pub plugins_loaded: usize,
    pub js_bridge_available: bool,
}

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

    #[tokio::test]
    async fn test_engine_creation() {
        let config = PostCSSConfig::default();
        let engine = PostCSSEngine::new(config);
        assert!(engine.is_ok());
    }

    #[tokio::test]
    async fn test_css_processing() {
        let engine = PostCSSEngine::new(PostCSSConfig::default()).unwrap();
        let input = ".test { color: red; }";
        let result = engine.process_css(input).await;
        assert!(result.is_ok());

        let css = result.unwrap();
        assert!(css.css.contains(".test"));
        assert!(css.css.contains("color: red"));
    }

    #[tokio::test]
    async fn test_caching() {
        let mut config = PostCSSConfig::default();
        config.performance.enable_cache = true;

        let engine = PostCSSEngine::new(config).unwrap();
        let input = ".test { color: red; }";

        // First processing
        let result1 = engine.process_css(input).await.unwrap();

        // Second processing (should use cache)
        let result2 = engine.process_css(input).await.unwrap();

        assert_eq!(result1.css, result2.css);
    }
}