web-sugars

It's a sugars for web related crates such as web-sys, js-sys, wasm-bindgens.

Targets of this crate

Non-targets

Example

Easy error handling for function of a #[wasm-bindgen], with easy common function calls:

```rust use web_sugars::prelude::*;

[wasm_bindgen]

pub fn mywasmfunc() -> Result // <- by wasm-bindgen spec. { // web-sugars functions are returns Result, // and WebSugarError has a From trait. Thus, you can type just ? 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::("https://example.com/mydata.json")?; // Fetch a binary data to JsValue of ArrayBuffer let _data: JsValue = fetchasarray-buffer("https://example.com/mydata.bin")?; // Get a Vec from JsValue of ArrayBuffer easily let data: Vec = _data.asvec()?;

// And more sugars may help you :) } ```

License

Author