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