lichtung-plot 0.3.0

Strongly typed scientific plotting with deterministic SVG and PNG output
Documentation
//! JSON projection of accepted geometry. Semantic references use the same
//! portable values as native recipe declarations.

use serde_json::{json, Value};

use crate::{
    layout::{GridCellReport, GridCellState, InkExtent, LayoutNode, LayoutReport, PanelSlot, Rect},
    recipe::codec::{json_value, Codec},
    Result,
};

fn encoded(value: &impl Codec) -> Value {
    json_value(value.encode())
}

fn ink(extent: InkExtent) -> Value {
    match extent {
        InkExtent::Empty => json!({"kind":"empty"}),
        InkExtent::Unknown => json!({"kind":"unknown"}),
        InkExtent::Bounds(rect) => json!({"kind":"bounds","bounds":encoded(&rect)}),
    }
}

fn rects(rects: &[Rect]) -> Vec<Value> {
    rects.iter().map(encoded).collect()
}

fn node(node: &LayoutNode) -> Value {
    json!({
        "owner": encoded(node.owner()),
        "role": encoded(&node.role()),
        "allocated": encoded(&node.allocated()),
        "measured": node.measured(),
        "ink": ink(node.ink()),
        "clips": rects(node.clips()),
        "rect_ref": node.rect_ref().map(encoded),
        "children": node.children().iter().map(self::node).collect::<Vec<_>>(),
    })
}

fn slot(slot: &PanelSlot) -> Value {
    json!({
        "name": slot.name(),
        "row": slot.row(),
        "column": slot.column(),
        "row_span": slot.row_span(),
        "column_span": slot.column_span(),
        "is_filled": slot.is_filled(),
        "reservation": slot.reservation(),
    })
}

fn cell(cell: &GridCellReport) -> Value {
    let state = match cell.state() {
        GridCellState::Empty => "empty",
        GridCellState::Filled => "filled",
        GridCellState::Reserved => "reserved",
        GridCellState::Vacant => "vacant",
        GridCellState::Covered => "covered",
    };
    json!({
        "row": cell.row(),
        "column": cell.column(),
        "state": state,
        "anchor": cell.anchor(),
        "slot": cell.slot().map(slot),
        "allocated": encoded(&cell.allocated()),
    })
}

impl LayoutReport {
    /// Serialize the actual accepted geometry as indented JSON. Rectangular
    /// and paint references use the corresponding portable Recipe structure.
    /// Unknown ink and definitely empty ink have distinct kinds.
    pub fn to_json(&self) -> Result<String> {
        crate::operation::checkpoint()?;
        let [left, right, top, bottom] = self.root_padding();
        let value = json!({
            "viewport": encoded(&self.viewport()),
            "root_padding": {"left":left,"right":right,"top":top,"bottom":bottom},
            "nodes": self.nodes().iter().map(node).collect::<Vec<_>>(),
            "grid_cells": self.grid_cells().iter().map(cell).collect::<Vec<_>>(),
            "paint_order": self.paint_order().iter().map(|batch| json!({
                "reference":encoded(batch.reference()),
                "ink":ink(batch.ink()),
                "clips":rects(batch.clips()),
            })).collect::<Vec<_>>(),
            "boundaries": self.boundaries().iter().map(encoded).collect::<Vec<_>>(),
        });
        crate::operation::checkpoint()?;
        serde_json::to_string_pretty(&value)
            .map_err(|error| crate::Error::Render(format!("layout report JSON: {error}").into()))
    }
}