| 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 |
use crate::screen::{ |
| 56 |
Act, Cell, Cells, Choice, Column, Field, Figure, Meter, Node, Prose, RegionKind, Row, Slot, Tag, |
| 57 |
}; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 65 |
#[non_exhaustive] |
| 66 |
pub enum Containment { |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
Text, |
| 74 |
|
| 75 |
Inlines, |
| 76 |
|
| 77 |
Blocks, |
| 78 |
|
| 79 |
Collection(Of), |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
Opaque, |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 107 |
#[non_exhaustive] |
| 108 |
pub enum Of { |
| 109 |
|
| 110 |
Rows, |
| 111 |
|
| 112 |
TableRows, |
| 113 |
|
| 114 |
Cells, |
| 115 |
|
| 116 |
Fields, |
| 117 |
|
| 118 |
Figures, |
| 119 |
|
| 120 |
Choices, |
| 121 |
} |
| 122 |
|
| 123 |
impl Of { |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
#[must_use] |
| 130 |
pub fn element_containment(self) -> Containment { |
| 131 |
match self { |
| 132 |
|
| 133 |
Self::Rows | Self::TableRows | Self::Cells => Containment::Inlines, |
| 134 |
|
| 135 |
|
| 136 |
Self::Fields => Containment::Collection(Of::Choices), |
| 137 |
Self::Figures | Self::Choices => Containment::Text, |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 150 |
pub enum Level { |
| 151 |
|
| 152 |
Leaf, |
| 153 |
|
| 154 |
Run, |
| 155 |
|
| 156 |
Block, |
| 157 |
} |
| 158 |
|
| 159 |
impl Containment { |
| 160 |
|
| 161 |
#[must_use] |
| 162 |
pub fn level(self) -> Option<Level> { |
| 163 |
match self { |
| 164 |
Self::Text => Some(Level::Leaf), |
| 165 |
Self::Inlines => Some(Level::Run), |
| 166 |
Self::Blocks | Self::Collection(_) => Some(Level::Block), |
| 167 |
Self::Opaque => None, |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
pub trait Element { |
| 181 |
|
| 182 |
|
| 183 |
fn containment(&self) -> Containment { |
| 184 |
Containment::Text |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
impl Element for Node { |
| 189 |
fn containment(&self) -> Containment { |
| 190 |
match self { |
| 191 |
|
| 192 |
|
| 193 |
Self::Heading { .. } | Self::Text { .. } | Self::Rich { .. } | Self::Token(_) => { |
| 194 |
Containment::Text |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
Self::Link { .. } => Containment::Text, |
| 199 |
|
| 200 |
Self::Figure(_) => Containment::Text, |
| 201 |
|
| 202 |
Self::Act(_) => Containment::Text, |
| 203 |
|
| 204 |
|
| 205 |
Self::Notice { .. } => Containment::Text, |
| 206 |
|
| 207 |
Self::StandIn { .. } => Containment::Inlines, |
| 208 |
|
| 209 |
|
| 210 |
Self::Field(field) => field.containment(), |
| 211 |
Self::Form { .. } => Containment::Collection(Of::Fields), |
| 212 |
Self::List { .. } => Containment::Collection(Of::Rows), |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
Self::Table { .. } => Containment::Collection(Of::TableRows), |
| 217 |
Self::Select { .. } => Containment::Collection(Of::Choices), |
| 218 |
Self::Meter(meter) => meter.containment(), |
| 219 |
Self::Stats { .. } => Containment::Collection(Of::Figures), |
| 220 |
Self::Region(slot) => slot.containment(), |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
impl Element for Slot { |
| 226 |
fn containment(&self) -> Containment { |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
if matches!(self.kind, RegionKind::Bespoke { .. }) { |
| 234 |
Containment::Opaque |
| 235 |
} else { |
| 236 |
Containment::Blocks |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
impl Element for Row { |
| 242 |
fn containment(&self) -> Containment { |
| 243 |
Containment::Inlines |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
impl Element for Cells { |
| 248 |
fn containment(&self) -> Containment { |
| 249 |
Containment::Collection(Of::Cells) |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
impl Element for Cell { |
| 254 |
fn containment(&self) -> Containment { |
| 255 |
Containment::Inlines |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
impl Element for Field { |
| 260 |
fn containment(&self) -> Containment { |
| 261 |
if self.kind.offers_options() { |
| 262 |
Containment::Collection(Of::Choices) |
| 263 |
} else { |
| 264 |
Containment::Text |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
impl Element for Act {} |
| 273 |
impl Element for Tag {} |
| 274 |
impl Element for Figure {} |
| 275 |
impl Element for Meter {} |
| 276 |
impl Element for Choice {} |
| 277 |
impl Element for Column {} |
| 278 |
impl Element for Prose {} |
| 279 |
|
| 280 |
#[cfg(test)] |
| 281 |
mod tests { |
| 282 |
use super::*; |
| 283 |
use crate::layout; |
| 284 |
use crate::screen::Action; |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
fn every_node() -> Vec<Node> { |
| 293 |
vec![ |
| 294 |
Node::page("t"), |
| 295 |
Node::text("t"), |
| 296 |
Node::rich("*t*"), |
| 297 |
Node::Act(Act::new("go", Action::get("/"))), |
| 298 |
Node::Token(Tag::badge("tag")), |
| 299 |
Node::Notice { |
| 300 |
kind: layout::Notice::Banner, |
| 301 |
tone: layout::Tone::Neutral, |
| 302 |
text: "t".into(), |
| 303 |
}, |
| 304 |
Node::StandIn { |
| 305 |
state: layout::Readiness::Empty, |
| 306 |
message: "nothing yet".into(), |
| 307 |
act: None, |
| 308 |
}, |
| 309 |
Node::Field(Box::new(Field::new( |
| 310 |
layout::FieldKind::Text, |
| 311 |
"name", |
| 312 |
"Name", |
| 313 |
))), |
| 314 |
Node::Form { |
| 315 |
action: Action::post("/"), |
| 316 |
submit: "Save".into(), |
| 317 |
fields: Vec::new(), |
| 318 |
}, |
| 319 |
Node::List { |
| 320 |
rows: Vec::new(), |
| 321 |
more: None, |
| 322 |
}, |
| 323 |
Node::Table { |
| 324 |
columns: Vec::new(), |
| 325 |
rows: Vec::new(), |
| 326 |
}, |
| 327 |
Node::Select { |
| 328 |
kind: layout::Selector::Tabs, |
| 329 |
options: Vec::new(), |
| 330 |
chosen: None, |
| 331 |
action: None, |
| 332 |
}, |
| 333 |
Node::Meter(Meter::new(1, 2)), |
| 334 |
Node::Stats { |
| 335 |
figures: Vec::new(), |
| 336 |
}, |
| 337 |
Node::Region(Slot::new("r", RegionKind::Pane)), |
| 338 |
] |
| 339 |
} |
| 340 |
|
| 341 |
#[test] |
| 342 |
fn the_containment_ladder_only_goes_down() { |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
for node in every_node() { |
| 348 |
if node.containment() != Containment::Text { |
| 349 |
continue; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
assert_eq!(node.containment().level(), Some(Level::Leaf)); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
for of in [ |
| 359 |
Of::Rows, |
| 360 |
Of::TableRows, |
| 361 |
Of::Cells, |
| 362 |
Of::Fields, |
| 363 |
Of::Figures, |
| 364 |
Of::Choices, |
| 365 |
] { |
| 366 |
let inner = of.element_containment(); |
| 367 |
|
| 368 |
|
| 369 |
if let Containment::Collection(nested) = inner { |
| 370 |
assert_eq!( |
| 371 |
nested.element_containment(), |
| 372 |
Containment::Text, |
| 373 |
"{of:?} nests {nested:?}, which must terminate in text" |
| 374 |
); |
| 375 |
assert_ne!(nested, of, "{of:?} holds itself"); |
| 376 |
continue; |
| 377 |
} |
| 378 |
assert!( |
| 379 |
matches!(inner, Containment::Text | Containment::Inlines), |
| 380 |
"{of:?} holds {inner:?}, which is not below a collection" |
| 381 |
); |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
let region = Node::Region(Slot::new("r", RegionKind::Pane)); |
| 386 |
assert_eq!(region.containment(), Containment::Blocks); |
| 387 |
} |
| 388 |
|
| 389 |
#[test] |
| 390 |
fn a_run_holds_no_blocks() { |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
for run in [Row::new("r").containment(), Cell::new("c").containment()] { |
| 396 |
assert_eq!(run, Containment::Inlines); |
| 397 |
assert_eq!(run.level(), Some(Level::Run)); |
| 398 |
assert!(run.level() < Containment::Blocks.level()); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
#[test] |
| 403 |
fn most_of_the_vocabulary_declares_nothing_and_is_text() { |
| 404 |
|
| 405 |
assert_eq!(Tag::badge("t").containment(), Containment::Text); |
| 406 |
assert_eq!( |
| 407 |
Act::new("go", Action::get("/")).containment(), |
| 408 |
Containment::Text |
| 409 |
); |
| 410 |
assert_eq!(Figure::new("7", "tasks").containment(), Containment::Text); |
| 411 |
assert_eq!(Meter::new(1, 2).containment(), Containment::Text); |
| 412 |
} |
| 413 |
|
| 414 |
#[test] |
| 415 |
fn a_field_holds_its_options_only_when_it_has_any() { |
| 416 |
|
| 417 |
|
| 418 |
let plain = Field::new(layout::FieldKind::Text, "name", "Name"); |
| 419 |
assert_eq!(plain.containment(), Containment::Text); |
| 420 |
|
| 421 |
let choosing = Field::select("priority", "Priority", vec![Choice::plain("high")]); |
| 422 |
assert_eq!(choosing.containment(), Containment::Collection(Of::Choices)); |
| 423 |
assert!(choosing.kind.offers_options()); |
| 424 |
} |
| 425 |
|
| 426 |
#[test] |
| 427 |
fn a_region_holds_leaves_as_well_as_blocks() { |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
let pane = Slot::new("facts", RegionKind::Pane) |
| 434 |
.with(Node::section("Facts")) |
| 435 |
.with(Node::text("Two files")) |
| 436 |
.with(Node::Token(Tag::badge("beta"))); |
| 437 |
|
| 438 |
assert_eq!(pane.containment(), Containment::Blocks); |
| 439 |
for node in &pane.body { |
| 440 |
assert!(node.containment().level() <= pane.containment().level()); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
#[test] |
| 445 |
fn a_bespoke_region_is_opaque_rather_than_an_exception() { |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
let bespoke = Node::Region(Slot::bespoke("canvas", "editor")); |
| 450 |
assert_eq!(bespoke.containment(), Containment::Opaque); |
| 451 |
|
| 452 |
assert_eq!(bespoke.containment().level(), None); |
| 453 |
} |
| 454 |
} |
| 455 |
|