typst_wasm_protocol/
lib.rs1pub use typst_wasm_macros::wasm_export;
2
3#[link(wasm_import_module = "typst_env")]
4unsafe extern "C" {
5 #[link_name = "wasm_minimal_protocol_send_result_to_host"]
6 unsafe fn __send_result_to_host(ptr: *const u8, len: usize);
7 #[link_name = "wasm_minimal_protocol_write_args_to_buffer"]
8 unsafe fn __write_args_to_buffer(ptr: *mut u8);
9}
10
11pub fn send_result_to_host(val: Vec<u8>) {
12 unsafe {
13 __send_result_to_host(val.as_ptr(), val.len());
14 }
15}
16
17pub fn write_args_to_buffer(ptr: *mut u8) {
18 unsafe {
19 __write_args_to_buffer(ptr);
20 }
21}
22
23pub trait PluginResult {
24 fn send_result(self) -> i32;
25}
26
27impl<T, E> PluginResult for Result<T, E>
28where
29 T: Into<Vec<u8>>,
30 E: ToString,
31{
32 fn send_result(self) -> i32 {
33 let (value, code) = match self {
34 Ok(value) => (value.into(), 0),
35 Err(err) => (err.to_string().into_bytes(), 1),
36 };
37 send_result_to_host(value);
38 code
39 }
40}
41
42impl PluginResult for &[u8] {
43 fn send_result(self) -> i32 {
44 send_result_to_host(self.to_vec());
45 0
46 }
47}
48
49impl PluginResult for Vec<u8> {
50 fn send_result(self) -> i32 {
51 send_result_to_host(self);
52 0
53 }
54}