vertigo/css/
tailwind_class.rs

1use std::{
2    borrow::Cow,
3    ops::{Add, AddAssign},
4};
5
6use crate::AttrValue;
7
8/// This represents a tailwind class. Use [tw!](crate::tw!) macro to create one.
9pub struct TwClass<'a>(Cow<'a, str>);
10
11impl<'a> TwClass<'a> {
12    pub fn new(value: impl Into<Cow<'a, str>>) -> Self {
13        Self(value.into())
14    }
15
16    pub fn join<'b>(&self, value: &TwClass<'b>) -> Self {
17        let new_str: String = [self.0.as_ref(), value.0.as_ref()].join(" ");
18        Self(Cow::Owned(new_str))
19    }
20
21    // This is intentionally not implemented as ToString to allow usage only in "tw=" attribute in `dom!` macro.
22    pub fn to_class_value(&self) -> String {
23        self.0.to_string()
24    }
25}
26
27impl<'a> From<&'static str> for TwClass<'a> {
28    fn from(value: &'static str) -> Self {
29        Self::new(value)
30    }
31}
32
33impl<'a> From<TwClass<'a>> for AttrValue {
34    fn from(value: TwClass<'a>) -> Self {
35        value.0.to_string().into()
36    }
37}
38
39impl<'a> Add for TwClass<'a> {
40    type Output = Self;
41
42    fn add(self, rhs: Self) -> Self::Output {
43        self.join(&rhs)
44    }
45}
46
47impl<'a> AddAssign for TwClass<'a> {
48    fn add_assign(&mut self, other: Self) {
49        *self = self.join(&other);
50    }
51}