/
traits.rs
152 lines (142 loc) · 3.23 KB
/
traits.rs
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
use crate::utils::normalize_class_list_string;
/// A trait which designates toggle types.
///
/// [`bool`], [`Option`], [`Result`], and [`Fn`] types for them are supported.
///
/// When implementing it yourself, it should return a [`bool`] which is the truthy version of the value.
///
/// - `Some(x)` -> `true`
/// - `Some(true)` -> `true`
/// - `Some(false)` -> `false`
/// - `None` -> `false`
pub trait ClassToggle {
/// From [`ClassToggle`].
///
/// Converts the type into a boolean.
///
/// - `Some(x)` -> `true`
/// - `Some(true)` -> `true`
/// - `Some(false)` -> `false`
/// - `None` -> `false`
fn to_class_toggle(&self) -> bool;
}
impl<T, F> ClassToggle for F
where
F: Fn() -> T,
T: ClassToggle,
{
fn to_class_toggle(&self) -> bool {
self().to_class_toggle()
}
}
impl<T, E> ClassToggle for Result<T, E>
where
T: ClassToggle,
{
fn to_class_toggle(&self) -> bool {
if let Ok(value) = self {
value.to_class_toggle()
} else {
false
}
}
}
impl<T> ClassToggle for Option<T>
where
T: ClassToggle,
{
fn to_class_toggle(&self) -> bool {
if let Some(value) = self {
value.to_class_toggle()
} else {
false
}
}
}
impl ClassToggle for bool {
fn to_class_toggle(&self) -> bool {
*self
}
}
/// A trait which designates classlistable types.
///
/// [`str`], [`String`], [`Option`], [`Result`], and [`Fn`] types for them are supported.
///
/// When implementing it yourself, it should return a trimmed string of the class names separated by only one space.
///
/// - ✅ `"class"`
/// - ✅ `"class list"`
/// - ❌ `" class list "`
/// - ❌ `"class list"`
pub trait ClassList {
/// From [`ClassList`].
///
/// Converts the type into a class list string.
///
/// If `normalize` is `true`, it'll replace duplicate spaces and trim the string.
///
/// `normalize` being an option is mostly for internal optimization of macro-generated code.
///
/// `" class list "` -> `"class list"`
fn to_class_list(&self, normalize: bool) -> String;
}
impl<T, F> ClassList for F
where
F: Fn() -> T,
T: ClassList,
{
fn to_class_list(&self, normalize: bool) -> String {
self().to_class_list(normalize)
}
}
impl<T, E> ClassList for Result<T, E>
where
T: ClassList,
{
fn to_class_list(&self, normalize: bool) -> String {
if let Ok(value) = self {
value.to_class_list(normalize)
} else {
String::default()
}
}
}
impl<T> ClassList for Option<T>
where
T: ClassList,
{
fn to_class_list(&self, normalize: bool) -> String {
if let Some(value) = self {
value.to_class_list(normalize)
} else {
String::default()
}
}
}
impl ClassList for String {
fn to_class_list(&self, normalize: bool) -> String {
if normalize {
normalize_class_list_string(self.clone())
} else {
self.clone()
}
}
}
impl ClassList for &str {
fn to_class_list(&self, normalize: bool) -> String {
if normalize {
normalize_class_list_string(self.to_string())
} else {
self.to_string()
}
}
}
impl ClassList for str {
fn to_class_list(&self, normalize: bool) -> String {
if normalize {
normalize_class_list_string(self.to_string())
} else {
self.to_string()
}
}
}