use napi_derive::napi;
use crate::binary::BinaryStream;
use crate::types::Uint8;
#[napi]
pub struct VarInt {}
#[napi]
impl VarInt {
#[napi]
pub fn read(stream: &mut BinaryStream) -> u32 {
let mut value = 0;
let mut size = 0;
loop {
let byte = Uint8::read(stream);
value |= (byte as u32 & 0x7F) << (size * 7);
size += 1;
if size > 5 {
println!("VarInt is too big");
return 0;
}
if (byte & 0x80) != 0x80 {
break;
}
}
return value;
}
#[napi]
pub fn write(stream: &mut BinaryStream, value: u32) {
let mut value = value;
loop {
let mut byte = (value & 0x7F) as u8;
value >>= 7;
if value != 0 {
byte |= 0x80;
}
Uint8::write(stream, byte);
if value == 0 {
break;
}
}
}
}