use std::io::{Read, Write};
use crate::error::{BinverseResult, BinverseError};
pub const MAX_LEN: usize = 10;
pub fn read<R: Read>(r: &mut R) -> BinverseResult<u64> {
let mut x: u64 = 0;
let mut s = 0;
let mut b = [0_u8; 1];
for i in 0..MAX_LEN {
r.read_exact(&mut b)?;
let b = b[0];
if b < 0x80 {
if i == MAX_LEN-1 && b > 1 {
return Err(BinverseError::VarIntOverflow)
}
return Ok(x | (b as u64) << s)
}
x |= ((b&0x7f) as u64) << s;
s += 7;
}
Err(BinverseError::VarIntOverflow)
}
pub fn write<W: Write>(mut x: u64, w: &mut W) -> Result<(), BinverseError> {
while x >= 0x80 {
w.write_all(&[x as u8 | 0x80])?;
x >>= 7;
}
w.write_all(&[x as u8])?;
Ok(())
}