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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//! Gradient support for tailwind-rs
//!
//! This module provides comprehensive support for Tailwind CSS gradients,
//! including background gradients, gradient directions, and gradient stops.
//! Examples: bg-gradient-to-r, from-blue-500, via-purple-500, to-pink-500, etc.

use crate::classes::ClassBuilder;
use crate::color::Color;
use serde::{Deserialize, Serialize};
use std::fmt;

/// Represents gradient directions in Tailwind CSS
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum GradientDirection {
    /// To right
    ToRight,
    /// To left
    ToLeft,
    /// To top
    ToTop,
    /// To bottom
    ToBottom,
    /// To top right
    ToTopRight,
    /// To top left
    ToTopLeft,
    /// To bottom right
    ToBottomRight,
    /// To bottom left
    ToBottomLeft,
}

impl GradientDirection {
    /// Convert to Tailwind CSS class name
    pub fn to_class_name(&self) -> String {
        match self {
            GradientDirection::ToRight => "to-r".to_string(),
            GradientDirection::ToLeft => "to-l".to_string(),
            GradientDirection::ToTop => "to-t".to_string(),
            GradientDirection::ToBottom => "to-b".to_string(),
            GradientDirection::ToTopRight => "to-tr".to_string(),
            GradientDirection::ToTopLeft => "to-tl".to_string(),
            GradientDirection::ToBottomRight => "to-br".to_string(),
            GradientDirection::ToBottomLeft => "to-bl".to_string(),
        }
    }

    /// Convert to CSS value
    pub fn to_css_value(&self) -> String {
        match self {
            GradientDirection::ToRight => "to right".to_string(),
            GradientDirection::ToLeft => "to left".to_string(),
            GradientDirection::ToTop => "to top".to_string(),
            GradientDirection::ToBottom => "to bottom".to_string(),
            GradientDirection::ToTopRight => "to top right".to_string(),
            GradientDirection::ToTopLeft => "to top left".to_string(),
            GradientDirection::ToBottomRight => "to bottom right".to_string(),
            GradientDirection::ToBottomLeft => "to bottom left".to_string(),
        }
    }
}

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

/// Represents gradient stops in Tailwind CSS
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum GradientStop {
    /// From stop (starting color)
    From(Color),
    /// Via stop (middle color)
    Via(Color),
    /// To stop (ending color)
    To(Color),
}

impl GradientStop {
    /// Create a from stop
    pub fn from(color: Color) -> Self {
        Self::From(color)
    }

    /// Create a via stop
    pub fn via(color: Color) -> Self {
        Self::Via(color)
    }

    /// Create a to stop
    pub fn to(color: Color) -> Self {
        Self::To(color)
    }

    /// Convert to Tailwind CSS class name
    pub fn to_class_name(&self) -> String {
        match self {
            GradientStop::From(color) => format!("from-{}", color.to_class_name()),
            GradientStop::Via(color) => format!("via-{}", color.to_class_name()),
            GradientStop::To(color) => format!("to-{}", color.to_class_name()),
        }
    }

    /// Convert to CSS value
    pub fn to_css_value(&self) -> String {
        match self {
            GradientStop::From(color) => color.to_css_value(),
            GradientStop::Via(color) => color.to_css_value(),
            GradientStop::To(color) => color.to_css_value(),
        }
    }
}

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

/// Represents a complete gradient configuration
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Gradient {
    /// Gradient direction
    pub direction: GradientDirection,
    /// Gradient stops
    pub stops: Vec<GradientStop>,
}

impl Gradient {
    /// Create a new gradient
    pub fn new(direction: GradientDirection) -> Self {
        Self {
            direction,
            stops: Vec::new(),
        }
    }

    /// Add a from stop
    pub fn from(mut self, color: Color) -> Self {
        self.stops.push(GradientStop::from(color));
        self
    }

    /// Add a via stop
    pub fn via(mut self, color: Color) -> Self {
        self.stops.push(GradientStop::via(color));
        self
    }

    /// Add a to stop
    pub fn to(mut self, color: Color) -> Self {
        self.stops.push(GradientStop::to(color));
        self
    }

    /// Convert to Tailwind CSS class names
    pub fn to_class_names(&self) -> Vec<String> {
        let mut classes = vec![format!("bg-gradient-{}", self.direction.to_class_name())];
        
        for stop in &self.stops {
            classes.push(stop.to_class_name());
        }
        
        classes
    }

    /// Convert to CSS value
    pub fn to_css_value(&self) -> String {
        let mut css = format!("linear-gradient({}, ", self.direction.to_css_value());
        
        let stop_values: Vec<String> = self.stops.iter()
            .map(|stop| stop.to_css_value())
            .collect();
        
        css.push_str(&stop_values.join(", "));
        css.push(')');
        css
    }

