#![cfg(target_arch = "wasm32")]
use {goods::*, std::collections::HashMap, wasm_bindgen::prelude::*};
#[derive(Clone, Debug, serde::Deserialize, PartialEq, Eq)]
struct Object {
key: Kind,
#[serde(flatten)]
rest: HashMap<String, String>,
}
#[derive(Clone, Copy, Debug, serde::Deserialize, PartialEq, Eq)]
enum Kind {
#[serde(rename = "json")]
Json,
#[serde(rename = "yaml")]
Yaml,
}
impl SimpleAsset for Object {}
impl<K> AssetDefaultFormat<K> for Object {
type DefaultFormat = JsonFormat;
}
#[wasm_bindgen]
pub async fn run() {
console_log::init_with_level(log::Level::Trace).unwrap_throw();
log::trace!("Running");
let registry = Registry::builder()
.with(FetchSource::new())
.build();
let cache = Cache::new(registry, WasmBindgen);
let object_json: Handle<Object> = cache.load("asset.json".to_string());
let object_yaml: Handle<Object> = cache.load_with_format("asset.yaml".to_string(), YamlFormat);
log::info!("From json: {:#?}", object_json.clone().await.unwrap_throw());
log::info!("From yaml: {:#?}", object_yaml.clone().await.unwrap_throw());
let document = web_sys::window().unwrap_throw().document().unwrap_throw();
let div_json = document.create_element("div").unwrap_throw();
div_json.set_inner_html(&format!("{:#?}", object_json.get().unwrap_throw()));
document
.body()
.unwrap_throw()
.append_child(&div_json)
.unwrap_throw();
let div_yaml = document.create_element("div").unwrap_throw();
div_yaml.set_inner_html(&format!("{:#?}", object_yaml.get().unwrap_throw()));
document
.body()
.unwrap_throw()
.append_child(&div_yaml)
.unwrap_throw();
}