Skip to main content

host_contract/
host_contract.rs

1//! Host contract cookbook (std only).
2//!
3//! The crate paints. The **host** is the other half of the moat. This example
4//! is the legal shape of a sealed page — not a web framework.
5//!
6//! Allowed DOM (and nothing else for the data):
7//!
8//! ```html
9//! <div class="sealed-host" data-token="…" data-mode="sealed">
10//!   <img class="surface" alt="" src="/surface/{token}.png" />
11//!   <img class="tooltip" alt="" hidden />
12//! </div>
13//! ```
14//!
15//! HTTP:
16//! - `GET /surface/{id}.png` → `Content-Type: image/png` (`frame.png`)
17//! - `GET /surface/{id}/tooltip.png?x=&y=` → `image/png` or 204
18//! - `GET /surface/{id}/clipboard.tsv` → **403** when sealed; TSV only if Copyable
19//!
20//! Pointer JS may compute `(x, y)` from the mouse. It may **not** parse tooltip
21//! bodies as text. No `aria-label` of cell values. No JSON `{ "value": "49.00" }`.
22//!
23//! Scroll: call `Surface::metrics`, put `content_*` on a scrollbar, send the next
24//! `Viewport { origin_x, origin_y, width, height }` and re-render.
25//!
26//! ```text
27//! cargo run -p sealed-surface --example host_contract
28//! ```
29
30use std::fs;
31
32use sealed_surface::{
33    CellValue, Column, FrameRequest, Grid, LicenseLens, Pointer, Row, Surface, SurfaceMode,
34    Viewport,
35};
36
37fn main() -> Result<(), Box<dyn std::error::Error>> {
38    let mut rows = Vec::new();
39    for i in 0..80u32 {
40        rows.push(Row::new(
41            i,
42            vec![
43                CellValue::text(format!("ACME-SKU-{i}")),
44                CellValue::float(10.0 + f64::from(i), 2),
45            ],
46        ));
47    }
48    let grid = Grid::try_new(vec![Column::new(0, "sku"), Column::new(1, "price")], rows)?;
49
50    let surface = Surface::new(SurfaceMode::Sealed, LicenseLens::open());
51    let metrics = surface.metrics(&grid);
52
53    // Host scrollbar: window 480×160, content from metrics.
54    let window = Viewport {
55        origin_x: 0,
56        origin_y: 0,
57        width: 480,
58        height: 160,
59    };
60    println!(
61        "host scrollbar: content {}x{} window {}x{} max_origin_y={}",
62        metrics.content_width,
63        metrics.content_height,
64        window.width,
65        window.height,
66        metrics.content_height.saturating_sub(window.height)
67    );
68
69    let dir = std::env::temp_dir().join("sealed-surface-examples");
70    fs::create_dir_all(&dir)?;
71
72    for (name, origin_y) in [("y0", 0u32), ("y180", 180), ("y360", 360)] {
73        let mut req = FrameRequest::full(Viewport {
74            origin_x: 0,
75            origin_y,
76            width: window.width,
77            height: window.height,
78        });
79        req.pointer = Some(Pointer { x: 24, y: 40 });
80        let frame = surface.render(&grid, &req)?;
81        let path = dir.join(format!("host_{name}.png"));
82        fs::write(&path, &frame.png)?;
83        assert!(
84            frame.host.audit_text().is_empty(),
85            "sealed host leaked: {:?}",
86            frame.host.audit_text()
87        );
88        println!(
89            "{name}: rows {}..{} png={} tooltip={} file={}",
90            frame.visible_row_start,
91            frame.visible_row_end,
92            frame.png.len(),
93            frame.tooltip_png.as_ref().map_or(0, Vec::len),
94            path.display()
95        );
96    }
97
98    println!("forbidden: interpolating cell values, tooltip JSON, CSS blur over live rows");
99    println!("allowed:   <img alt=\"\"> + tooltip PNG + metrics-driven origin");
100    Ok(())
101}