| 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 |
#![forbid(unsafe_code)] |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
pub trait Intent { |
| 47 |
|
| 48 |
fn token(self) -> &'static str; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 58 |
pub enum Bevel { |
| 59 |
|
| 60 |
Raised, |
| 61 |
|
| 62 |
|
| 63 |
Inset, |
| 64 |
} |
| 65 |
|
| 66 |
impl Bevel { |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
#[must_use] |
| 72 |
pub const fn edges(self) -> (Edge, Edge) { |
| 73 |
match self { |
| 74 |
Self::Raised => (Edge::Light, Edge::Dark), |
| 75 |
Self::Inset => (Edge::Dark, Edge::Light), |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#[must_use] |
| 85 |
pub const fn pressed(self) -> Self { |
| 86 |
match self { |
| 87 |
Self::Raised => Self::Inset, |
| 88 |
Self::Inset => Self::Raised, |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 95 |
pub enum Edge { |
| 96 |
|
| 97 |
Light, |
| 98 |
|
| 99 |
Dark, |
| 100 |
} |
| 101 |
|
| 102 |
impl Intent for Edge { |
| 103 |
fn token(self) -> &'static str { |
| 104 |
match self { |
| 105 |
Self::Light => "bevel-light", |
| 106 |
Self::Dark => "bevel-dark", |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 113 |
pub enum Fill { |
| 114 |
|
| 115 |
Page, |
| 116 |
|
| 117 |
Raised, |
| 118 |
|
| 119 |
Overlay, |
| 120 |
|
| 121 |
Well, |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
impl Intent for Fill { |
| 135 |
fn token(self) -> &'static str { |
| 136 |
match self { |
| 137 |
Self::Page => "surface-page", |
| 138 |
Self::Raised => "surface-raised", |
| 139 |
Self::Overlay => "surface-overlay", |
| 140 |
Self::Well => "surface-well", |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 154 |
pub enum Depth { |
| 155 |
|
| 156 |
Flat, |
| 157 |
|
| 158 |
Raised, |
| 159 |
|
| 160 |
|
| 161 |
Well, |
| 162 |
} |
| 163 |
|
| 164 |
impl Depth { |
| 165 |
|
| 166 |
#[must_use] |
| 167 |
pub const fn bevel(self) -> Option<Bevel> { |
| 168 |
match self { |
| 169 |
Self::Flat => None, |
| 170 |
Self::Raised => Some(Bevel::Raised), |
| 171 |
Self::Well => Some(Bevel::Inset), |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
#[must_use] |
| 180 |
pub const fn fill(self) -> Option<Fill> { |
| 181 |
match self { |
| 182 |
Self::Flat => None, |
| 183 |
Self::Raised => Some(Fill::Raised), |
| 184 |
Self::Well => Some(Fill::Well), |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
#[must_use] |
| 190 |
pub const fn pressed(self) -> Self { |
| 191 |
match self { |
| 192 |
Self::Raised => Self::Well, |
| 193 |
other => other, |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
#[cfg(test)] |
| 199 |
mod tests { |
| 200 |
use super::*; |
| 201 |
|
| 202 |
#[test] |
| 203 |
fn inset_is_raised_with_the_light_moved() { |
| 204 |
let (rl, rd) = Bevel::Raised.edges(); |
| 205 |
let (il, id) = Bevel::Inset.edges(); |
| 206 |
assert_eq!((rl, rd), (Edge::Light, Edge::Dark)); |
| 207 |
assert_eq!((il, id), (rd, rl)); |
| 208 |
} |
| 209 |
|
| 210 |
#[test] |
| 211 |
fn pressing_twice_is_a_no_op() { |
| 212 |
for b in [Bevel::Raised, Bevel::Inset] { |
| 213 |
assert_eq!(b.pressed().pressed(), b); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
#[test] |
| 218 |
fn a_raised_region_is_never_filled_with_a_recessed_surface() { |
| 219 |
|
| 220 |
assert_eq!(Depth::Raised.fill(), Some(Fill::Raised)); |
| 221 |
assert_eq!(Depth::Raised.bevel(), Some(Bevel::Raised)); |
| 222 |
assert_eq!(Depth::Well.bevel(), Some(Bevel::Inset)); |
| 223 |
assert_ne!(Depth::Well.fill(), Depth::Raised.fill()); |
| 224 |
} |
| 225 |
|
| 226 |
#[test] |
| 227 |
fn flat_has_neither_edge_nor_fill() { |
| 228 |
assert_eq!(Depth::Flat.bevel(), None); |
| 229 |
assert_eq!(Depth::Flat.fill(), None); |
| 230 |
} |
| 231 |
|
| 232 |
#[test] |
| 233 |
fn pressing_a_card_makes_a_well() { |
| 234 |
assert_eq!(Depth::Raised.pressed(), Depth::Well); |
| 235 |
assert_eq!( |
| 236 |
Depth::Raised.pressed().bevel(), |
| 237 |
Depth::Raised.bevel().map(Bevel::pressed) |
| 238 |
); |
| 239 |
|
| 240 |
assert_eq!(Depth::Flat.pressed(), Depth::Flat); |
| 241 |
assert_eq!(Depth::Well.pressed(), Depth::Well); |
| 242 |
} |
| 243 |
|
| 244 |
#[test] |
| 245 |
fn intents_name_makeover_tokens_and_nothing_else() { |
| 246 |
assert_eq!(Edge::Light.token(), "bevel-light"); |
| 247 |
assert_eq!(Edge::Dark.token(), "bevel-dark"); |
| 248 |
assert_eq!(Fill::Raised.token(), "surface-raised"); |
| 249 |
assert_eq!(Fill::Well.token(), "surface-well"); |
| 250 |
|
| 251 |
for t in [Edge::Light.token(), Edge::Dark.token()] { |
| 252 |
assert!(!t.starts_with('#'), "{t} looks like a value"); |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|