Skip to main content

logicaffeine_web/ui/
data_fetch.rs

1//! Runtime fetch of build-staged static data.
2//!
3//! `scripts/stage-web-data.sh` publishes the repo's data files (benchmark results,
4//! program sources, legal HTML, lexicon) under `/data/` next to the app bundle.
5//! Native builds compile the same bytes in via `include_str!`; these helpers are the
6//! wasm side of that split, so the data never rides inside the shipped binary.
7
8#[cfg(target_arch = "wasm32")]
9pub async fn fetch_static_text(path: &str) -> Result<String, String> {
10    let response = gloo_net::http::Request::get(path)
11        .send()
12        .await
13        .map_err(|e| format!("fetching {path}: {e}"))?;
14    if !response.ok() {
15        return Err(format!("fetching {path}: HTTP {}", response.status()));
16    }
17    response
18        .text()
19        .await
20        .map_err(|e| format!("reading {path}: {e}"))
21}
22
23#[cfg(target_arch = "wasm32")]
24pub async fn fetch_static_json<T: serde::de::DeserializeOwned>(path: &str) -> Result<T, String> {
25    let text = fetch_static_text(path).await?;
26    serde_json::from_str(&text).map_err(|e| format!("parsing {path}: {e}"))
27}