tailwind_css_fixes/modules/flexbox/justify/justify_content/
mod.rs

1use super::*;
2
3#[doc=include_str!("readme.md")]
4#[derive(Debug, Clone)]
5pub struct TailwindJustifyContent {
6    kind: StandardValue,
7}
8
9crate::macros::sealed::keyword_instance!(TailwindJustifyContent => "justify-content", {
10    // Mapping keyword to the property value
11    "between" => "space-between",
12    "around" => "space-around",
13    "evenly" => "space-evenly",
14    "start" => "flex-start",
15    "left" => "flex-start",
16    "end" => "flex-end",
17    "right" => "flex-end",
18    "center-safe" => "safe center",
19    "end-safe" => "safe end",
20    // For any other valid keyword, return it as is.
21});
22
23impl Display for TailwindJustifyContent {
24    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
25        match &self.kind {
26            StandardValue::Keyword(s) => match s.as_str() {
27                // Match the mapped property keywords to their different tailwind names
28                "flex-start" => write!(f, "justify-start"),
29                "flex-end" => write!(f, "justify-end"),
30                "center" => write!(f, "justify-center"),
31                "space-between" => write!(f, "justify-between"),
32                "space-around" => write!(f, "justify-around"),
33                "space-evenly" => write!(f, "justify-evenly"),
34                "safe center" => write!(f, "justify-center-safe"),
35                "safe end" => write!(f, "justify-end-safe"),
36                // Other keywords like 'baseline' would simply be 'justify-baseline'
37                _ => write!(f, "justify-{}", s),
38            },
39            StandardValue::Arbitrary(s) => s.write_class(f, "justify-content-"),
40        }
41    }
42}
43
44impl TailwindJustifyContent {
45    /// <https://tailwindcss.com/docs/justify-content>
46    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
47        // 1. Parse the input as before.
48        let mut kind = StandardValue::parser("justify-content", &Self::check_valid)(pattern, arbitrary)?;
49
50        // 2. Then, if the result is a keyword, map it to its canonical form.
51        if let StandardValue::Keyword(s) = &kind {
52            kind = StandardValue::Keyword(Self::map_keyword(s).to_string());
53        }
54
55        Ok(Self { kind })
56    }
57    /// dispatch to [justify-content](https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content)
58    pub fn parse_arbitrary(arbitrary: &TailwindArbitrary) -> Result<Self> {
59        StandardValue::parse_arbitrary(arbitrary).map(|kind| Self { kind })
60    }
61    /// <https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content#syntax>
62    pub fn check_valid(mode: &str) -> bool {
63        let set = BTreeSet::from_iter(vec![
64            "start",
65            "end",
66            "center",
67            "between",
68            "around",
69            "evenly",
70            "stretch",
71            "baseline",
72            "normal",
73            "flex-start",       // alias for start
74            "flex-end",         // alias for end
75            "space-around",    // alias for around
76            "space-between",   // alias for between
77            "space-evenly",    // alias for evenly
78            "start",
79            "stretch",
80            "end-safe",
81            "center-safe",
82            // Extended syntax
83            "inherit",
84            "initial",
85            "left",
86            "revert",
87            "right",
88            "unset",
89        ]);
90        set.contains(mode)
91    }
92}