use wasm_bindgen::prelude::*;
fn js_err(e: crate::Error) -> JsError {
JsError::new(&e.to_string())
}
#[wasm_bindgen(js_name = embedManifest)]
pub fn embed_manifest(zip: &[u8], store: &[u8]) -> Result<Vec<u8>, JsError> {
crate::embed_manifest(zip, store).map_err(js_err)
}
#[wasm_bindgen(js_name = readManifest)]
pub fn read_manifest(zip: &[u8]) -> Result<Option<Vec<u8>>, JsError> {
crate::read_manifest(zip).map_err(js_err)
}
#[wasm_bindgen(js_name = removeManifest)]
pub fn remove_manifest(zip: &[u8]) -> Result<Vec<u8>, JsError> {
crate::remove_manifest(zip).map_err(js_err)
}
#[wasm_bindgen(js_name = collectionMembers)]
pub fn collection_members(zip: &[u8]) -> Result<Vec<JsValue>, JsError> {
Ok(crate::collection_members(zip)
.map_err(js_err)?
.into_iter()
.map(|m| {
let out = js_sys::Object::new();
let _ = js_sys::Reflect::set(&out, &"name".into(), &m.name.as_str().into());
let _ = js_sys::Reflect::set(&out, &"start".into(), &(m.content.start as u32).into());
let _ = js_sys::Reflect::set(&out, &"length".into(), &(m.content.len() as u32).into());
out.into()
})
.collect())
}
#[wasm_bindgen(js_name = centralDirectoryRange)]
pub fn central_directory_range(zip: &[u8]) -> Result<JsValue, JsError> {
let r = crate::central_directory_range(zip).map_err(js_err)?;
let out = js_sys::Object::new();
let _ = js_sys::Reflect::set(&out, &"start".into(), &(r.start as u32).into());
let _ = js_sys::Reflect::set(&out, &"length".into(), &(r.len() as u32).into());
Ok(out.into())
}
#[wasm_bindgen(js_name = verify)]
pub fn verify(zip: &[u8]) -> Result<JsValue, JsError> {
let c = crate::verify(zip).map_err(js_err)?;
let out = js_sys::Object::new();
let _ = js_sys::Reflect::set(&out, &"hasManifest".into(), &c.has_manifest.into());
let _ = js_sys::Reflect::set(&out, &"manifestLen".into(), &(c.manifest_len as u32).into());
let _ = js_sys::Reflect::set(&out, &"isValidZip".into(), &c.is_valid_zip.into());
Ok(out.into())
}
#[wasm_bindgen(js_name = manifestPath)]
pub fn manifest_path() -> String {
crate::ZIP_MANIFEST_PATH.to_string()
}