wnfs_wasm/lib.rs
1#![allow(clippy::unused_unit)] // To prevent clippy screaming about wasm_bindgen macros.
2#![cfg(target_arch = "wasm32")] // This project only makes sense as a wasm32 target.
3use wasm_bindgen::prelude::wasm_bindgen;
4
5pub mod fs;
6
7//--------------------------------------------------------------------------------------------------
8// Utilities
9//--------------------------------------------------------------------------------------------------
10
11/// Panic hook lets us get better error messages if our Rust code ever panics.
12///
13/// This function needs to be called at least once during initialisation.
14/// https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/template-deep-dive/src-utils-rs.html#2-what-is-console_error_panic_hook
15#[wasm_bindgen(js_name = "setPanicHook")]
16pub fn set_panic_hook() {
17 #[cfg(feature = "console_error_panic_hook")]
18 console_error_panic_hook::set_once();
19}
20
21// For logging in the console.
22#[wasm_bindgen]
23extern "C" {
24 #[wasm_bindgen(js_namespace = console)]
25 pub(crate) fn log(s: &str);
26}
27
28//--------------------------------------------------------------------------------------------------
29// Macros
30//--------------------------------------------------------------------------------------------------
31
32#[macro_export]
33macro_rules! value {
34 ($value:expr) => {
35 wasm_bindgen::JsValue::from($value)
36 };
37}
38
39#[macro_export]
40macro_rules! console_log {
41 ($($t:tt)*) => ($crate::log(&format_args!($($t)*).to_string()))
42}