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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use std::fmt::Display;
use std::ops::Add;
use dioxus::prelude::*;
use dioxus::dioxus_core::AttributeValue;

/// Class struct is just a wrapper of `Vec<String>`
///
/// several From<> provided for easier construction
/// most of the time, you'll use class! proc_macro to
/// create instance.
///
/// Some Add<> helper also provided, so you can define
/// common part, and append extra values if needed.
///
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct Class(pub Vec<String>);

impl Display for Class {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.to_class())
    }
}

impl Class {
    pub const NONE: Class = Class(vec![]);

    pub fn to_class(&self) -> String {
        self.0.join(" ")
    }
    pub fn validate(&mut self) -> () {
        //TODO: validate the string format for css
        self.0.sort_unstable();
        self.0.dedup();
    }
    pub fn validated(&mut self) -> Self {
        self.validate();
        self.to_owned()
    }
    pub fn append(&mut self, v: &str) -> () {
        self.0.push(v.to_string());
    }
}

impl IntoAttributeValue for Class {
    fn into_value(self) -> AttributeValue {
        AttributeValue::Text(self.to_class())
    }
}

impl IntoAttributeValue for &Class {
    fn into_value(self) -> AttributeValue {
        AttributeValue::Text(self.to_class())
    }
}

impl From<Vec<&str>> for Class {
    fn from(v: Vec<&str>) -> Self {
        Self(v.iter().map(|x| x.to_string()).collect::<Vec<String>>())
    }
}

impl From<Vec<String>> for Class {
    fn from(v: Vec<String>) -> Self {
        Self(v)
    }
}

impl From<&[&str]> for Class {
    fn from(v: &[&str]) -> Self {
        Self(v.iter().map(|x| x.to_string()).collect::<Vec<String>>())
    }
}

impl From<&[String]> for Class {
    fn from(v: &[String]) -> Self {
        Self(v.to_vec())
    }
}

impl Add<&str> for Class {
    type Output = Self;

    fn add(self, rhs: &str) -> Self::Output {
        let mut result = self.clone();
        result.append(rhs);
        result.validated()
    }
}

impl Add<String> for Class {
    type Output = Self;

    fn add(self, rhs: String) -> Self::Output {
        let mut result = self.clone();
        result.append(&rhs);
        result.validated()
    }
}

impl Add<Vec<&str>> for Class {
    type Output = Self;

    fn add(self, rhs: Vec<&str>) -> Self::Output {
        let mut result = self.clone();
        for part in rhs {
            result.append(part);
        }
        result.validated()
    }
}

impl Add<Vec<String>> for Class {
    type Output = Self;

    fn add(self, rhs: Vec<String>) -> Self::Output {
        let mut result = self.clone();
        for part in rhs {
            result.append(&part);
        }
        result.validated()
    }
}

impl Add<Class> for Class {
    type Output = Self;

    fn add(self, rhs: Class) -> Self::Output {
        let mut result = self.clone();
        for part in rhs.0 {
            result.append(&part);
        }
        result.validated()
    }
}

impl Add<&Class> for Class {
    type Output = Self;

    fn add(self, rhs: &Class) -> Self::Output {
        let mut result = self.clone();
        for part in rhs.0.iter() {
            result.append(&part);
        }
        result.validated()
    }
}

impl Add<Option<&str>> for Class {
    type Output = Self;

    fn add(self, rhs: Option<&str>) -> Self::Output {
        match rhs {
            Some(rhs) => self + rhs,
            None => self,
        }
    }
}

impl Add<Option<String>> for Class {
    type Output = Self;

    fn add(self, rhs: Option<String>) -> Self::Output {
        match rhs {
            Some(rhs) => self + rhs,
            None => self,
        }
    }
}

impl Add<Option<Vec<&str>>> for Class {
    type Output = Self;

    fn add(self, rhs: Option<Vec<&str>>) -> Self::Output {
        match rhs {
            Some(rhs) => self + rhs,
            None => self,
        }
    }
}

impl Add<Option<Vec<String>>> for Class {
    type Output = Self;

    fn add(self, rhs: Option<Vec<String>>) -> Self::Output {
        match rhs {
            Some(rhs) => self + rhs,
            None => self,
        }
    }
}

impl Add<Option<Class>> for Class {
    type Output = Self;

    fn add(self, rhs: Option<Class>) -> Self::Output {
        match rhs {
            Some(rhs) => self + rhs,
            None => self,
        }
    }
}

impl Add<Option<&Class>> for Class {
    type Output = Self;

    fn add(self, rhs: Option<&Class>) -> Self::Output {
        match rhs {
            Some(rhs) => self + rhs,
            None => self,
        }
    }
}

impl Add<&Option<&Class>> for Class {
    type Output = Self;

    #[allow(suspicious_double_ref_op)]
    fn add(self, rhs: &Option<&Class>) -> Self::Output {
        match rhs {
            Some(rhs) => self + rhs.clone(),
            None => self,
        }
    }
}