    /// Validate the gradient
    pub fn validate(&self) -> Result<(), GradientError> {
        if self.stops.is_empty() {
            return Err(GradientError::NoStops);
        }

        // Check for at least one from and one to stop
        let has_from = self.stops.iter().any(|stop| matches!(stop, GradientStop::From(_)));
        let has_to = self.stops.iter().any(|stop| matches!(stop, GradientStop::To(_)));

        if !has_from {
            return Err(GradientError::MissingFromStop);
        }

        if !has_to {
            return Err(GradientError::MissingToStop);
        }

        Ok(())
    }
}

/// Errors that can occur when working with gradients
#[derive(Debug, thiserror::Error)]
pub enum GradientError {
    #[error("No gradient stops defined")]
    NoStops,
    
    #[error("Missing 'from' stop")]
    MissingFromStop,
    
    #[error("Missing 'to' stop")]
    MissingToStop,
}

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

/// Trait for adding gradient utilities to a class builder
pub trait GradientUtilities {
    /// Add a gradient direction
    fn gradient_direction(self, direction: GradientDirection) -> Self;
    
    /// Add a gradient from stop
    fn gradient_from(self, color: Color) -> Self;
    
    /// Add a gradient via stop
    fn gradient_via(self, color: Color) -> Self;
    
    /// Add a gradient to stop
    fn gradient_to(self, color: Color) -> Self;
    
    /// Add a complete gradient
    fn gradient(self, gradient: Gradient) -> Self;
    
    /// Add a simple two-color gradient
    fn gradient_simple(self, direction: GradientDirection, from: Color, to: Color) -> Self;
    
    /// Add a three-color gradient
    fn gradient_three(self, direction: GradientDirection, from: Color, via: Color, to: Color) -> Self;
    
    /// Add a gradient from color names
    fn gradient_from_names(self, direction: GradientDirection, from: &str, to: &str) -> Self;
    
    /// Add a gradient from color names with via
    fn gradient_from_names_with_via(self, direction: GradientDirection, from: &str, via: &str, to: &str) -> Self;
}

impl GradientUtilities for ClassBuilder {
    fn gradient_direction(self, direction: GradientDirection) -> Self {
        self.class(format!("bg-gradient-{}", direction.to_class_name()))
    }
    
    fn gradient_from(self, color: Color) -> Self {
        self.class(format!("from-{}", color.to_class_name()))
    }
    
    fn gradient_via(self, color: Color) -> Self {
        self.class(format!("via-{}", color.to_class_name()))
    }
    
    fn gradient_to(self, color: Color) -> Self {
        self.class(format!("to-{}", color.to_class_name()))
    }
    
    fn gradient(self, gradient: Gradient) -> Self {
        let mut builder = self;
        let class_names = gradient.to_class_names();
        
        for class_name in class_names {
            builder = builder.class(class_name);
        }
        
        builder
    }
    
    fn gradient_simple(self, direction: GradientDirection, from: Color, to: Color) -> Self {
        let gradient = Gradient::new(direction)
            .from(from)
            .to(to);
        
        self.gradient(gradient)
    }
    
    fn gradient_three(self, direction: GradientDirection, from: Color, via: Color, to: Color) -> Self {
        let gradient = Gradient::new(direction)
            .from(from)
            .via(via)
            .to(to);
        
        self.gradient(gradient)
    }
    
    fn gradient_from_names(self, direction: GradientDirection, from: &str, to: &str) -> Self {
        // Parse color names (simplified - in real implementation, you'd want more robust parsing)
        let from_color = parse_color_name(from);
        let to_color = parse_color_name(to);
        
        self.gradient_simple(direction, from_color, to_color)
    }
    
    fn gradient_from_names_with_via(self, direction: GradientDirection, from: &str, via: &str, to: &str) -> Self {
        // Parse color names (simplified - in real implementation, you'd want more robust parsing)
        let from_color = parse_color_name(from);
        let via_color = parse_color_name(via);
        let to_color = parse_color_name(to);
        
        self.gradient_three(direction, from_color, via_color, to_color)
    }
}

