use std::io::Cursor;
use wasm_bindgen::prelude::*;
use crate::book::{Book, Format};
#[wasm_bindgen(start)]
pub fn init() {
#[cfg(feature = "wasm")]
console_error_panic_hook::set_once();
}
#[wasm_bindgen]
pub fn epub_to_azw3(data: &[u8]) -> Result<Vec<u8>, JsValue> {
let mut book =
Book::from_bytes(data, Format::Epub).map_err(|e| JsValue::from_str(&e.to_string()))?;
let mut output = Cursor::new(Vec::new());
book.export(Format::Azw3, &mut output)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
Ok(output.into_inner())
}
#[wasm_bindgen]
pub fn epub_to_kfx(data: &[u8]) -> Result<Vec<u8>, JsValue> {
let mut book =
Book::from_bytes(data, Format::Epub).map_err(|e| JsValue::from_str(&e.to_string()))?;
let mut output = Cursor::new(Vec::new());
book.export(Format::Kfx, &mut output)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
Ok(output.into_inner())
}
#[wasm_bindgen]
pub fn azw3_to_epub(data: &[u8]) -> Result<Vec<u8>, JsValue> {
let mut book =
Book::from_bytes(data, Format::Azw3).map_err(|e| JsValue::from_str(&e.to_string()))?;
let mut output = Cursor::new(Vec::new());
book.export(Format::Epub, &mut output)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
Ok(output.into_inner())
}
#[wasm_bindgen]
pub fn kfx_to_epub(data: &[u8]) -> Result<Vec<u8>, JsValue> {
let mut book =
Book::from_bytes(data, Format::Kfx).map_err(|e| JsValue::from_str(&e.to_string()))?;
let mut output = Cursor::new(Vec::new());
book.export(Format::Epub, &mut output)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
Ok(output.into_inner())
}
#[wasm_bindgen]
pub fn mobi_to_epub(data: &[u8]) -> Result<Vec<u8>, JsValue> {
let mut book =
Book::from_bytes(data, Format::Mobi).map_err(|e| JsValue::from_str(&e.to_string()))?;
let mut output = Cursor::new(Vec::new());
book.export(Format::Epub, &mut output)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
Ok(output.into_inner())
}
#[wasm_bindgen]
pub fn mobi_to_azw3(data: &[u8]) -> Result<Vec<u8>, JsValue> {
let mut book =
Book::from_bytes(data, Format::Mobi).map_err(|e| JsValue::from_str(&e.to_string()))?;
let mut output = Cursor::new(Vec::new());
book.export(Format::Azw3, &mut output)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
Ok(output.into_inner())
}
#[wasm_bindgen]
pub fn epub_to_markdown(data: &[u8]) -> Result<Vec<u8>, JsValue> {
let mut book =
Book::from_bytes(data, Format::Epub).map_err(|e| JsValue::from_str(&e.to_string()))?;
let mut output = Cursor::new(Vec::new());
book.export(Format::Markdown, &mut output)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
Ok(output.into_inner())
}
#[wasm_bindgen]
pub fn azw3_to_markdown(data: &[u8]) -> Result<Vec<u8>, JsValue> {
let mut book =
Book::from_bytes(data, Format::Azw3).map_err(|e| JsValue::from_str(&e.to_string()))?;
let mut output = Cursor::new(Vec::new());
book.export(Format::Markdown, &mut output)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
Ok(output.into_inner())
}
#[wasm_bindgen]
pub fn kfx_to_markdown(data: &[u8]) -> Result<Vec<u8>, JsValue> {
let mut book =
Book::from_bytes(data, Format::Kfx).map_err(|e| JsValue::from_str(&e.to_string()))?;
let mut output = Cursor::new(Vec::new());
book.export(Format::Markdown, &mut output)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
Ok(output.into_inner())
}
#[wasm_bindgen]
pub fn mobi_to_markdown(data: &[u8]) -> Result<Vec<u8>, JsValue> {
let mut book =
Book::from_bytes(data, Format::Mobi).map_err(|e| JsValue::from_str(&e.to_string()))?;
let mut output = Cursor::new(Vec::new());
book.export(Format::Markdown, &mut output)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
Ok(output.into_inner())
}