Skip to main content

rust_pptx_wasm/
lib.rs

1use rust_pptx::{detect_format, parse_presentation as parse_core_presentation, ParseLimits};
2use serde::Serialize;
3use serde_wasm_bindgen::Serializer;
4use wasm_bindgen::prelude::*;
5
6#[wasm_bindgen]
7pub fn detect_presentation_format(bytes: &[u8]) -> Result<String, JsValue> {
8    detect_format(bytes)
9        .map(|format| {
10            match format {
11                rust_pptx::PresentationFormat::Pptx => "pptx",
12                rust_pptx::PresentationFormat::Ppt => "ppt",
13            }
14            .into()
15        })
16        .map_err(|error| JsValue::from_str(&error.to_string()))
17}
18
19#[wasm_bindgen]
20pub fn parse_presentation(bytes: &[u8]) -> Result<JsValue, JsValue> {
21    let document = parse_core_presentation(bytes, &ParseLimits::default())
22        .map_err(|error| JsValue::from_str(&error.to_string()))?;
23
24    // The TypeScript model exposes keyed collections as `Record<string, T>` rather than
25    // `Map<string, T>`. Keep byte buffers as Uint8Array and optional values as undefined,
26    // while making every Rust map cross the Wasm boundary as a plain JavaScript object.
27    document
28        .serialize(&Serializer::new().serialize_maps_as_objects(true))
29        .map_err(|error| JsValue::from_str(&error.to_string()))
30}