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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
//! @tailwind Directive Processing System
//!
//! This module provides comprehensive processing of Tailwind CSS directives
//! including @tailwind base, @tailwind components, @tailwind utilities,
//! @apply directives, and @layer directives.

use regex::Regex;
use std::collections::HashMap;
use thiserror::Error;

/// Main processor for Tailwind CSS directives
pub struct TailwindProcessor {
    base_styles: String,
    component_styles: String,
    utility_styles: String,
    config: TailwindConfig,
    cache: ProcessorCache,
    directive_parser: DirectiveParser,
    css_injector: CSSInjector,
}

/// Configuration for Tailwind directive processing
#[derive(Debug, Clone)]
pub struct TailwindConfig {
    pub base_styles: bool,
    pub component_styles: bool,
    pub utility_styles: bool,
    pub apply_processing: bool,
    pub layer_processing: bool,
    pub custom_directives: Vec<CustomDirective>,
}

/// Custom directive configuration
#[derive(Debug, Clone)]
pub struct CustomDirective {
    pub name: String,
    pub pattern: String,
    pub handler: String,
}

/// Cache for processed directives
#[derive(Debug, Clone)]
pub struct ProcessorCache {
    directive_cache: HashMap<String, String>,
    apply_cache: HashMap<String, String>,
    layer_cache: HashMap<String, String>,
}

/// Directive parser for @tailwind, @apply, and @layer directives
pub struct DirectiveParser {
    patterns: Vec<DirectivePattern>,
    config: ParserConfig,
}

/// Directive pattern matching
#[derive(Debug, Clone)]
pub struct DirectivePattern {
    pub name: String,
    pub regex: Regex,
    pub capture_groups: usize,
}

/// Parser configuration
#[derive(Debug, Clone)]
pub struct ParserConfig {
    pub case_sensitive: bool,
    pub multiline: bool,
    pub dotall: bool,
}

/// CSS injector for base, components, and utilities
pub struct CSSInjector {
    base_css: String,
    components_css: String,
    utilities_css: String,
    config: InjectionConfig,
}

/// Injection configuration
#[derive(Debug, Clone)]
pub struct InjectionConfig {
    pub preserve_order: bool,
    pub minify: bool,
    pub source_map: bool,
}

/// Tailwind directive types
#[derive(Debug, Clone, PartialEq)]
pub enum TailwindDirective {
    Base,
    Components,
    Utilities,
}

/// @apply directive structure
#[derive(Debug, Clone)]
pub struct ApplyDirective {
    pub classes: Vec<String>,
    pub selector: String,
    pub line_number: usize,
}

/// @layer directive structure
#[derive(Debug, Clone, PartialEq)]
pub enum LayerDirective {
    Base,
    Components,
    Utilities,
    Custom(String),
}

/// Directive match result
#[derive(Debug, Clone)]
pub struct DirectiveMatch {
    pub directive_type: String,
    pub content: String,
    pub line_number: usize,
    pub start_pos: usize,
    pub end_pos: usize,
}

/// Processing result with statistics
#[derive(Debug, Clone)]
pub struct ProcessingResult {
    pub processed_css: String,
    pub directives_processed: Vec<DirectiveMatch>,
    pub statistics: ProcessingStatistics,
}

/// Processing statistics
#[derive(Debug, Clone)]
pub struct ProcessingStatistics {
    pub total_directives: usize,
    pub base_directives: usize,
    pub component_directives: usize,
    pub utility_directives: usize,
    pub apply_directives: usize,
    pub layer_directives: usize,
    pub processing_time_ms: u64,
}

/// Error types for Tailwind processing
#[derive(Debug, Error)]
pub enum TailwindProcessorError {
    #[error("Invalid @tailwind directive: {directive}")]
    InvalidDirective { directive: String },

    #[error("Unknown class in @apply: {class}")]
    UnknownClass { class: String },

    #[error("Circular @apply dependency: {class}")]
    CircularDependency { class: String },

    #[error("Invalid @layer directive: {layer}")]
    InvalidLayer { layer: String },

    #[error("CSS parsing error: {error}")]
    CSSParsingError { error: String },

    #[error("Configuration error: {error}")]
    ConfigurationError { error: String },
}

