tailwind_css_fixes/systems/instruction/arbitrary/
mod.rs

1use super::*;
2use std::fmt::Write;
3
4mod methods;
5
6#[derive(Debug, Clone)]
7pub struct TailwindArbitrary {
8    inner: Box<str>,
9}
10
11impl Display for TailwindArbitrary {
12    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
13        f.write_char('[')?;
14        for c in self.inner.chars() {
15            match c {
16                ' ' => f.write_char('_')?,
17                _ => f.write_char(c)?,
18            }
19        }
20        f.write_char(']')
21    }
22}
23
24impl From<&str> for TailwindArbitrary {
25    fn from(s: &str) -> Self {
26        Self { inner: Box::from(s) }
27    }
28}
29
30impl From<&Self> for TailwindArbitrary {
31    fn from(s: &Self) -> Self {
32        Self { inner: s.inner.clone() }
33    }
34}
35
36impl TailwindArbitrary {
37    pub fn new<T>(s: T) -> Result<Self>
38    where
39        T: Into<Self>,
40    {
41        let out = s.into();
42        if cfg!(feature = "compile_time") {
43            if out.inner.is_empty() {
44                return Err(TailwindError::syntax_error("Arbitrary value cannot be empty"));
45            }
46            // TODO: Check unbalanced quotes
47            if out.inner.contains('\n') {
48                return Err(TailwindError::syntax_error("Arbitrary value does balance quotes"));
49            }
50        }
51        Ok(out)
52    }
53
54    pub fn get_class(&self) -> String {
55        let mut class = String::with_capacity(self.inner.len() + 2);
56        class.push('[');
57        for c in self.inner.chars() {
58            match c {
59                ' ' => class.push('_'),
60                _ => class.push(c),
61            }
62        }
63        class.push(']');
64        class
65    }
66    pub fn write(&self, f: &mut Formatter) -> std::fmt::Result {
67        self.write_class(f, "")
68    }
69    pub fn write_class(&self, f: &mut Formatter, before: &str) -> std::fmt::Result {
70        write!(f, "{}{}", before, self.get_class())
71    }
72    pub fn get_properties(&self) -> String {
73        self.inner.to_string()
74    }
75}