#![warn(missing_docs)]
use std::error::Error;
use std::fmt;
use std::fmt::Display;
pub use std::string::FromUtf8Error;
pub use bitstream_reader_derive::{BitRead, BitReadSized};
pub use buffer::BitBuffer;
pub use endianness::*;
pub use read::{BitRead, BitReadSized, LazyBitRead, LazyBitReadSized};
pub use stream::BitStream;
mod buffer;
mod endianness;
mod is_signed;
mod read;
mod stream;
mod unchecked_primitive;
#[derive(Debug)]
pub enum ReadError {
TooManyBits {
requested: usize,
max: usize,
},
NotEnoughData {
requested: usize,
bits_left: usize,
},
IndexOutOfBounds {
pos: usize,
size: usize,
},
UnmatchedDiscriminant {
discriminant: usize,
enum_name: String,
},
Utf8Error(FromUtf8Error),
}
impl Display for ReadError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ReadError::TooManyBits { requested, max } =>
write!(f, "Too many bits requested to fit in the requested data type, requested to read {} bits while only {} fit in the datatype", requested, max),
ReadError::NotEnoughData { requested, bits_left } =>
write!(f, "Not enough data in the buffer to read all requested bits, requested to read {} bits while only {} bits are left", requested, bits_left),
ReadError::IndexOutOfBounds { pos, size } =>
write!(f, "The requested position is outside the bounds of the stream, requested position {} while the stream or buffer is only {} bits long", pos, size),
ReadError::UnmatchedDiscriminant { discriminant, enum_name } =>
write!(f, "Unmatched discriminant '{}' found while trying to read enum '{}'", discriminant, enum_name),
ReadError::Utf8Error(err) => err.fmt(f)
}
}
}
impl From<FromUtf8Error> for ReadError {
fn from(err: FromUtf8Error) -> ReadError {
ReadError::Utf8Error(err)
}
}
impl Error for ReadError {
fn cause(&self) -> Option<&dyn Error> {
match self {
ReadError::Utf8Error(err) => Some(err),
_ => None,
}
}
}
pub type Result<T> = std::result::Result<T, ReadError>;
#[inline(always)]
pub fn bit_size_of<T: BitRead<LittleEndian>>() -> Option<usize> {
T::bit_size()
}
#[inline(always)]
pub fn bit_size_of_sized<T: BitReadSized<LittleEndian>>(size: usize) -> Option<usize> {
T::bit_size_sized(size)
}