tailwind_css_fixes/modules/typography/underline_offset/
mod.rs

1use super::*;
2
3#[doc=include_str!("readme.md")]
4#[derive(Debug, Clone)]
5pub struct TailwindUnderlineOffset {
6    kind: UnderlineOffset,
7}
8
9#[derive(Debug, Clone)]
10enum UnderlineOffset {
11    Auto,
12    Unit(i32),
13    Length(LengthUnit),
14}
15
16impl Display for UnderlineOffset {
17    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
18        match self {
19            Self::Auto => write!(f, "auto"),
20            Self::Unit(n) => write!(f, "{}", n),
21            Self::Length(n) => write!(f, "{}", n.get_class_arbitrary()),
22        }
23    }
24}
25
26impl Display for TailwindUnderlineOffset {
27    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
28        write!(f, "underline-offset-{}", self.kind)
29    }
30}
31
32impl TailwindInstance for TailwindUnderlineOffset {
33    fn attributes(&self, _: &TailwindBuilder) -> CssAttributes {
34        let offset = match self.kind {
35            UnderlineOffset::Auto => "auto".to_string(),
36            UnderlineOffset::Unit(n) => format!("{}px", n),
37            UnderlineOffset::Length(n) => n.get_properties(),
38        };
39        css_attributes! {
40            "text-underline-offset" => offset
41        }
42    }
43}
44
45impl TailwindUnderlineOffset {
46    /// https://tailwindcss.com/docs/text-underline-offset
47    pub fn parse(input: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
48        match input {
49            ["auto"] => Ok(Self { kind: UnderlineOffset::Auto }),
50            [] => Self::parse_arbitrary(arbitrary),
51            [n] => {
52                let a = TailwindArbitrary::from(*n);
53                Self::parse_arbitrary(&a)
54            },
55            _ => syntax_error!("Unknown opacity instructions: {}", input.join("-")),
56        }
57    }
58    /// https://tailwindcss.com/docs/text-underline-offset#arbitrary-values
59    pub fn parse_arbitrary(arbitrary: &TailwindArbitrary) -> Result<Self> {
60        Self::maybe_no_unit(arbitrary).or_else(|_| Self::maybe_length(arbitrary))
61    }
62    fn maybe_length(arbitrary: &TailwindArbitrary) -> Result<Self> {
63        let n = arbitrary.as_length_or_fraction()?;
64        Ok(Self { kind: UnderlineOffset::Length(n) })
65    }
66    fn maybe_no_unit(arbitrary: &TailwindArbitrary) -> Result<Self> {
67        let n = arbitrary.as_integer()?;
68        Ok(Self { kind: UnderlineOffset::Unit(n) })
69    }
70}