impl TailwindProcessor {
    /// Create a new TailwindProcessor with default configuration
    pub fn new() -> Self {
        Self::with_config(TailwindConfig::default())
    }

    /// Create a new TailwindProcessor with custom configuration
    pub fn with_config(config: TailwindConfig) -> Self {
        let directive_parser = DirectiveParser::new();
        let css_injector = CSSInjector::new();
        let cache = ProcessorCache::new();

        Self {
            base_styles: String::new(),
            component_styles: String::new(),
            utility_styles: String::new(),
            config,
            cache,
            directive_parser,
            css_injector,
        }
    }

    /// Create a new TailwindProcessor with cache
    pub fn with_cache(config: TailwindConfig, cache: ProcessorCache) -> Self {
        let directive_parser = DirectiveParser::new();
        let css_injector = CSSInjector::new();

        Self {
            base_styles: String::new(),
            component_styles: String::new(),
            utility_styles: String::new(),
            config,
            cache,
            directive_parser,
            css_injector,
        }
    }

    /// Process @tailwind directives in CSS
    pub fn process_directives(
        &mut self,
        css: &str,
    ) -> Result<ProcessingResult, TailwindProcessorError> {
        let start_time = std::time::Instant::now();

        // Parse all directives
        let directives = self.directive_parser.parse_tailwind_directives(css)?;
        let apply_directives = self.directive_parser.parse_apply_directives(css)?;
        let layer_directives = self.directive_parser.parse_layer_directives(css)?;

        // Process each directive type
        let mut processed_css = css.to_string();
        let mut processed_directives = Vec::new();

        // Process @tailwind directives
        for directive in &directives {
            let css_content = self.generate_css_for_directive(directive)?;
            processed_css = self.replace_directive(&processed_css, directive, &css_content)?;
            processed_directives.push(DirectiveMatch {
                directive_type: "tailwind".to_string(),
                content: format!("@tailwind {}", directive.to_string()),
                line_number: 0, // Will be filled by parser
                start_pos: 0,   // Will be filled by parser
                end_pos: 0,     // Will be filled by parser
            });
        }

        // Process @apply directives
        for apply in &apply_directives {
            let css_content = self.process_apply_directive(apply)?;
            processed_css = self.replace_apply_directive(&processed_css, apply, &css_content)?;
            processed_directives.push(DirectiveMatch {
                directive_type: "apply".to_string(),
                content: format!("@apply {}", apply.classes.join(" ")),
                line_number: apply.line_number,
                start_pos: 0, // Will be filled by parser
                end_pos: 0,   // Will be filled by parser
            });
        }

        // Process @layer directives
        for layer in &layer_directives {
            let css_content = self.process_layer_directive(layer)?;
            processed_css = self.replace_layer_directive(&processed_css, layer, &css_content)?;
            processed_directives.push(DirectiveMatch {
                directive_type: "layer".to_string(),
                content: format!("@layer {}", layer.to_string()),
                line_number: 0, // Will be filled by parser
                start_pos: 0,   // Will be filled by parser
                end_pos: 0,     // Will be filled by parser
            });
        }

        let processing_time = start_time.elapsed().as_millis() as u64;

        // Generate statistics
        let statistics = ProcessingStatistics {
            total_directives: processed_directives.len(),
            base_directives: directives
                .iter()
                .filter(|d| matches!(d, TailwindDirective::Base))
                .count(),
            component_directives: directives
                .iter()
                .filter(|d| matches!(d, TailwindDirective::Components))
                .count(),
            utility_directives: directives
                .iter()
                .filter(|d| matches!(d, TailwindDirective::Utilities))
                .count(),
            apply_directives: apply_directives.len(),
            layer_directives: layer_directives.len(),
            processing_time_ms: processing_time,
        };

        Ok(ProcessingResult {
            processed_css,
            directives_processed: processed_directives,
            statistics,
        })
    }

    /// Process @apply directive
    pub fn process_apply(&mut self, css: &str) -> Result<String, TailwindProcessorError> {
        let apply_directives = self.directive_parser.parse_apply_directives(css)?;
        let mut processed_css = css.to_string();

        for apply in apply_directives {
            let css_content = self.process_apply_directive(&apply)?;
            processed_css = self.replace_apply_directive(&processed_css, &apply, &css_content)?;
        }

        Ok(processed_css)
    }

