mirror of
https://codeberg.org/icewind/bitbuffer.git
synced 2026-06-03 16:44:06 +02:00
better error type
This commit is contained in:
parent
f5e5fe54d1
commit
bedeec3d26
2 changed files with 40 additions and 6 deletions
|
|
@ -82,10 +82,17 @@ where
|
||||||
|
|
||||||
fn read_usize(&self, position: usize, count: usize) -> Result<usize> {
|
fn read_usize(&self, position: usize, count: usize) -> Result<usize> {
|
||||||
if position + count > self.bit_len {
|
if position + count > self.bit_len {
|
||||||
return Err(ReadError::NotEnoughData {
|
if position > self.bit_len {
|
||||||
requested: count,
|
return Err(ReadError::IndexOutOfBounds{
|
||||||
bits_left: self.bit_len - position,
|
pos: position,
|
||||||
});
|
size: self.bit_len,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return Err(ReadError::NotEnoughData {
|
||||||
|
requested: count,
|
||||||
|
bits_left: self.bit_len - position,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let byte_index = min(position / 8, self.byte_len - USIZE_SIZE);
|
let byte_index = min(position / 8, self.byte_len - USIZE_SIZE);
|
||||||
let bit_offset = position - byte_index * 8;
|
let bit_offset = position - byte_index * 8;
|
||||||
|
|
|
||||||
31
src/lib.rs
31
src/lib.rs
|
|
@ -44,10 +44,14 @@
|
||||||
// for bench on nightly
|
// for bench on nightly
|
||||||
//extern crate test;
|
//extern crate test;
|
||||||
|
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fmt;
|
||||||
|
use std::fmt::Display;
|
||||||
|
pub use std::string::FromUtf8Error;
|
||||||
|
|
||||||
pub use buffer::BitBuffer;
|
pub use buffer::BitBuffer;
|
||||||
pub use endianness::*;
|
pub use endianness::*;
|
||||||
pub use read::{Read, ReadSized};
|
pub use read::{Read, ReadSized};
|
||||||
pub use std::string::FromUtf8Error;
|
|
||||||
pub use stream::BitStream;
|
pub use stream::BitStream;
|
||||||
|
|
||||||
mod buffer;
|
mod buffer;
|
||||||
|
|
@ -75,7 +79,7 @@ pub enum ReadError {
|
||||||
/// the number of bits left in the buffer
|
/// the number of bits left in the buffer
|
||||||
bits_left: usize,
|
bits_left: usize,
|
||||||
},
|
},
|
||||||
/// The requested position is outside the bounds of the buffer
|
/// The requested position is outside the bounds of the stream or buffer
|
||||||
IndexOutOfBounds {
|
IndexOutOfBounds {
|
||||||
/// The requested position
|
/// The requested position
|
||||||
pos: usize,
|
pos: usize,
|
||||||
|
|
@ -86,11 +90,34 @@ pub enum ReadError {
|
||||||
Utf8Error(FromUtf8Error),
|
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::Utf8Error(err) => err.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<FromUtf8Error> for ReadError {
|
impl From<FromUtf8Error> for ReadError {
|
||||||
fn from(err: FromUtf8Error) -> ReadError {
|
fn from(err: FromUtf8Error) -> ReadError {
|
||||||
ReadError::Utf8Error(err)
|
ReadError::Utf8Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Error for ReadError {
|
||||||
|
fn cause(&self) -> Option<&Error> {
|
||||||
|
match self {
|
||||||
|
ReadError::Utf8Error(err) => Some(err),
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Either the read bits in the requested format or a [`ReadError`](enum.ReadError.html)
|
/// Either the read bits in the requested format or a [`ReadError`](enum.ReadError.html)
|
||||||
pub type Result<T> = std::result::Result<T, ReadError>;
|
pub type Result<T> = std::result::Result<T, ReadError>;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue