tailwind_css_fixes/modules/typography/align/
mod.rs

1use super::*;
2
3#[doc=include_str!("readme.md")]
4#[derive(Debug, Clone)]
5pub struct TailwindAlign {
6    kind: VerticalAlign,
7}
8
9#[derive(Debug, Clone)]
10pub enum VerticalAlign {
11    Standard(String),
12    Length(LengthUnit),
13}
14
15impl<T> From<T> for TailwindAlign
16where
17    T: Into<String>,
18{
19    fn from(kind: T) -> Self {
20        Self { kind: VerticalAlign::Standard(kind.into()) }
21    }
22}
23
24impl Display for TailwindAlign {
25    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
26        match &self.kind {
27            VerticalAlign::Standard(s) => write!(f, "align-{}", s),
28            VerticalAlign::Length(s) => write!(f, "align-[{}]", s),
29        }
30    }
31}
32
33impl TailwindInstance for TailwindAlign {
34    fn attributes(&self, _: &TailwindBuilder) -> CssAttributes {
35        let align = match &self.kind {
36            VerticalAlign::Standard(s) => s.to_owned(),
37            VerticalAlign::Length(s) => s.get_properties(),
38        };
39        css_attributes! {
40            "vertical-align" => align
41        }
42    }
43}
44
45impl TailwindAlign {
46    /// https://tailwindcss.com/docs/text-align
47    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
48        match pattern {
49            [s] if Self::check_valid(s) => Ok(Self { kind: VerticalAlign::Standard(s.to_string()) }),
50            [s] => {
51                let n = TailwindArbitrary::from(*s).as_length_or_fraction()?;
52                Ok(Self { kind: VerticalAlign::Length(n) })
53            },
54            [] => Self::parse_arbitrary(arbitrary),
55            _ => syntax_error!("Unknown align instructions: {}", pattern.join("-")),
56        }
57    }
58    /// https://tailwindcss.com/docs/text-align
59    pub fn parse_arbitrary(arbitrary: &TailwindArbitrary) -> Result<Self> {
60        Ok(Self { kind: VerticalAlign::Length(arbitrary.as_length_or_fraction()?) })
61    }
62    /// https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align#syntax
63    pub fn check_valid(mode: &str) -> bool {
64        let set = BTreeSet::from_iter(vec![
65            "baseline",
66            "sub",
67            "super",
68            "text-top",
69            "text-bottom",
70            "middle",
71            "top",
72            "bottom",
73            "inherit",
74            "initial",
75            "revert",
76            "unset",
77        ]);
78        set.contains(mode)
79    }
80}