use napi::bindgen_prelude::BigInt;
use napi_derive::napi;
use crate::binary::BinaryStream;
use crate::types::VarLong;
#[napi]
pub struct ZigZong {}
#[napi]
impl ZigZong {
#[napi]
pub fn read(stream: &mut BinaryStream) -> BigInt {
let value = VarLong::read(stream);
let value = value.get_u64().1;
let value = ((value >> 1) as i64) ^ (-((value & 1) as i64));
let signed = value < 0;
let value = match signed {
true => (-value) as u64,
false => value as u64,
};
let value = napi::bindgen_prelude::BigInt {
sign_bit: signed,
words: vec![value],
};
value
}
#[napi]
pub fn write(stream: &mut BinaryStream, value: BigInt) {
let value = value.get_i64().0;
let value = ((value << 1) ^ (value >> 63)) as u64;
let value = napi::bindgen_prelude::BigInt {
sign_bit: false,
words: vec![value],
};
VarLong::write(stream, value);
}
}