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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use super::*;

mod traits;

/// Used to represent those attributes that only have integers
#[derive(Debug, Clone)]
pub enum NumericValue {
    Number { n: f32, negative: bool, can_be_negative: bool },
    Keyword(String),
    Arbitrary(TailwindArbitrary),
}

impl NumericValue {
    pub fn get_properties(&self, number: impl FnOnce(&f32) -> String) -> String {
        match self {
            Self::Number { n, .. } => number(n),
            Self::Keyword(s) => s.to_string(),
            Self::Arbitrary(s) => s.get_properties(),
        }
    }
    pub fn write_negative(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match *self {
            Self::Number { n, can_be_negative, .. } if can_be_negative && n < 0.0 => write!(f, "-"),
            _ => write!(f, ""),
        }
    }
    pub fn write_class(&self, f: &mut Formatter, before: &str) -> std::fmt::Result {
        write!(f, "{}{}", before, self)
    }
}

impl NumericValue {
    pub fn negative_parser(
        id: &'static str,
        checker: impl Fn(&str) -> bool,
    ) -> impl Fn(&[&str], &TailwindArbitrary, Negative) -> Result<Self> {
        move |pattern: &[&str], arbitrary: &TailwindArbitrary, negative: Negative| {
            let joined = pattern.join("-");
            match pattern {
                _ if checker(&joined) => Ok(Self::Keyword(joined)),
                [] => Self::parse_arbitrary(arbitrary),
                [n] => Self::parse_number(n, negative),
                _ => Err(TailwindError::syntax_error(format!("Unknown {} pattern", id))),
            }
        }
    }
    pub fn positive_parser(
        id: &'static str,
        checker: impl Fn(&str) -> bool,
    ) -> impl Fn(&[&str], &TailwindArbitrary) -> Result<Self> {
        move |pattern: &[&str], arbitrary: &TailwindArbitrary| {
            let joined = pattern.join("-");
            match pattern {
                _ if checker(&joined) => Ok(Self::Keyword(joined)),
                [] => Self::parse_arbitrary(arbitrary),
                [n] => {
                    let i = TailwindArbitrary::from(*n).as_integer()?;
                    Ok(Self::Number { n: i as f32, negative: false, can_be_negative: true })
                },
                _ => Err(TailwindError::syntax_error(format!("Unknown {} pattern", id))),
            }
        }
    }
    pub fn parse_arbitrary(arbitrary: &TailwindArbitrary) -> Result<Self> {
        Ok(Self::Arbitrary(TailwindArbitrary::new(arbitrary)?))
    }
    pub fn parse_number(n: &str, negative: Negative) -> Result<Self> {
        let mut n = TailwindArbitrary::from(n).as_float()?;
        if negative.0 {
            n = -n
        }
        Ok(Self::Number { n, negative: negative.0, can_be_negative: false })
    }
}