/// Parse a color name string into a Color struct
/// This is a simplified implementation - in production, you'd want more robust parsing
fn parse_color_name(color_name: &str) -> Color {
    // Handle common color patterns like "blue-500", "red-600", etc.
    let parts: Vec<&str> = color_name.split('-').collect();
    
    if parts.len() == 2 {
        let palette = match parts[0] {
            "slate" => ColorPalette::Slate,
            "gray" => ColorPalette::Gray,
            "zinc" => ColorPalette::Zinc,
            "neutral" => ColorPalette::Neutral,
            "stone" => ColorPalette::Stone,
            "red" => ColorPalette::Red,
            "orange" => ColorPalette::Orange,
            "amber" => ColorPalette::Amber,
            "yellow" => ColorPalette::Yellow,
            "lime" => ColorPalette::Lime,
            "green" => ColorPalette::Green,
            "emerald" => ColorPalette::Emerald,
            "teal" => ColorPalette::Teal,
            "cyan" => ColorPalette::Cyan,
            "sky" => ColorPalette::Sky,
            "blue" => ColorPalette::Blue,
            "indigo" => ColorPalette::Indigo,
            "violet" => ColorPalette::Violet,
            "purple" => ColorPalette::Purple,
            "fuchsia" => ColorPalette::Fuchsia,
            "pink" => ColorPalette::Pink,
            "rose" => ColorPalette::Rose,
            _ => ColorPalette::Blue, // Default fallback
        };
        
        let shade = match parts[1] {
            "50" => ColorShade::Shade50,
            "100" => ColorShade::Shade100,
            "200" => ColorShade::Shade200,
            "300" => ColorShade::Shade300,
            "400" => ColorShade::Shade400,
            "500" => ColorShade::Shade500,
            "600" => ColorShade::Shade600,
            "700" => ColorShade::Shade700,
            "800" => ColorShade::Shade800,
            "900" => ColorShade::Shade900,
            "950" => ColorShade::Shade950,
            _ => ColorShade::Shade500, // Default fallback
        };
        
        Color::new(palette, shade)
    } else {
        // Default to blue-500 if parsing fails
        Color::new(ColorPalette::Blue, ColorShade::Shade500)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_gradient_direction() {
        assert_eq!(GradientDirection::ToRight.to_class_name(), "to-r");
        assert_eq!(GradientDirection::ToLeft.to_class_name(), "to-l");
        assert_eq!(GradientDirection::ToTop.to_class_name(), "to-t");
        assert_eq!(GradientDirection::ToBottom.to_class_name(), "to-b");
        assert_eq!(GradientDirection::ToTopRight.to_class_name(), "to-tr");
        assert_eq!(GradientDirection::ToTopLeft.to_class_name(), "to-tl");
        assert_eq!(GradientDirection::ToBottomRight.to_class_name(), "to-br");
        assert_eq!(GradientDirection::ToBottomLeft.to_class_name(), "to-bl");
    }
    
    #[test]
    fn test_gradient_direction_css() {
        assert_eq!(GradientDirection::ToRight.to_css_value(), "to right");
        assert_eq!(GradientDirection::ToLeft.to_css_value(), "to left");
        assert_eq!(GradientDirection::ToTop.to_css_value(), "to top");
        assert_eq!(GradientDirection::ToBottom.to_css_value(), "to bottom");
    }
    
    #[test]
    fn test_gradient_stop() {
        let from_stop = GradientStop::from(Color::new(ColorPalette::Blue, ColorShade::Shade500));
        assert_eq!(from_stop.to_class_name(), "from-blue-500");
        
        let via_stop = GradientStop::via(Color::new(ColorPalette::Purple, ColorShade::Shade500));
        assert_eq!(via_stop.to_class_name(), "via-purple-500");
        
        let to_stop = GradientStop::to(Color::new(ColorPalette::Pink, ColorShade::Shade500));
        assert_eq!(to_stop.to_class_name(), "to-pink-500");
    }
    
    #[test]
    fn test_gradient_creation() {
        let gradient = Gradient::new(GradientDirection::ToRight)
            .from(Color::new(ColorPalette::Blue, ColorShade::Shade500))
            .to(Color::new(ColorPalette::Red, ColorShade::Shade500));
        
        let class_names = gradient.to_class_names();
        assert!(class_names.contains(&"bg-gradient-to-r".to_string()));
        assert!(class_names.contains(&"from-blue-500".to_string()));
        assert!(class_names.contains(&"to-red-500".to_string()));
    }
    
    #[test]
    fn test_gradient_three_colors() {
        let gradient = Gradient::new(GradientDirection::ToRight)
            .from(Color::new(ColorPalette::Blue, ColorShade::Shade500))
            .via(Color::new(ColorPalette::Purple, ColorShade::Shade500))
            .to(Color::new(ColorPalette::Pink, ColorShade::Shade500));
        
        let class_names = gradient.to_class_names();
        assert!(class_names.contains(&"bg-gradient-to-r".to_string()));
        assert!(class_names.contains(&"from-blue-500".to_string()));
        assert!(class_names.contains(&"via-purple-500".to_string()));
        assert!(class_names.contains(&"to-pink-500".to_string()));
    }
    
    #[test]
    fn test_gradient_validation() {
        // Valid gradient
        let valid_gradient = Gradient::new(GradientDirection::ToRight)
            .from(Color::new(ColorPalette::Blue, ColorShade::Shade500))
            .to(Color::new(ColorPalette::Red, ColorShade::Shade500));
        assert!(valid_gradient.validate().is_ok());
        
        // Invalid gradient - no stops
        let invalid_gradient = Gradient::new(GradientDirection::ToRight);
        assert!(invalid_gradient.validate().is_err());
        
        // Invalid gradient - missing from
        let invalid_gradient2 = Gradient::new(GradientDirection::ToRight)
            .to(Color::new(ColorPalette::Red, ColorShade::Shade500));
        assert!(invalid_gradient2.validate().is_err());
        
        // Invalid gradient - missing to
        let invalid_gradient3 = Gradient::new(GradientDirection::ToRight)
            .from(Color::new(ColorPalette::Blue, ColorShade::Shade500));
        assert!(invalid_gradient3.validate().is_err());
    }
    
    #[test]
    fn test_gradient_utilities() {
        let classes = ClassBuilder::new()
            .gradient_direction(GradientDirection::ToRight)
            .gradient_from(Color::new(ColorPalette::Blue, ColorShade::Shade500))
            .gradient_to(Color::new(ColorPalette::Red, ColorShade::Shade500))
            .build();
        
        let css_classes = classes.to_css_classes();
        assert!(css_classes.contains("bg-gradient-to-r"));
        assert!(css_classes.contains("from-blue-500"));
        assert!(css_classes.contains("to-red-500"));
    }
    
    #[test]
    fn test_gradient_simple() {
        let classes = ClassBuilder::new()
            .gradient_simple(
                GradientDirection::ToRight,
                Color::new(ColorPalette::Blue, ColorShade::Shade500),
                Color::new(ColorPalette::Red, ColorShade::Shade500)
            )
            .build();
        
        let css_classes = classes.to_css_classes();
        assert!(css_classes.contains("bg-gradient-to-r"));
        assert!(css_classes.contains("from-blue-500"));
        assert!(css_classes.contains("to-red-500"));
    }
    
    #[test]
    fn test_gradient_three_colors_utility() {
        let classes = ClassBuilder::new()
            .gradient_three(
                GradientDirection::ToRight,
                Color::new(ColorPalette::Blue, ColorShade::Shade500),
                Color::new(ColorPalette::Purple, ColorShade::Shade500),
                Color::new(ColorPalette::Pink, ColorShade::Shade500)
            )
            .build();
        
        let css_classes = classes.to_css_classes();
        assert!(css_classes.contains("bg-gradient-to-r"));
        assert!(css_classes.contains("from-blue-500"));
        assert!(css_classes.contains("via-purple-500"));
        assert!(css_classes.contains("to-pink-500"));
    }
    
    #[test]
    fn test_gradient_from_names() {
        let classes = ClassBuilder::new()
            .gradient_from_names(GradientDirection::ToRight, "blue-500", "red-500")
            .build();
        
        let css_classes = classes.to_css_classes();
        assert!(css_classes.contains("bg-gradient-to-r"));
        assert!(css_classes.contains("from-blue-500"));
        assert!(css_classes.contains("to-red-500"));
    }
    
    #[test]
    fn test_gradient_from_names_with_via() {
        let classes = ClassBuilder::new()
            .gradient_from_names_with_via(GradientDirection::ToRight, "blue-500", "purple-500", "pink-500")
            .build();
        
        let css_classes = classes.to_css_classes();
        assert!(css_classes.contains("bg-gradient-to-r"));
        assert!(css_classes.contains("from-blue-500"));
        assert!(css_classes.contains("via-purple-500"));
        assert!(css_classes.contains("to-pink-500"));
    }
    
    #[test]
    fn test_gradient_display() {
        let gradient = Gradient::new(GradientDirection::ToRight)
            .from(Color::new(ColorPalette::Blue, ColorShade::Shade500))
            .to(Color::new(ColorPalette::Red, ColorShade::Shade500));
        
        let display = format!("{}", gradient);
        assert!(display.contains("bg-gradient-to-r"));
        assert!(display.contains("from-blue-500"));
        assert!(display.contains("to-red-500"));
    }
    
    // #[test]
    // fn test_gradient_css_value() {
    //     let gradient = Gradient::new(GradientDirection::ToRight)
    //         .from(Color::Blue)
    //         .to(Color::Red);
    //     
    //     let css_value = gradient.to_css_value();
    //     assert!(css_value.contains("linear-gradient(to right"));
    //     assert!(css_value.contains("blue"));
    //     assert!(css_value.contains("red"));
    // }
}