tailwind_css_fixes/modules/svg/stroke/stroke_width/
mod.rs

1use super::*;
2
3#[derive(Debug, Clone)]
4pub struct TailwindStrokeWidth {
5    kind: StrokeWidth,
6}
7
8#[derive(Debug, Clone)]
9enum StrokeWidth {
10    Unit(i32),
11    Length(LengthUnit),
12}
13
14impl Display for StrokeWidth {
15    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
16        match self {
17            StrokeWidth::Unit(s) => write!(f, "{}", s),
18            StrokeWidth::Length(s) => write!(f, "{}", s.get_class()),
19        }
20    }
21}
22
23impl Display for TailwindStrokeWidth {
24    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
25        write!(f, "stroke-{}", self.kind)
26    }
27}
28
29impl TailwindInstance for TailwindStrokeWidth {
30    fn attributes(&self, _: &TailwindBuilder) -> CssAttributes {
31        let width = match self.kind {
32            StrokeWidth::Unit(s) => format!("{}px", s),
33            StrokeWidth::Length(s) => s.get_properties(),
34        };
35        css_attributes! {
36            "stroke-width" => width
37        }
38    }
39}
40
41impl TailwindStrokeWidth {
42    /// https://tailwindcss.com/docs/stroke-width
43    pub fn try_new(width: &str) -> Result<Self> {
44        Ok(Self { kind: StrokeWidth::parse(width)? })
45    }
46}
47
48impl StrokeWidth {
49    pub fn parse(width: &str) -> Result<Self> {
50        let a = TailwindArbitrary::from(width);
51        Self::maybe_no_unit(&a).or_else(|_| Self::maybe_length(&a))
52    }
53    fn maybe_no_unit(arbitrary: &TailwindArbitrary) -> Result<Self> {
54        Ok(Self::Unit(arbitrary.as_integer()?))
55    }
56    fn maybe_length(arbitrary: &TailwindArbitrary) -> Result<Self> {
57        Ok(Self::Length(arbitrary.as_length_or_fraction()?))
58    }
59}