Skip to main content

cotis_modules_debug/
layout_module_debugger.rs

1//! Capture and replay layout-module output for debugging.
2//!
3//! - [`store_computed_layout`] / [`load_computed_layout`] — JSON file I/O for layout output.
4//! - [`capture::LayoutModuleCapture`] — wraps a [`LayoutManager`](cotis::layout::LayoutManager)
5//!   and records per-frame output (stage 1).
6//! - [`isolated_replay::LayoutModuleReplay`] — replays a stored declaration through a layout
7//!   module without [`CotisApp`](cotis::cotis_app::CotisApp) (alternative to stage-2 `UiFn`).
8
9use std::path::Path;
10
11use serde::Serialize;
12use serde::de::DeserializeOwned;
13
14pub mod capture;
15
16pub mod isolated_replay;
17
18/// Writes a vector of layout outputs to a JSON file.
19///
20/// Typically used after a record pass to persist
21/// [`LayoutModuleCapture::output`](capture::LayoutModuleCapture::output).
22///
23/// # Errors
24///
25/// Returns an error if JSON serialization or file writing fails.
26pub fn store_computed_layout<ElementsOutput: Clone + Serialize>(
27    res: &Vec<ElementsOutput>,
28    file: &Path,
29) -> Result<(), Box<dyn std::error::Error>> {
30    let bytes = serde_json::to_vec(res)?;
31    std::fs::write(file, bytes)?;
32    Ok(())
33}
34
35/// Loads a vector of layout outputs from a JSON file.
36///
37/// Used in stage 3 (render-only) to skip the layout manager and feed precomputed output
38/// directly into a render pipe.
39///
40/// # Errors
41///
42/// Returns an error if file reading or JSON deserialization fails.
43pub fn load_computed_layout<ElementsOutput: Clone + DeserializeOwned>(
44    file: &Path,
45) -> Result<Vec<ElementsOutput>, Box<dyn std::error::Error>> {
46    let bytes = std::fs::read(file)?;
47    let serialized: Vec<ElementsOutput> = serde_json::from_slice(&bytes)?;
48    Ok(serialized)
49}