use core2::io::{Cursor, Read};
use thiserror_no_std::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("variable-length uint overflow")]
VarUintOverflow,
#[error("unexpected end-of-buffer")]
UnexpectedEof,
}
pub fn read(cursor: &mut Cursor<&[u8]>) -> Result<u64, Error> {
let mut output = 0u128;
let mut buf = [0u8; 1];
loop {
cursor
.read_exact(&mut buf)
.map_err(|_| Error::UnexpectedEof)?;
let byte = buf[0];
output = (output << 7) | (byte & 0x7F) as u128;
if output > u64::MAX.into() {
return Ok(u64::MAX);
}
if (byte & 0x80) == 0 {
return Ok(output as u64);
}
}
}