clippy + refactor out config parsing

This commit is contained in:
Toshit Chawda 2024-07-29 22:17:44 -07:00
parent aa218770c7
commit 3d1b5b4c09
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
2 changed files with 22 additions and 56 deletions

View file

@ -29,31 +29,26 @@ fn create_encode_function(encode: Function) -> EncodeFn {
})
}
fn get_str(config: &Object, k: &str) -> String {
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"),
}
}
#[wasm_bindgen]
pub fn rewrite_js(js: &str, url: &str, config: Object) -> Vec<u8> {
rewrite(
js,
Url::from_str(url).unwrap(),
Config {
prefix: Reflect::get(&config, &"prefix".into())
.unwrap()
.as_string()
.unwrap(),
encode: create_encode_function(Reflect::get(&config, &"encode".into()).unwrap().into()),
wrapfn: Reflect::get(&config, &"wrapfn".into())
.unwrap()
.as_string()
.unwrap(),
importfn: Reflect::get(&config, &"importfn".into())
.unwrap()
.as_string()
.unwrap(),
rewritefn: Reflect::get(&config, &"rewritefn".into())
.unwrap()
.as_string()
.unwrap(),
},
)
rewrite(js, Url::from_str(url).unwrap(), get_config(config))
}
#[wasm_bindgen]
@ -61,27 +56,5 @@ pub fn rewrite_js_from_arraybuffer(js: &[u8], url: &str, config: Object) -> Vec<
// 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(),
Config {
prefix: Reflect::get(&config, &"prefix".into())
.unwrap()
.as_string()
.unwrap(),
encode: create_encode_function(Reflect::get(&config, &"encode".into()).unwrap().into()),
wrapfn: Reflect::get(&config, &"wrapfn".into())
.unwrap()
.as_string()
.unwrap(),
importfn: Reflect::get(&config, &"importfn".into())
.unwrap()
.as_string()
.unwrap(),
rewritefn: Reflect::get(&config, &"rewritefn".into())
.unwrap()
.as_string()
.unwrap(),
},
)
rewrite(js, Url::from_str(url).unwrap(), get_config(config))
}