docx_handlebars/
lib.rs

1#[cfg(target_arch = "wasm32")]
2use wasm_bindgen::prelude::*;
3#[cfg(target_arch = "wasm32")]
4use wasm_bindgen::JsValue;
5
6pub mod errors;
7pub mod imagesize;
8mod template;
9pub mod utils;
10
11pub use errors::DocxError;
12
13// WASM 平台:导出 WASM 兼容的渲染函数
14#[cfg(target_arch = "wasm32")]
15#[wasm_bindgen]
16pub fn render_template(
17    zip_bytes: Vec<u8>,
18    data_json: &str,
19) -> Result<JsValue, JsValue> {
20    let data: serde_json::Value = serde_json::from_str(data_json)
21            .map_err(|e| JsValue::from_str(&format!("JSON Parse Error: {e}")))?;
22
23    // 调用模板渲染函数
24    let result = template::render_template(zip_bytes, &data)
25        .map_err(|e| JsValue::from_str(&e.to_string()))?;
26
27    // 返回结果
28    Ok(JsValue::from(result))
29}
30
31// 非 WASM 平台:直接导出原生 Rust 函数
32#[cfg(not(target_arch = "wasm32"))]
33pub use template::render_template;