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