tailwind_rs_core/utilities/effects/
box_shadow.rs

1//! Box shadow utilities for tailwind-rs
2//!
3//! This module provides utilities for box shadow effects.
4
5use crate::classes::ClassBuilder;
6use serde::{Deserialize, Serialize};
7use std::fmt;
8
9/// Box shadow values
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
11pub enum BoxShadow {
12    /// No shadow
13    None,
14    /// Small shadow
15    Sm,
16    /// Default shadow
17    Default,
18    /// Medium shadow
19    Md,
20    /// Large shadow
21    Lg,
22    /// Extra large shadow
23    Xl,
24    /// 2x large shadow
25    Xl2,
26    /// Inner shadow
27    Inner,
28}
29
30impl fmt::Display for BoxShadow {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            BoxShadow::None => write!(f, "shadow-none"),
34            BoxShadow::Sm => write!(f, "shadow-sm"),
35            BoxShadow::Default => write!(f, "shadow"),
36            BoxShadow::Md => write!(f, "shadow-md"),
37            BoxShadow::Lg => write!(f, "shadow-lg"),
38            BoxShadow::Xl => write!(f, "shadow-xl"),
39            BoxShadow::Xl2 => write!(f, "shadow-2xl"),
40            BoxShadow::Inner => write!(f, "shadow-inner"),
41        }
42    }
43}
44
45/// Trait for adding box shadow utilities to a class builder
46pub trait BoxShadowUtilities {
47    fn box_shadow(self, shadow: BoxShadow) -> Self;
48}
49
50impl BoxShadowUtilities for ClassBuilder {
51    fn box_shadow(self, shadow: BoxShadow) -> Self {
52        self.class(shadow.to_string())
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_box_shadow_display() {
62        assert_eq!(BoxShadow::None.to_string(), "shadow-none");
63        assert_eq!(BoxShadow::Sm.to_string(), "shadow-sm");
64        assert_eq!(BoxShadow::Default.to_string(), "shadow");
65        assert_eq!(BoxShadow::Md.to_string(), "shadow-md");
66        assert_eq!(BoxShadow::Lg.to_string(), "shadow-lg");
67        assert_eq!(BoxShadow::Xl.to_string(), "shadow-xl");
68        assert_eq!(BoxShadow::Xl2.to_string(), "shadow-2xl");
69        assert_eq!(BoxShadow::Inner.to_string(), "shadow-inner");
70    }
71
72    #[test]
73    fn test_box_shadow_utilities() {
74        let classes = ClassBuilder::new()
75            .box_shadow(BoxShadow::Lg)
76            .build();
77        
78        assert!(classes.to_css_classes().contains("shadow-lg"));
79    }
80
81    #[test]
82    fn test_box_shadow_serialization() {
83        let box_shadow = BoxShadow::Md;
84        let serialized = serde_json::to_string(&box_shadow).unwrap();
85        let deserialized: BoxShadow = serde_json::from_str(&serialized).unwrap();
86        assert_eq!(box_shadow, deserialized);
87    }
88
89    #[test]
90    fn test_box_shadow_equality_and_hash() {
91        let box_shadow1 = BoxShadow::Lg;
92        let box_shadow2 = BoxShadow::Lg;
93        let box_shadow3 = BoxShadow::Md;
94        
95        assert_eq!(box_shadow1, box_shadow2);
96        assert_ne!(box_shadow1, box_shadow3);
97        
98        // Test that equal effects have the same hash
99        use std::collections::hash_map::DefaultHasher;
100        use std::hash::{Hash, Hasher};
101        
102        let mut hasher1 = DefaultHasher::new();
103        let mut hasher2 = DefaultHasher::new();
104        box_shadow1.hash(&mut hasher1);
105        box_shadow2.hash(&mut hasher2);
106        assert_eq!(hasher1.finish(), hasher2.finish());
107    }
108}