    /// Process @layer directive
    pub fn process_layer(&mut self, css: &str) -> Result<String, TailwindProcessorError> {
        let layer_directives = self.directive_parser.parse_layer_directives(css)?;
        let mut processed_css = css.to_string();

        for layer in layer_directives {
            let css_content = self.process_layer_directive(&layer)?;
            processed_css = self.replace_layer_directive(&processed_css, &layer, &css_content)?;
        }

        Ok(processed_css)
    }

    /// Generate CSS for a specific directive
    fn generate_css_for_directive(
        &self,
        directive: &TailwindDirective,
    ) -> Result<String, TailwindProcessorError> {
        match directive {
            TailwindDirective::Base => {
                if self.config.base_styles {
                    Ok(self.css_injector.inject_base(&self.base_styles)?)
                } else {
                    Ok(String::new())
                }
            }
            TailwindDirective::Components => {
                if self.config.component_styles {
                    Ok(self
                        .css_injector
                        .inject_components(&self.component_styles)?)
                } else {
                    Ok(String::new())
                }
            }
            TailwindDirective::Utilities => {
                if self.config.utility_styles {
                    Ok(self.css_injector.inject_utilities(&self.utility_styles)?)
                } else {
                    Ok(String::new())
                }
            }
        }
    }

    /// Process @apply directive
    fn process_apply_directive(
        &mut self,
        apply: &ApplyDirective,
    ) -> Result<String, TailwindProcessorError> {
        let mut css = String::new();

        for class in &apply.classes {
            let class_css = self.resolve_class_to_css(class)?;
            css.push_str(&format!("{} {{ {} }}\n", apply.selector, class_css));
        }

        Ok(css)
    }

    /// Process @layer directive
    fn process_layer_directive(
        &self,
        layer: &LayerDirective,
    ) -> Result<String, TailwindProcessorError> {
        match layer {
            LayerDirective::Base => Ok(self.css_injector.inject_base(&self.base_styles)?),
            LayerDirective::Components => Ok(self
                .css_injector
                .inject_components(&self.component_styles)?),
            LayerDirective::Utilities => {
                Ok(self.css_injector.inject_utilities(&self.utility_styles)?)
            }
            LayerDirective::Custom(name) => {
                // Handle custom layers
                Ok(format!("/* Custom layer: {} */\n", name))
            }
        }
    }

    /// Resolve class name to CSS
    fn resolve_class_to_css(&mut self, class: &str) -> Result<String, TailwindProcessorError> {
        // Check cache first
        if let Some(cached) = self.cache.get_cached_apply(class) {
            return Ok(cached.clone());
        }

        // TODO: Implement actual class resolution
        // This would integrate with the existing CSS generator
        let css = format!("/* TODO: Resolve class {} */", class);

        // Cache the result
        self.cache.cache_apply(class.to_string(), css.clone());

        Ok(css)
    }

    /// Replace directive in CSS
    fn replace_directive(
        &self,
        css: &str,
        directive: &TailwindDirective,
        content: &str,
    ) -> Result<String, TailwindProcessorError> {
        let pattern = match directive {
            TailwindDirective::Base => r"@tailwind\s+base;",
            TailwindDirective::Components => r"@tailwind\s+components;",
            TailwindDirective::Utilities => r"@tailwind\s+utilities;",
        };

        let regex = Regex::new(pattern).map_err(|e| TailwindProcessorError::CSSParsingError {
            error: format!("Invalid regex pattern: {}", e),
        })?;

        Ok(regex.replace_all(css, content).to_string())
    }

    /// Replace @apply directive in CSS
    fn replace_apply_directive(
        &self,
        css: &str,
        apply: &ApplyDirective,
        content: &str,
    ) -> Result<String, TailwindProcessorError> {
        let pattern = format!(r"@apply\s+{};", apply.classes.join(r"\s+"));
        let regex = Regex::new(&pattern).map_err(|e| TailwindProcessorError::CSSParsingError {
            error: format!("Invalid regex pattern: {}", e),
        })?;

        Ok(regex.replace_all(css, content).to_string())
    }

