Skip to main content

dotnet_io_binary/slice/
_7bit_code.rs

1use num_traits::PrimInt;
2
3use crate::_7bit_code::{
4    _7BitDecode,
5    DecodeState,
6};
7
8#[derive(Debug, thiserror::Error)]
9pub enum SplitError {
10    #[error("unexpected end of input")]
11    End,
12    #[error("7-bit encoded integer overflow")]
13    Overflow,
14}
15
16pub fn split_7bc<T: _7BitDecode + PrimInt>(buf: &[u8]) -> Result<(T, &[u8]), SplitError> {
17    let mut builder = T::build_from_7bit_codes();
18    let mut i = 0;
19    loop {
20        let &b = buf.get(i).ok_or(SplitError::End)?;
21        match builder(b) {
22            DecodeState::InProgress => (),
23            DecodeState::Done(res) => break Ok((res, &buf[i..])),
24            DecodeState::Overflow(_) => Err(SplitError::Overflow)?,
25        }
26        i += 1;
27    }
28}