use crate::parser::ast::{self, Document, Element, PanelElement};
use super::build::{Category, classify};
use super::node::{FlowSource, KindTag, Node, NodeKind, TextSource};
use super::tree::Scene;
pub fn assert_scene_parity(doc: &Document, scene: &Scene) {
let mut expected: Vec<&Element> = Vec::new();
for child in &doc.page.children {
collect_expected(child, &mut expected);
}
let actual: Vec<&Node> = scene
.iter_tree_order()
.filter(|n| !matches!(n.kind(), NodeKind::Root))
.collect();
assert_eq!(
expected.len(),
actual.len(),
"AST has {} node-bearing elements; scene has {} non-root nodes.\n\
expected elements: {:?}\n\
actual kind tags: {:?}",
expected.len(),
actual.len(),
expected.iter().map(|e| element_tag(e)).collect::<Vec<_>>(),
actual.iter().map(|n| n.kind_tag()).collect::<Vec<_>>(),
);
for (i, (elem, node)) in expected.iter().zip(actual.iter()).enumerate() {
let expected_tag = expected_kind(elem);
assert_eq!(
node.kind_tag(),
expected_tag,
"kind mismatch at tree index {i}: AST element = {:?} (expects {:?}), \
scene node = {:?}",
element_tag(elem),
expected_tag,
node.kind_tag(),
);
assert_eq!(
node.aml_id(),
element_aml_id(elem).as_deref(),
"aml_id mismatch at tree index {i}: AST id = {:?}, scene aml_id = {:?}",
element_aml_id(elem),
node.aml_id(),
);
}
}
fn collect_expected<'a>(e: &'a Element, out: &mut Vec<&'a Element>) {
match classify(e) {
Category::NodeBearing => {
out.push(e);
for child in scene_children(e) {
collect_expected(child, out);
}
}
Category::Ancillary | Category::Inline => {
}
}
}
fn scene_children(e: &Element) -> Vec<&Element> {
match e {
Element::Text(t) => t
.children
.iter()
.filter(|c| match c {
Element::Text(_) => false,
other => classify(other) == Category::NodeBearing,
})
.collect(),
Element::Box(b) => nb(&b.children),
Element::Row(r) => nb(&r.children),
Element::Col(c) => nb(&c.children),
Element::Header(c) | Element::Body(c) | Element::Footer(c) => nb(&c.children),
Element::Thead(c) | Element::Tbody(c) | Element::Pagination(c) => nb(&c.children),
Element::Nav(n) => nb(&n.children),
Element::List(l) => nb(&l.children),
Element::Item(i) => nb(&i.children),
Element::Form(f) => nb(&f.children),
Element::Heading(h) => nb(&h.children),
Element::Link(l) => nb(&l.children),
Element::Select(s) => nb(&s.children),
Element::Table(t) => nb(&t.children),
Element::Tr(tr) => nb(&tr.children),
Element::Th(c) | Element::Td(c) => nb(&c.children),
Element::Animate(a) => nb(&a.children),
Element::Frame(f) => nb(&f.children),
Element::Live(l) => nb(&l.children),
Element::Panel(p) => {
nb(&p.children)
}
Element::State(s) => nb(&s.children),
Element::Details(d) => {
let mut v: Vec<&Element> = Vec::new();
v.extend(
d.summary_children
.iter()
.filter(|c| classify(c) == Category::NodeBearing),
);
if d.open {
v.extend(
d.children
.iter()
.filter(|c| classify(c) == Category::NodeBearing),
);
}
v
}
Element::Pre(_)
| Element::Hr(_)
| Element::Spacer(_)
| Element::Art(_)
| Element::Input(_)
| Element::Option(_)
| Element::Button(_)
| Element::ElementDef(_)
| Element::TextAnimate(_)
| Element::Tween(_)
| Element::On(_)
| Element::Include(_) => Vec::new(),
}
}
fn nb(elems: &[Element]) -> Vec<&Element> {
elems
.iter()
.filter(|c| classify(c) == Category::NodeBearing)
.collect()
}
fn expected_kind(e: &Element) -> KindTag {
match e {
Element::Box(b) => {
if b.x.is_some() || b.y.is_some() {
KindTag::Absolute
} else {
KindTag::Flow
}
}
Element::Row(_) => KindTag::Row,
Element::Col(_) => KindTag::Flow,
Element::Header(_)
| Element::Body(_)
| Element::Footer(_)
| Element::Nav(_)
| Element::Thead(_)
| Element::Tbody(_)
| Element::Pagination(_)
| Element::List(_)
| Element::Item(_)
| Element::Form(_)
| Element::State(_)
| Element::Details(_)
| Element::Frame(_) => KindTag::Flow,
Element::Hr(_) => KindTag::Hr,
Element::Spacer(_) => KindTag::Spacer,
Element::Text(_)
| Element::Pre(_)
| Element::Heading(_)
| Element::Art(_)
| Element::ElementDef(_)
| Element::TextAnimate(_) => KindTag::Text,
Element::Link(_) => KindTag::Link,
Element::Input(_) => KindTag::Input,
Element::Select(_) => KindTag::Select,
Element::Option(_) => KindTag::OptionLeaf,
Element::Button(_) => KindTag::Button,
Element::Table(_) => KindTag::Table,
Element::Tr(_) => KindTag::Tr,
Element::Th(_) => KindTag::Th,
Element::Td(_) => KindTag::Td,
Element::Animate(_) => KindTag::Animation,
Element::Live(_) => KindTag::LiveRegion,
Element::Panel(_) => KindTag::Panel,
Element::Tween(_) | Element::On(_) | Element::Include(_) => {
panic!("expected_kind called on ancillary element {e:?}")
}
}
}
fn element_tag(e: &Element) -> &'static str {
match e {
Element::Box(_) => "Box",
Element::Row(_) => "Row",
Element::Col(_) => "Col",
Element::Hr(_) => "Hr",
Element::Spacer(_) => "Spacer",
Element::Header(_) => "Header",
Element::Body(_) => "Body",
Element::Footer(_) => "Footer",
Element::Nav(_) => "Nav",
Element::Text(_) => "Text",
Element::Pre(_) => "Pre",
Element::Heading(_) => "Heading",
Element::List(_) => "List",
Element::Item(_) => "Item",
Element::Link(_) => "Link",
Element::Input(_) => "Input",
Element::Select(_) => "Select",
Element::Option(_) => "Option",
Element::Button(_) => "Button",
Element::Form(_) => "Form",
Element::Art(_) => "Art",
Element::Table(_) => "Table",
Element::Thead(_) => "Thead",
Element::Tbody(_) => "Tbody",
Element::Tr(_) => "Tr",
Element::Th(_) => "Th",
Element::Td(_) => "Td",
Element::Animate(_) => "Animate",
Element::Frame(_) => "Frame",
Element::ElementDef(_) => "ElementDef",
Element::Tween(_) => "Tween",
Element::TextAnimate(_) => "TextAnimate",
Element::Live(_) => "Live",
Element::Panel(_) => "Panel",
Element::State(_) => "State",
Element::Details(_) => "Details",
Element::Pagination(_) => "Pagination",
Element::On(_) => "On",
Element::Include(_) => "Include",
}
}
fn element_aml_id(e: &Element) -> Option<String> {
match e {
Element::Link(l) => l.id.clone(),
Element::Input(i) => i.id.clone(),
Element::Panel(p) => Some(p.id.clone()),
Element::Live(l) => Some(l.id.clone()),
Element::Animate(a) => Some(a.id.clone()),
Element::ElementDef(e) => Some(e.id.clone()),
Element::State(s) => Some(s.name.clone()),
_ => None,
}
}
#[allow(dead_code)]
const _KEEP: (
Option<PanelElement>,
Option<FlowSource>,
Option<TextSource>,
Option<ast::TransitionKind>,
) = (None, None, None, None);
#[cfg(test)]
mod tests {
use super::*;
use crate::parser;
use crate::scanner::Scanner;
use crate::compositor::scene::build;
fn parse(aml: &str) -> Document {
let mut scanner = Scanner::new(aml.as_bytes()).unwrap();
let tokens = scanner.scan_all().unwrap();
parser::parse(tokens).document.expect("parse failed")
}
#[test]
fn parity_holds_for_hello_world() {
let doc = parse(r#"[page mode=document][text]Hello[/text][/page]"#);
let scene = build::from_document(&doc);
assert_scene_parity(&doc, &scene);
}
#[test]
fn parity_holds_for_panel() {
let doc = parse(
r#"[page mode=document]
[panel id="p1" state="a"]
[state name="a"][text]A[/text][/state]
[state name="b"][text]B[/text][/state]
[/panel]
[/page]"#,
);
let scene = build::from_document(&doc);
assert_scene_parity(&doc, &scene);
let panel_id = scene.find_by_aml_id("p1").expect("p1 must be found");
let panel = scene.get(panel_id).unwrap();
if let NodeKind::Panel { active, states, .. } = panel.kind() {
let active_node = scene.get(*active).unwrap();
assert_eq!(active_node.aml_id(), Some("a"));
assert_eq!(states.len(), 2);
} else {
panic!("expected Panel kind");
}
}
#[test]
#[should_panic(expected = "AST has")]
fn parity_fails_on_count_mismatch() {
let doc = parse(r#"[page mode=document][text]Hi[/text][/page]"#);
let mut scene = build::from_document(&doc);
let root = scene.root();
let victim = scene.get(root).unwrap().children[0];
if let Some(root_node) = scene.nodes.get_mut(root) {
root_node.children.retain(|&c| c != victim);
}
scene.nodes.remove(victim);
assert_scene_parity(&doc, &scene);
}
#[test]
#[should_panic(expected = "kind mismatch")]
fn parity_fails_on_kind_mismatch() {
let doc = parse(r#"[page mode=document][text]Hi[/text][/page]"#);
let mut scene = build::from_document(&doc);
let root = scene.root();
let text_id = scene.get(root).unwrap().children[0];
if let Some(text_node) = scene.nodes.get_mut(text_id) {
text_node.kind = NodeKind::Row(crate::compositor::scene::node::RowData::default());
}
assert_scene_parity(&doc, &scene);
}
#[test]
fn parity_holds_with_inline_bold() {
let doc = parse(r#"[page mode=document][text]hi [text bold]world[/text][/text][/page]"#);
let scene = build::from_document(&doc);
assert_scene_parity(&doc, &scene);
let count = scene
.iter_tree_order()
.filter(|n| matches!(n.kind(), NodeKind::Text(_)))
.count();
assert_eq!(
count, 1,
"inline [b] should flatten to a run, not spawn a node"
);
let text_node = scene
.iter_tree_order()
.find(|n| matches!(n.kind(), NodeKind::Text(_)))
.unwrap();
if let NodeKind::Text(tc) = text_node.kind() {
assert_eq!(tc.runs.len(), 2, "expected 2 runs (plain + bold)");
assert!(tc.runs.iter().any(|r| r.bold), "one run must be bold");
}
}
#[test]
fn parity_holds_with_on_bindings() {
let doc = parse(
r#"[page mode=document]
[text]Hello[/text]
[on event="page-load" do="set" target="x" to="y"/]
[/page]"#,
);
let scene = build::from_document(&doc);
assert_scene_parity(&doc, &scene);
let count = scene
.iter_tree_order()
.filter(|n| !matches!(n.kind(), NodeKind::Root))
.count();
assert_eq!(count, 1, "only the [text] should produce a node");
}
}