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
//! Custom variant system for tailwind-rs
//!
//! Implements Tailwind CSS v4.1.13 @custom-variant features
//! This replaces the old aria, data, and supports theme keys with a unified system

use crate::error::{Result, TailwindError};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fmt;

/// Custom variant types supported by Tailwind v4.1.13
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CustomVariantType {
    /// ARIA attributes (aria-*)
    Aria,
    /// Data attributes (data-*)
    Data,
    /// CSS feature queries (@supports)
    Supports,
    /// Container queries (@container)
    Container,
    /// Media queries (@media)
    Media,
    /// User-defined custom variants
    Custom(String),
}

impl CustomVariantType {
    /// Get the prefix for this variant type
    pub fn prefix(&self) -> &'static str {
        match self {
            CustomVariantType::Aria => "aria-",
            CustomVariantType::Data => "data-",
            CustomVariantType::Supports => "supports-",
            CustomVariantType::Container => "container-",
            CustomVariantType::Media => "media-",
            CustomVariantType::Custom(_name) => {
                // Custom variants should not start or end with - or _
                // For now, return empty string for all custom variants
                ""
            }
        }
    }

    /// Validate a custom variant name
    pub fn validate_name(name: &str) -> Result<()> {
        if name.is_empty() {
            return Err(TailwindError::validation(
                "Custom variant name cannot be empty",
            ));
        }

        // Custom variants cannot start or end with - or _
        if name.starts_with('-')
            || name.starts_with('_')
            || name.ends_with('-')
            || name.ends_with('_')
        {
            return Err(TailwindError::validation(format!(
                "Custom variant '{}' cannot start or end with '-' or '_'",
                name
            )));
        }

        // Custom variants cannot start with @-
        if name.starts_with("@-") {
            return Err(TailwindError::validation(format!(
                "Custom variant '{}' cannot start with '@-'",
                name
            )));
        }

        Ok(())
    }
}

/// A custom variant definition
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CustomVariant {
    /// The variant type
    pub variant_type: CustomVariantType,
    /// The variant name (without prefix)
    pub name: String,
    /// The variant value (for aria, data, etc.)
    pub value: Option<String>,
    /// Whether this variant is enabled
    pub enabled: bool,
    /// Custom configuration for this variant
    pub config: HashMap<String, serde_json::Value>,
}

impl CustomVariant {
    /// Create a new custom variant
    pub fn new(
        variant_type: CustomVariantType,
        name: String,
        value: Option<String>,
    ) -> Result<Self> {
        // Validate the name
        CustomVariantType::validate_name(&name)?;

        Ok(Self {
            variant_type,
            name,
            value,
            enabled: true,
            config: HashMap::new(),
        })
    }

    /// Create an ARIA variant
    pub fn aria(name: String, value: Option<String>) -> Result<Self> {
        Self::new(CustomVariantType::Aria, name, value)
    }

    /// Create a data variant
    pub fn data(name: String, value: Option<String>) -> Result<Self> {
        Self::new(CustomVariantType::Data, name, value)
    }

    /// Create a supports variant
    pub fn supports(name: String, value: Option<String>) -> Result<Self> {
        Self::new(CustomVariantType::Supports, name, value)
    }

    /// Create a container variant
    pub fn container(name: String, value: Option<String>) -> Result<Self> {
        Self::new(CustomVariantType::Container, name, value)
    }

    /// Create a media variant
    pub fn media(name: String, value: Option<String>) -> Result<Self> {
        Self::new(CustomVariantType::Media, name, value)
    }

    /// Create a custom variant
    pub fn custom(name: String, value: Option<String>) -> Result<Self> {
        Self::new(CustomVariantType::Custom(name.clone()), name, value)
    }

    /// Get the full variant string (e.g., "aria-checked", "data-theme=dark")
    pub fn to_variant_string(&self) -> String {
        let prefix = self.variant_type.prefix();
        let base = format!("{}{}", prefix, self.name);

        if let Some(value) = &self.value {
            format!("{}={}", base, value)
        } else {
            base
        }
    }

