docx_handlebars/
lib.rs

1use wasm_bindgen::prelude::*;
2use wasm_bindgen::JsValue;
3
4pub mod template;
5pub mod utils;
6pub mod errors;
7
8// 重新导出常用的类型和函数
9pub use errors::DocxError;
10pub use template::render_handlebars;
11
12/// 当 `console_error_panic_hook` 功能启用时,我们可以调用 `set_panic_hook` 函数
13/// 至少一次在初始化过程中,以便在 panic 时获得更好的错误消息。
14pub fn set_panic_hook() {
15    #[cfg(feature = "console_error_panic_hook")]
16    console_error_panic_hook::set_once();
17}
18
19/// 主要的 DOCX Handlebars 处理器
20#[wasm_bindgen]
21pub fn render(
22    zip_bytes: Vec<u8>,
23    data_json: &str,
24) -> Result<JsValue, JsValue> {
25    let data: serde_json::Value = serde_json::from_str(data_json)
26            .map_err(|e| JsValue::from_str(&format!("JSON 解析错误: {}", e)))?;
27
28    // 调用模板渲染函数
29    let result = template::render_handlebars(zip_bytes, &data)
30        .map_err(|e| JsValue::from_str(&e.to_string()))?;
31
32    // 返回结果
33    Ok(JsValue::from(result))
34}