Skip to main content

adam_web_ui/
diagnostics.rs

1//! Cross-platform diagnostic reporting.
2//!
3//! `eprintln!` reaches a visible stderr on desktop, but `wasm32-unknown-unknown`'s stdio is
4//! a no-op sink — nothing written to it is ever observable, in a devtools console or
5//! otherwise. [`report_error`] is the single point every diagnostic raised through this
6//! crate's UI should go through instead, so a failure is visible on every platform a
7//! consumer of `adam-web-ui` ships on.
8//!
9//! Gated on `target_arch = "wasm32"` rather than just `feature = "desktop"`: the web-only
10//! code paths this feeds are also built and unit-tested on the native host (`cargo test -p
11//! adam-web-ui`), and calling the real `web_sys`/`wasm_bindgen` JS FFI outside an actual
12//! wasm32 host crashes the process rather than erroring — `eprintln!` is the correct, safe
13//! fallback for that native-host case too. A consumer embedding this crate's components into
14//! its own desktop (webview) build should enable this crate's `desktop` feature so its
15//! diagnostics land on that process's stderr instead of being silently dropped.
16
17/// Reports `msg` on the platform's error channel: the process's stderr on desktop, or when
18/// built for any non-wasm32 host (e.g. running this crate's own test suite natively).
19#[cfg(any(feature = "desktop", not(target_arch = "wasm32")))]
20pub fn report_error(msg: &str) {
21    eprintln!("{msg}");
22}
23
24/// Reports `msg` on the platform's error channel: the browser's console, for a genuine
25/// wasm32 web build.
26#[cfg(all(not(feature = "desktop"), target_arch = "wasm32"))]
27pub fn report_error(msg: &str) {
28    web_sys::console::error_1(&wasm_bindgen::JsValue::from_str(msg));
29}