It's a sugars for web related crates such as web-sys
, js-sys
, wasm-bindgen
s.
Easy error handling for function of a #[wasm-bindgen]
, with easy common function calls:
```rust use web_sugars::prelude::*;
pub fn mywasmfunc() -> Result?
to throw an error.
let window = getwindow()?;
// No need get_window()?.get_document()?
chains, just call get_document
.
let document = getdocument()?;
// No need .get_window()?.get_document()?.get_element_by_id()?
chains, just acll get_element_by_id
.
let element = getelementbyid("target-id")?;
let elements = getelementsbytagname("div")?;
let _xpathresults = evaluate("/html//p")?;
// Fetch a String data, all of errors are handled by WebSugarError
.
let data: String = fetchasstring("https://example.com/mydata.txt")?;
// Fetch a JSON
let data: JsValue = fetchasstring("https://example.com/mydata.json")?;
// Fetch a JSON, and then deserialize to MyType
with serde.
let data: MyType = fetchasjsonas::JsValue
of ArrayBuffer
let _data: JsValue = fetchasarray-buffer("https://example.com/mydata.bin")?;
// Get a Vec
// And more sugars may help you :) } ```