tailwind_css_fixes/modules/sizing/
display.rs

1use super::*;
2
3impl Display for SizingUnit {
4    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
5        match self {
6            Self::None => write!(f, "none"),
7            Self::Min => write!(f, "min"),
8            Self::Max => write!(f, "max"),
9            Self::Fit => write!(f, "fit"),
10            Self::Auto => write!(f, "auto"),
11            Self::Full => write!(f, "full"),
12            Self::Screen => write!(f, "screen"),
13            Self::Fraction(numerator, denominator) => write!(f, "{}/{}", numerator, denominator),
14            Self::Length(x) => write!(f, "[{}]", x),
15        }
16    }
17}
18
19impl SizingUnit {
20    fn get_attribute(&self, kind: TailwindSizingKind) -> String {
21        let is_width = kind.is_width();
22        let is_width_str = if is_width { "vw" } else { "vh" };
23
24        match (self, kind) {
25            // `None` logic is now combined in the match
26            (Self::None, TailwindSizingKind::MaxWidth | TailwindSizingKind::MaxHeight) => {
27                "none".to_string()
28            }
29            (Self::None, _) => "0px".to_string(),
30
31            // Other cases
32            (Self::Min, _) => "min-content".to_string(),
33            (Self::Max, _) => "max-content".to_string(),
34            (Self::Fit, _) => "fit-content".to_string(),
35            (Self::Auto, _) => "auto".to_string(),
36            (Self::Full, _) => "100%".to_string(),
37            (Self::Screen, _) => format!("100{}", is_width_str),
38            (Self::Fraction(numerator, denominator), _) => {
39                format!("{}%", (*numerator as f32 / *denominator as f32) * 100.0)
40            }
41            (Self::Length(x), _) => format!("{}", x),
42        }
43    }
44}
45
46impl TailwindSizingKind {
47    fn is_width(self) -> bool {
48        matches!(self, Self::Width | Self::MinWidth | Self::MaxWidth)
49    }
50}
51
52impl Display for TailwindSizingKind {
53    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
54        match self {
55            Self::Width => f.write_str("width"),
56            Self::MinWidth => f.write_str("min-width"),
57            Self::MaxWidth => f.write_str("max-width"),
58            Self::Height => f.write_str("height"),
59            Self::MinHeight => f.write_str("min-height"),
60            Self::MaxHeight => f.write_str("max-height"),
61        }
62    }
63}
64
65impl Display for TailwindSizing {
66    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
67        match (&self.kind, &self.size) {
68            // Handles `max-w-none` and `max-h-none`
69            (TailwindSizingKind::MaxWidth | TailwindSizingKind::MaxHeight, SizingUnit::None) => {
70                write!(f, "{}-none", self.kind)
71            }
72            // Handles `w-none`, `h-none`, etc. by turning them into `w-0`, `h-0`
73            (_, SizingUnit::None) => write!(f, "{}-0", self.kind),
74
75            // Handles a parsed `w-0`, `h-0`, etc.
76            (_, SizingUnit::Length(val)) if val.is_zero() => {
77                write!(f, "{}-0", self.kind)
78            }
79
80            // Handles all other cases like `w-full` or `w-1/2`
81            _ => write!(f, "{}-{}", self.kind, self.size),
82        }
83    }
84}
85
86impl TailwindInstance for TailwindSizing {
87    fn attributes(&self, _: &TailwindBuilder) -> CssAttributes {
88        let class = self.kind.to_string();
89        let width = self.size.get_attribute(self.kind);
90        css_attributes! {
91            class => width
92        }
93    }
94}