    /// Get the CSS selector for this variant
    pub fn to_css_selector(&self) -> String {
        match &self.variant_type {
            CustomVariantType::Aria => {
                if let Some(value) = &self.value {
                    format!("[aria-{}={}]", self.name, value)
                } else {
                    format!("[aria-{}]", self.name)
                }
            }
            CustomVariantType::Data => {
                if let Some(value) = &self.value {
                    format!("[data-{}={}]", self.name, value)
                } else {
                    format!("[data-{}]", self.name)
                }
            }
            CustomVariantType::Supports => {
                format!("@supports ({})", self.name)
            }
            CustomVariantType::Container => {
                format!("@container {}", self.name)
            }
            CustomVariantType::Media => {
                format!("@media {}", self.name)
            }
            CustomVariantType::Custom(name) => {
                // Custom variants are handled by the user
                name.clone()
            }
        }
    }

    /// Enable this variant
    pub fn enable(&mut self) {
        self.enabled = true;
    }

    /// Disable this variant
    pub fn disable(&mut self) {
        self.enabled = false;
    }

    /// Set a configuration value
    pub fn set_config(&mut self, key: String, value: serde_json::Value) {
        self.config.insert(key, value);
    }

    /// Get a configuration value
    pub fn get_config(&self, key: &str) -> Option<&serde_json::Value> {
        self.config.get(key)
    }
}

/// Manager for custom variants
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CustomVariantManager {
    /// Registered custom variants
    variants: HashMap<String, CustomVariant>,
    /// Known variant values for suggestions
    known_values: HashMap<String, HashSet<String>>,
}

impl CustomVariantManager {
    /// Create a new custom variant manager
    pub fn new() -> Self {
        Self {
            variants: HashMap::new(),
            known_values: HashMap::new(),
        }
    }

    /// Register a custom variant
    pub fn register(&mut self, variant: CustomVariant) -> Result<()> {
        let key = variant.to_variant_string();

        // Check for conflicts
        if self.variants.contains_key(&key) {
            return Err(TailwindError::validation(format!(
                "Custom variant '{}' is already registered",
                key
            )));
        }

        self.variants.insert(key, variant);
        Ok(())
    }

    /// Get a custom variant by key
    pub fn get(&self, key: &str) -> Option<&CustomVariant> {
        self.variants.get(key)
    }

    /// Get all registered variants
    pub fn get_all(&self) -> &HashMap<String, CustomVariant> {
        &self.variants
    }

    /// Get variants by type
    pub fn get_by_type(&self, variant_type: &CustomVariantType) -> Vec<&CustomVariant> {
        self.variants
            .values()
            .filter(|v| &v.variant_type == variant_type)
            .collect()
    }

    /// Remove a custom variant
    pub fn remove(&mut self, key: &str) -> Option<CustomVariant> {
        self.variants.remove(key)
    }

    /// Check if a variant is registered
    pub fn contains(&self, key: &str) -> bool {
        self.variants.contains_key(key)
    }

    /// Add known values for a variant (for suggestions)
    pub fn add_known_values(&mut self, variant_key: String, values: HashSet<String>) {
        self.known_values.insert(variant_key, values);
    }

    /// Get known values for a variant
    pub fn get_known_values(&self, variant_key: &str) -> Option<&HashSet<String>> {
        self.known_values.get(variant_key)
    }

    /// Get suggestions for a variant
    pub fn get_suggestions(&self, partial: &str) -> Vec<String> {
        let mut suggestions = Vec::new();

        // Add exact matches
        for key in self.variants.keys() {
            if key.starts_with(partial) {
                suggestions.push(key.clone());
            }
        }

        // Add known values
        for (variant_key, values) in &self.known_values {
            if variant_key.starts_with(partial) {
                for value in values {
                    suggestions.push(format!("{}={}", variant_key, value));
                }
            }
        }

        suggestions.sort();
        suggestions.dedup();
        suggestions
    }

