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, derive_more::Display, derive_more::Error)]
9pub enum SplitError {
10    End,
11    Overflow,
12}
13
14pub fn split_7bc<T: _7BitDecode + PrimInt>(buf: &[u8]) -> Result<(T, &[u8]), SplitError> {
15    let mut builder = T::build_from_7bit_codes();
16    let mut i = 0;
17    loop {
18        let &b = buf.get(i).ok_or(SplitError::End)?;
19        match builder(b) {
20            DecodeState::InProgress => (),
21            DecodeState::Done(res) => break Ok((res, &buf[i..])),
22            DecodeState::Overflow(_) => Err(SplitError::Overflow)?,
23        }
24        i += 1;
25    }
26}