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
75
use super::*;
use std::fmt::Write;

mod methods;

#[derive(Debug, Clone)]
pub struct TailwindArbitrary {
    inner: Box<str>,
}

impl Display for TailwindArbitrary {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_char('[')?;
        for c in self.inner.chars() {
            match c {
                ' ' => f.write_char('_')?,
                _ => f.write_char(c)?,
            }
        }
        f.write_char(']')
    }
}

impl From<&str> for TailwindArbitrary {
    fn from(s: &str) -> Self {
        Self { inner: Box::from(s) }
    }
}

impl From<&Self> for TailwindArbitrary {
    fn from(s: &Self) -> Self {
        Self { inner: s.inner.clone() }
    }
}

impl TailwindArbitrary {
    pub fn new<T>(s: T) -> Result<Self>
    where
        T: Into<Self>,
    {
        let out = s.into();
        if cfg!(compile_time) {
            if out.inner.is_empty() {
                return Err(TailwindError::syntax_error("Arbitrary value cannot be empty"));
            }
            // TODO: Check unbalanced quotes
            if out.inner.contains('\n') {
                return Err(TailwindError::syntax_error("Arbitrary value does balance quotes"));
            }
        }
        Ok(out)
    }

    pub fn get_class(&self) -> String {
        let mut class = String::with_capacity(self.inner.len() + 2);
        class.push('[');
        for c in self.inner.chars() {
            match c {
                ' ' => class.push('_'),
                _ => class.push(c),
            }
        }
        class.push(']');
        class
    }
    pub fn write(&self, f: &mut Formatter) -> std::fmt::Result {
        self.write_class(f, "")
    }
    pub fn write_class(&self, f: &mut Formatter, before: &str) -> std::fmt::Result {
        write!(f, "{}{}", before, self.get_class())
    }
    pub fn get_properties(&self) -> String {
        self.inner.to_string()
    }
}