Skip to main content

pointblitz_web/
lib.rs

1//! PointBlitz browser target (wasm): the same `pointblitz-core` on a canvas (P2, decisions 0028, 0029).
2//!
3//! Browser-only code is compiled for wasm32 alone; the host build keeps the crate checkable.
4//! Built with `web/build.sh` (cargo → wasm32 → `wasm-bindgen --target web`), loaded by `web/`.
5//!
6//! JS drives the loop: it hands chunks to [`Viewer::insert`], sets the camera, and calls
7//! [`Viewer::render`] from `requestAnimationFrame` only when something changed (decision 0027).
8//! [`Viewer::gpu_done`] resolves when the GPU has finished the frames submitted so far — the
9//! `presented` mark (decision 0020).
10
11#[cfg(target_arch = "wasm32")]
12mod viewer;
13#[cfg(target_arch = "wasm32")]
14pub use viewer::Viewer;
15
16// Used by the wasm viewer; on the host only its tests use it.
17#[cfg_attr(not(target_arch = "wasm32"), allow(dead_code))]
18mod oneshot;
19
20#[cfg_attr(not(target_arch = "wasm32"), allow(dead_code))]
21fn json_str(s: &str) -> String {
22    let mut out = String::with_capacity(s.len() + 2);
23    out.push('"');
24    for c in s.chars() {
25        match c {
26            '"' => out.push_str("\\\""),
27            '\\' => out.push_str("\\\\"),
28            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
29            c => out.push(c),
30        }
31    }
32    out.push('"');
33    out
34}
35
36#[cfg(test)]
37mod tests {
38    #[test]
39    fn json_strings_are_escaped() {
40        assert_eq!(super::json_str("a\"b\\c\n"), "\"a\\\"b\\\\c\\u000a\"");
41    }
42}