mirror of
https://github.com/encounter/objdiff.git
synced 2025-12-16 00:17:16 +00:00
Add experimental wasm bindings
Published to npm as objdiff-wasm
This commit is contained in:
53
objdiff-core/src/bindings/wasm.rs
Normal file
53
objdiff-core/src/bindings/wasm.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use anyhow::Context;
|
||||
use prost::Message;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
use crate::{bindings::diff::DiffResult, diff, obj};
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn run_diff(
|
||||
left: Option<Box<[u8]>>,
|
||||
right: Option<Box<[u8]>>,
|
||||
config: diff::DiffObjConfig,
|
||||
) -> Result<String, JsError> {
|
||||
let target = left
|
||||
.as_ref()
|
||||
.map(|data| obj::read::parse(data, &config).context("Loading target"))
|
||||
.transpose()
|
||||
.map_err(|e| JsError::new(&e.to_string()))?;
|
||||
let base = right
|
||||
.as_ref()
|
||||
.map(|data| obj::read::parse(data, &config).context("Loading base"))
|
||||
.transpose()
|
||||
.map_err(|e| JsError::new(&e.to_string()))?;
|
||||
let result = diff::diff_objs(&config, target.as_ref(), base.as_ref(), None)
|
||||
.map_err(|e| JsError::new(&e.to_string()))?;
|
||||
let left = target.as_ref().and_then(|o| result.left.as_ref().map(|d| (o, d)));
|
||||
let right = base.as_ref().and_then(|o| result.right.as_ref().map(|d| (o, d)));
|
||||
let out = DiffResult::new(left, right);
|
||||
serde_json::to_string(&out).map_err(|e| JsError::new(&e.to_string()))
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn run_diff_proto(
|
||||
left: Option<Box<[u8]>>,
|
||||
right: Option<Box<[u8]>>,
|
||||
config: diff::DiffObjConfig,
|
||||
) -> Result<Box<[u8]>, JsError> {
|
||||
let target = left
|
||||
.as_ref()
|
||||
.map(|data| obj::read::parse(data, &config).context("Loading target"))
|
||||
.transpose()
|
||||
.map_err(|e| JsError::new(&e.to_string()))?;
|
||||
let base = right
|
||||
.as_ref()
|
||||
.map(|data| obj::read::parse(data, &config).context("Loading base"))
|
||||
.transpose()
|
||||
.map_err(|e| JsError::new(&e.to_string()))?;
|
||||
let result = diff::diff_objs(&config, target.as_ref(), base.as_ref(), None)
|
||||
.map_err(|e| JsError::new(&e.to_string()))?;
|
||||
let left = target.as_ref().and_then(|o| result.left.as_ref().map(|d| (o, d)));
|
||||
let right = base.as_ref().and_then(|o| result.right.as_ref().map(|d| (o, d)));
|
||||
let out = DiffResult::new(left, right);
|
||||
Ok(out.encode_to_vec().into_boxed_slice())
|
||||
}
|
||||
Reference in New Issue
Block a user