| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use std::fmt::Write as _; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 33 |
#[non_exhaustive] |
| 34 |
pub enum Chrome { |
| 35 |
|
| 36 |
Bullet, |
| 37 |
|
| 38 |
|
| 39 |
Gutter, |
| 40 |
|
| 41 |
Fill, |
| 42 |
|
| 43 |
Edge, |
| 44 |
|
| 45 |
|
| 46 |
Shadow, |
| 47 |
|
| 48 |
Padding, |
| 49 |
|
| 50 |
|
| 51 |
Type, |
| 52 |
|
| 53 |
|
| 54 |
Pointing, |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
const ORDER: [(Chrome, &str); 8] = [ |
| 62 |
(Chrome::Bullet, "list-style: none"), |
| 63 |
(Chrome::Gutter, "margin: 0"), |
| 64 |
(Chrome::Fill, "background: none"), |
| 65 |
(Chrome::Edge, "border: none"), |
| 66 |
(Chrome::Shadow, "box-shadow: none"), |
| 67 |
(Chrome::Padding, "padding: 0"), |
| 68 |
(Chrome::Type, "font: inherit"), |
| 69 |
(Chrome::Pointing, "cursor: pointer"), |
| 70 |
]; |
| 71 |
|
| 72 |
const fn bit(chrome: Chrome) -> u8 { |
| 73 |
match chrome { |
| 74 |
Chrome::Bullet => 1 << 0, |
| 75 |
Chrome::Gutter => 1 << 1, |
| 76 |
Chrome::Fill => 1 << 2, |
| 77 |
Chrome::Edge => 1 << 3, |
| 78 |
Chrome::Shadow => 1 << 4, |
| 79 |
Chrome::Padding => 1 << 5, |
| 80 |
Chrome::Type => 1 << 6, |
| 81 |
Chrome::Pointing => 1 << 7, |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)] |
| 87 |
pub struct Reset(u8); |
| 88 |
|
| 89 |
impl Reset { |
| 90 |
|
| 91 |
|
| 92 |
pub const NOTHING: Self = Self(0); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
pub const BULLETS: Self = Self::NOTHING |
| 98 |
.and(Chrome::Bullet) |
| 99 |
.and(Chrome::Gutter) |
| 100 |
.and(Chrome::Padding); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
pub const FLAT_BUTTON: Self = Self::NOTHING |
| 111 |
.and(Chrome::Fill) |
| 112 |
.and(Chrome::Edge) |
| 113 |
.and(Chrome::Shadow); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
pub const TEXT_BUTTON: Self = Self::NOTHING |
| 133 |
.and(Chrome::Fill) |
| 134 |
.and(Chrome::Edge) |
| 135 |
.and(Chrome::Shadow) |
| 136 |
.and(Chrome::Padding) |
| 137 |
.and(Chrome::Type) |
| 138 |
.and(Chrome::Pointing); |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
#[must_use] |
| 143 |
pub const fn and(self, chrome: Chrome) -> Self { |
| 144 |
Self(self.0 | bit(chrome)) |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
#[must_use] |
| 149 |
pub const fn carries(self, chrome: Chrome) -> bool { |
| 150 |
self.0 & bit(chrome) != 0 |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
#[must_use] |
| 158 |
pub const fn is_empty(self) -> bool { |
| 159 |
self.0 == 0 |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
#[must_use] |
| 164 |
pub fn declarations(self) -> String { |
| 165 |
let mut css = String::new(); |
| 166 |
for (chrome, declaration) in ORDER { |
| 167 |
if self.carries(chrome) { |
| 168 |
let _ = writeln!(css, " {declaration};"); |
| 169 |
} |
| 170 |
} |
| 171 |
css |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
#[must_use] |
| 181 |
pub fn rule(self, selector: &str) -> String { |
| 182 |
if self.is_empty() { |
| 183 |
return String::new(); |
| 184 |
} |
| 185 |
format!("{selector} {{\n{}}}\n", self.declarations()) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
#[cfg(test)] |
| 190 |
mod tests { |
| 191 |
use super::*; |
| 192 |
|
| 193 |
#[test] |
| 194 |
fn nothing_emits_nothing() { |
| 195 |
assert!(Reset::NOTHING.is_empty()); |
| 196 |
assert_eq!(Reset::NOTHING.rule(".x"), ""); |
| 197 |
} |
| 198 |
|
| 199 |
#[test] |
| 200 |
fn the_selector_is_verbatim() { |
| 201 |
assert!( |
| 202 |
Reset::TEXT_BUTTON |
| 203 |
.rule("button.link") |
| 204 |
.starts_with("button.link {") |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
#[test] |
| 213 |
fn the_named_sets_emit_what_they_replaced() { |
| 214 |
assert_eq!( |
| 215 |
Reset::BULLETS.rule(".list"), |
| 216 |
".list {\n list-style: none;\n margin: 0;\n padding: 0;\n}\n" |
| 217 |
); |
| 218 |
assert_eq!( |
| 219 |
Reset::TEXT_BUTTON.rule("button.link"), |
| 220 |
"button.link {\n background: none;\n border: none;\n \ |
| 221 |
box-shadow: none;\n padding: 0;\n font: inherit;\n \ |
| 222 |
cursor: pointer;\n}\n" |
| 223 |
); |
| 224 |
assert_eq!( |
| 225 |
Reset::FLAT_BUTTON.rule(".facet-take"), |
| 226 |
".facet-take {\n background: none;\n border: none;\n \ |
| 227 |
box-shadow: none;\n}\n" |
| 228 |
); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
#[test] |
| 237 |
fn a_text_button_withdraws_everything_a_flat_one_does() { |
| 238 |
for (chrome, _) in ORDER { |
| 239 |
if Reset::FLAT_BUTTON.carries(chrome) { |
| 240 |
assert!(Reset::TEXT_BUTTON.carries(chrome), "{chrome:?}"); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
#[test] |
| 246 |
fn order_is_the_emitters() { |
| 247 |
let forwards = Reset::NOTHING.and(Chrome::Fill).and(Chrome::Bullet); |
| 248 |
let backwards = Reset::NOTHING.and(Chrome::Bullet).and(Chrome::Fill); |
| 249 |
assert_eq!(forwards, backwards); |
| 250 |
assert_eq!( |
| 251 |
forwards.declarations(), |
| 252 |
" list-style: none;\n background: none;\n" |
| 253 |
); |
| 254 |
} |
| 255 |
|
| 256 |
#[test] |
| 257 |
fn every_member_has_a_declaration() { |
| 258 |
for (chrome, _) in ORDER { |
| 259 |
assert!(Reset::NOTHING.and(chrome).carries(chrome), "{chrome:?}"); |
| 260 |
} |
| 261 |
|
| 262 |
let all = ORDER |
| 263 |
.iter() |
| 264 |
.fold(Reset::NOTHING, |set, (chrome, _)| set.and(*chrome)); |
| 265 |
assert_eq!(all.0, u8::MAX); |
| 266 |
assert_eq!(all.declarations().lines().count(), ORDER.len()); |
| 267 |
} |
| 268 |
} |
| 269 |
|