    /// Replace @layer directive in CSS
    fn replace_layer_directive(
        &self,
        css: &str,
        layer: &LayerDirective,
        content: &str,
    ) -> Result<String, TailwindProcessorError> {
        let pattern = match layer {
            LayerDirective::Base => r"@layer\s+base;",
            LayerDirective::Components => r"@layer\s+components;",
            LayerDirective::Utilities => r"@layer\s+utilities;",
            LayerDirective::Custom(name) => &format!(r"@layer\s+{};", regex::escape(name)),
        };

        let regex = Regex::new(pattern).map_err(|e| TailwindProcessorError::CSSParsingError {
            error: format!("Invalid regex pattern: {}", e),
        })?;

        Ok(regex.replace_all(css, content).to_string())
    }
}

impl Default for TailwindConfig {
    fn default() -> Self {
        Self {
            base_styles: true,
            component_styles: true,
            utility_styles: true,
            apply_processing: true,
            layer_processing: true,
            custom_directives: Vec::new(),
        }
    }
}

impl ProcessorCache {
    /// Create a new processor cache
    pub fn new() -> Self {
        Self {
            directive_cache: HashMap::new(),
            apply_cache: HashMap::new(),
            layer_cache: HashMap::new(),
        }
    }

    /// Get cached directive
    pub fn get_cached_directive(&self, directive: &str) -> Option<&String> {
        self.directive_cache.get(directive)
    }

    /// Cache directive
    pub fn cache_directive(&mut self, directive: String, css: String) {
        self.directive_cache.insert(directive, css);
    }

    /// Get cached apply
    pub fn get_cached_apply(&self, class: &str) -> Option<&String> {
        self.apply_cache.get(class)
    }

    /// Cache apply
    pub fn cache_apply(&mut self, class: String, css: String) {
        self.apply_cache.insert(class, css);
    }
}

impl DirectiveParser {
    /// Create a new directive parser
    pub fn new() -> Self {
        let patterns = vec![
            DirectivePattern {
                name: "tailwind_base".to_string(),
                regex: Regex::new(r"@tailwind\s+base;").unwrap(),
                capture_groups: 0,
            },
            DirectivePattern {
                name: "tailwind_components".to_string(),
                regex: Regex::new(r"@tailwind\s+components;").unwrap(),
                capture_groups: 0,
            },
            DirectivePattern {
                name: "tailwind_utilities".to_string(),
                regex: Regex::new(r"@tailwind\s+utilities;").unwrap(),
                capture_groups: 0,
            },
            DirectivePattern {
                name: "apply".to_string(),
                regex: Regex::new(r"@apply\s+([^;]+);").unwrap(),
                capture_groups: 1,
            },
            DirectivePattern {
                name: "layer".to_string(),
                regex: Regex::new(r"@layer\s+([^;{]+);").unwrap(),
                capture_groups: 1,
            },
        ];

        Self {
            patterns,
            config: ParserConfig {
                case_sensitive: false,
                multiline: true,
                dotall: false,
            },
        }
    }

    /// Parse @tailwind directives
    pub fn parse_tailwind_directives(
        &self,
        css: &str,
    ) -> Result<Vec<TailwindDirective>, TailwindProcessorError> {
        let mut directives = Vec::new();

        // Parse base directives
        if self.patterns[0].regex.is_match(css) {
            directives.push(TailwindDirective::Base);
        }

        // Parse components directives
        if self.patterns[1].regex.is_match(css) {
            directives.push(TailwindDirective::Components);
        }

        // Parse utilities directives
        if self.patterns[2].regex.is_match(css) {
            directives.push(TailwindDirective::Utilities);
        }

        Ok(directives)
    }

    /// Parse @apply directives
    pub fn parse_apply_directives(
        &self,
        css: &str,
    ) -> Result<Vec<ApplyDirective>, TailwindProcessorError> {
        let mut directives = Vec::new();
        let apply_pattern = &self.patterns[3];

        for (line_num, line) in css.lines().enumerate() {
            for cap in apply_pattern.regex.captures_iter(line) {
                let classes_str = &cap[1];
                let classes: Vec<String> = classes_str
                    .split_whitespace()
                    .map(|s| s.to_string())
                    .collect();

                directives.push(ApplyDirective {
                    classes,
                    selector: "".to_string(), // Will be determined by context
                    line_number: line_num + 1,
                });
            }
        }

        Ok(directives)
    }