    /// Validate a variant string
    pub fn validate_variant(&self, variant: &str) -> Result<()> {
        // Check if it's a registered variant
        if self.variants.contains_key(variant) {
            return Ok(());
        }

        // Check if it matches a known pattern
        if variant.starts_with("aria-")
            || variant.starts_with("data-")
            || variant.starts_with("supports-")
            || variant.starts_with("container-")
            || variant.starts_with("media-")
        {
            return Ok(());
        }

        // Check for invalid patterns
        if variant.starts_with("@-") {
            return Err(TailwindError::validation(format!(
                "Variant '{}' cannot start with '@-'",
                variant
            )));
        }

        if variant.starts_with('-')
            || variant.starts_with('_')
            || variant.ends_with('-')
            || variant.ends_with('_')
        {
            return Err(TailwindError::validation(format!(
                "Variant '{}' cannot start or end with '-' or '_'",
                variant
            )));
        }

        Ok(())
    }

    /// Create default variants (migrated from old theme keys)
    pub fn with_defaults() -> Self {
        let mut manager = Self::new();

        // Add common ARIA variants
        let aria_variants = vec![
            ("checked", None),
            ("disabled", None),
            ("expanded", None),
            ("hidden", None),
            ("pressed", None),
            ("required", None),
            ("selected", None),
        ];

        for (name, value) in aria_variants {
            if let Ok(variant) = CustomVariant::aria(name.to_string(), value) {
                let _ = manager.register(variant);
            }
        }

        // Add common data variants
        let data_variants = vec![
            ("theme", Some("dark".to_string())),
            ("theme", Some("light".to_string())),
            ("state", Some("loading".to_string())),
            ("state", Some("error".to_string())),
        ];

        for (name, value) in data_variants {
            if let Ok(variant) = CustomVariant::data(name.to_string(), value) {
                let _ = manager.register(variant);
            }
        }

        // Add common supports variants
        let supports_variants = vec![("backdrop-filter", None), ("grid", None), ("flexbox", None)];

        for (name, value) in supports_variants {
            if let Ok(variant) = CustomVariant::supports(name.to_string(), value) {
                let _ = manager.register(variant);
            }
        }

        manager
    }
}

impl Default for CustomVariantManager {
    fn default() -> Self {
        Self::with_defaults()
    }
}

impl fmt::Display for CustomVariant {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_variant_string())
    }
}

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

    #[test]
    fn test_custom_variant_creation() {
        let variant = CustomVariant::aria("checked".to_string(), None).unwrap();
        assert_eq!(variant.to_variant_string(), "aria-checked");
        assert_eq!(variant.to_css_selector(), "[aria-checked]");
    }

    #[test]
    fn test_custom_variant_with_value() {
        let variant = CustomVariant::data("theme".to_string(), Some("dark".to_string())).unwrap();
        assert_eq!(variant.to_variant_string(), "data-theme=dark");
        assert_eq!(variant.to_css_selector(), "[data-theme=dark]");
    }

    #[test]
    fn test_custom_variant_validation() {
        // Valid variants
        assert!(CustomVariantType::validate_name("valid-name").is_ok());
        assert!(CustomVariantType::validate_name("valid_name").is_ok());
        assert!(CustomVariantType::validate_name("validname").is_ok());

        // Invalid variants
        assert!(CustomVariantType::validate_name("-invalid").is_err());
        assert!(CustomVariantType::validate_name("invalid-").is_err());
        assert!(CustomVariantType::validate_name("_invalid").is_err());
        assert!(CustomVariantType::validate_name("invalid_").is_err());
    }

    #[test]
    fn test_custom_variant_manager() {
        let mut manager = CustomVariantManager::new();

        let variant = CustomVariant::aria("checked".to_string(), None).unwrap();
        manager.register(variant).unwrap();

        assert!(manager.contains("aria-checked"));
        assert!(manager.get("aria-checked").is_some());
    }

    #[test]
    fn test_custom_variant_suggestions() {
        let manager = CustomVariantManager::with_defaults();

        let suggestions = manager.get_suggestions("aria-");
        assert!(!suggestions.is_empty());
        assert!(suggestions.contains(&"aria-checked".to_string()));
    }

    #[test]
    fn test_custom_variant_validation_in_manager() {
        let manager = CustomVariantManager::with_defaults();

        // Valid variants
        assert!(manager.validate_variant("aria-checked").is_ok());
        assert!(manager.validate_variant("data-theme=dark").is_ok());

        // Invalid variants
        assert!(manager.validate_variant("@-invalid").is_err());
        assert!(manager.validate_variant("-invalid").is_err());
        assert!(manager.validate_variant("invalid-").is_err());
    }
}