logo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use super::*;

#[derive(Clone, Debug)]
pub enum Aspect {
    Radio(usize, usize),
    Standard(String),
    Arbitrary(TailwindArbitrary),
}

impl Display for Aspect {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Radio(a, b) => write!(f, "{}/{}", a, b),
            Self::Standard(s) => write!(f, "{}", s),
            Self::Arbitrary(s) => s.write(f),
        }
    }
}

impl Aspect {
    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
        let out = match pattern {
            ["square"] => Self::Radio(1, 1),
            ["video"] => Self::Radio(16, 9),
            [s] if Self::check_valid(s) => Self::Standard(s.to_string()),
            [n] => {
                let (a, b) = TailwindArbitrary::from(*n).as_fraction()?;
                Self::Radio(a, b)
            },
            [] => Self::parse_arbitrary(arbitrary)?,
            _ => return syntax_error!("unknown aspect-ratio elements"),
        };
        Ok(out)
    }
    pub fn parse_arbitrary(arbitrary: &TailwindArbitrary) -> Result<Self> {
        Ok(Self::Arbitrary(TailwindArbitrary::new(arbitrary)?))
    }
    pub fn check_valid(mode: &str) -> bool {
        let set = BTreeSet::from_iter(vec!["auto", "inherit", "initial", "revert", "unset"]);
        set.contains(mode)
    }
    pub fn get_properties(&self) -> String {
        match self {
            Self::Radio(a, b) => format!("{}/{}", a, b),
            Self::Standard(s) => s.to_string(),
            Self::Arbitrary(s) => s.get_properties(),
        }
    }
}