    /// Parse @layer directives
    pub fn parse_layer_directives(
        &self,
        css: &str,
    ) -> Result<Vec<LayerDirective>, TailwindProcessorError> {
        let mut directives = Vec::new();
        let layer_pattern = &self.patterns[4];

        for cap in layer_pattern.regex.captures_iter(css) {
            let layer_name = &cap[1];
            let directive = match layer_name {
                "base" => LayerDirective::Base,
                "components" => LayerDirective::Components,
                "utilities" => LayerDirective::Utilities,
                name => LayerDirective::Custom(name.to_string()),
            };
            directives.push(directive);
        }

        Ok(directives)
    }
}

impl CSSInjector {
    /// Create a new CSS injector
    pub fn new() -> Self {
        Self {
            base_css: String::new(),
            components_css: String::new(),
            utilities_css: String::new(),
            config: InjectionConfig {
                preserve_order: true,
                minify: false,
                source_map: false,
            },
        }
    }

    /// Inject base styles
    pub fn inject_base(&self, css: &str) -> Result<String, TailwindProcessorError> {
        // TODO: Implement actual base CSS injection
        Ok(format!("/* Base styles */\n{}", css))
    }

    /// Inject component styles
    pub fn inject_components(&self, css: &str) -> Result<String, TailwindProcessorError> {
        // TODO: Implement actual component CSS injection
        Ok(format!("/* Component styles */\n{}", css))
    }

    /// Inject utility styles
    pub fn inject_utilities(&self, css: &str) -> Result<String, TailwindProcessorError> {
        // TODO: Implement actual utility CSS injection
        Ok(format!("/* Utility styles */\n{}", css))
    }
}

impl std::fmt::Display for TailwindDirective {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TailwindDirective::Base => write!(f, "base"),
            TailwindDirective::Components => write!(f, "components"),
            TailwindDirective::Utilities => write!(f, "utilities"),
        }
    }
}

impl std::fmt::Display for LayerDirective {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LayerDirective::Base => write!(f, "base"),
            LayerDirective::Components => write!(f, "components"),
            LayerDirective::Utilities => write!(f, "utilities"),
            LayerDirective::Custom(name) => write!(f, "{}", name),
        }
    }
}

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

    #[test]
    fn test_tailwind_directive_processing() {
        let mut processor = TailwindProcessor::new();
        let css = "@tailwind base; @tailwind components; @tailwind utilities;";
        let result = processor.process_directives(css);
        assert!(result.is_ok());
    }

    #[test]
    fn test_apply_directive_processing() {
        let mut processor = TailwindProcessor::new();
        let css = ".btn { @apply bg-blue-500 text-white px-4 py-2; }";
        let result = processor.process_apply(css);
        assert!(result.is_ok());
    }

    #[test]
    fn test_layer_directive_processing() {
        let mut processor = TailwindProcessor::new();
        let css = "@layer base; @layer components; @layer utilities;";
        let result = processor.process_layer(css);
        assert!(result.is_ok());
    }

    #[test]
    fn test_directive_parser() {
        let parser = DirectiveParser::new();
        let css = "@tailwind base; @tailwind components; @tailwind utilities;";
        let directives = parser.parse_tailwind_directives(css).unwrap();
        assert_eq!(directives.len(), 3);
        assert!(directives.contains(&TailwindDirective::Base));
        assert!(directives.contains(&TailwindDirective::Components));
        assert!(directives.contains(&TailwindDirective::Utilities));
    }

    #[test]
    fn test_apply_parser() {
        let parser = DirectiveParser::new();
        let css = ".btn { @apply bg-blue-500 text-white px-4 py-2; }";
        let directives = parser.parse_apply_directives(css).unwrap();
        assert_eq!(directives.len(), 1);
        assert_eq!(directives[0].classes.len(), 4);
        assert!(directives[0].classes.contains(&"bg-blue-500".to_string()));
    }

    #[test]
    fn test_layer_parser() {
        let parser = DirectiveParser::new();
        let css = "@layer base; @layer components; @layer utilities;";
        let directives = parser.parse_layer_directives(css).unwrap();
        assert_eq!(directives.len(), 3);
        assert!(directives.contains(&LayerDirective::Base));
        assert!(directives.contains(&LayerDirective::Components));
        assert!(directives.contains(&LayerDirective::Utilities));
    }
}