Skip to main content

lichtung_plot/layout/
report_json.rs

1//! JSON projection of accepted geometry. Semantic references use the same
2//! portable values as native recipe declarations.
3
4use serde_json::{json, Value};
5
6use crate::{
7    layout::{GridCellReport, GridCellState, InkExtent, LayoutNode, LayoutReport, PanelSlot, Rect},
8    recipe::codec::{json_value, Codec},
9    Result,
10};
11
12fn encoded(value: &impl Codec) -> Value {
13    json_value(value.encode())
14}
15
16fn ink(extent: InkExtent) -> Value {
17    match extent {
18        InkExtent::Empty => json!({"kind":"empty"}),
19        InkExtent::Unknown => json!({"kind":"unknown"}),
20        InkExtent::Bounds(rect) => json!({"kind":"bounds","bounds":encoded(&rect)}),
21    }
22}
23
24fn rects(rects: &[Rect]) -> Vec<Value> {
25    rects.iter().map(encoded).collect()
26}
27
28fn node(node: &LayoutNode) -> Value {
29    json!({
30        "owner": encoded(node.owner()),
31        "role": encoded(&node.role()),
32        "allocated": encoded(&node.allocated()),
33        "measured": node.measured(),
34        "ink": ink(node.ink()),
35        "clips": rects(node.clips()),
36        "rect_ref": node.rect_ref().map(encoded),
37        "children": node.children().iter().map(self::node).collect::<Vec<_>>(),
38    })
39}
40
41fn slot(slot: &PanelSlot) -> Value {
42    json!({
43        "name": slot.name(),
44        "row": slot.row(),
45        "column": slot.column(),
46        "row_span": slot.row_span(),
47        "column_span": slot.column_span(),
48        "is_filled": slot.is_filled(),
49        "reservation": slot.reservation(),
50    })
51}
52
53fn cell(cell: &GridCellReport) -> Value {
54    let state = match cell.state() {
55        GridCellState::Empty => "empty",
56        GridCellState::Filled => "filled",
57        GridCellState::Reserved => "reserved",
58        GridCellState::Vacant => "vacant",
59        GridCellState::Covered => "covered",
60    };
61    json!({
62        "row": cell.row(),
63        "column": cell.column(),
64        "state": state,
65        "anchor": cell.anchor(),
66        "slot": cell.slot().map(slot),
67        "allocated": encoded(&cell.allocated()),
68    })
69}
70
71impl LayoutReport {
72    /// Serialize the actual accepted geometry as indented JSON. Rectangular
73    /// and paint references use the corresponding portable Recipe structure.
74    /// Unknown ink and definitely empty ink have distinct kinds.
75    pub fn to_json(&self) -> Result<String> {
76        crate::operation::checkpoint()?;
77        let [left, right, top, bottom] = self.root_padding();
78        let value = json!({
79            "viewport": encoded(&self.viewport()),
80            "root_padding": {"left":left,"right":right,"top":top,"bottom":bottom},
81            "nodes": self.nodes().iter().map(node).collect::<Vec<_>>(),
82            "grid_cells": self.grid_cells().iter().map(cell).collect::<Vec<_>>(),
83            "paint_order": self.paint_order().iter().map(|batch| json!({
84                "reference":encoded(batch.reference()),
85                "ink":ink(batch.ink()),
86                "clips":rects(batch.clips()),
87            })).collect::<Vec<_>>(),
88            "boundaries": self.boundaries().iter().map(encoded).collect::<Vec<_>>(),
89        });
90        crate::operation::checkpoint()?;
91        serde_json::to_string_pretty(&value)
92            .map_err(|error| crate::Error::Render(format!("layout report JSON: {error}").into()))
93    }
94}