generated from napi-rs/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
32 lines (28 loc) · 772 Bytes
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// #![deny(clippy::all)]
use napi::bindgen_prelude::Buffer;
use napi_derive::napi;
use rmpv::encode::write_value;
use rmpv::{decode::read_value, Value as ValueR};
use serde_json::Value;
use serde_json::{from_slice, to_vec};
#[cfg(all(
any(windows, unix),
target_arch = "x86_64",
not(target_env = "musl"),
not(debug_assertions)
))]
#[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
#[napi]
pub fn encode(input: Value) -> Buffer {
let mut wr = vec![];
let val = ValueR::Binary(to_vec(&input).unwrap());
write_value(&mut wr, &val).unwrap();
Buffer::from(wr)
}
#[napi]
pub fn decode(input: Buffer) -> Value {
let buf = read_value(&mut &input[..]).unwrap();
let output = from_slice(buf.as_slice().unwrap()).unwrap();
output
}