cargo fmt

This commit is contained in:
Toshit Chawda 2024-07-29 22:18:07 -07:00
parent 3d1b5b4c09
commit 9ad3faa331
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
4 changed files with 478 additions and 472 deletions

View file

@ -9,52 +9,52 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[wasm_bindgen]
pub fn init() {
panic::set_hook(Box::new(console_error_panic_hook::hook));
panic::set_hook(Box::new(console_error_panic_hook::hook));
}
fn create_encode_function(encode: Function) -> EncodeFn {
Box::new(move |str| {
encode
.call1(&JsValue::NULL, &str.into())
.unwrap()
.as_string()
.unwrap()
.to_string()
})
Box::new(move |str| {
encode
.call1(&JsValue::NULL, &str.into())
.unwrap()
.as_string()
.unwrap()
.to_string()
})
}
fn get_str(config: &Object, k: &str) -> String {
Reflect::get(config, &k.into())
.unwrap()
.as_string()
.unwrap()
Reflect::get(config, &k.into())
.unwrap()
.as_string()
.unwrap()
}
fn get_config(config: Object) -> Config {
Config {
prefix: get_str(&config, "prefix"),
encode: create_encode_function(Reflect::get(&config, &"encode".into()).unwrap().into()),
wrapfn: get_str(&config, "wrapfn"),
importfn: get_str(&config, "importfn"),
rewritefn: get_str(&config, "rewritefn"),
}
Config {
prefix: get_str(&config, "prefix"),
encode: create_encode_function(Reflect::get(&config, &"encode".into()).unwrap().into()),
wrapfn: get_str(&config, "wrapfn"),
importfn: get_str(&config, "importfn"),
rewritefn: get_str(&config, "rewritefn"),
}
}
#[wasm_bindgen]
pub fn rewrite_js(js: &str, url: &str, config: Object) -> Vec<u8> {
rewrite(js, Url::from_str(url).unwrap(), get_config(config))
rewrite(js, Url::from_str(url).unwrap(), get_config(config))
}
#[wasm_bindgen]
pub fn rewrite_js_from_arraybuffer(js: &[u8], url: &str, config: Object) -> Vec<u8> {
// we know that this is a valid utf-8 string
let js = unsafe { std::str::from_utf8_unchecked(js) };
// we know that this is a valid utf-8 string
let js = unsafe { std::str::from_utf8_unchecked(js) };
rewrite(js, Url::from_str(url).unwrap(), get_config(config))
rewrite(js, Url::from_str(url).unwrap(), get_config(config))
}