43 lines
874 B
Rust
43 lines
874 B
Rust
use bytes::{Buf, Bytes};
|
|
use failure;
|
|
use std::fmt;
|
|
use std::io;
|
|
use std::result;
|
|
|
|
pub type Result<T> = result::Result<T, failure::Error>;
|
|
|
|
#[derive(Debug)]
|
|
pub struct NotEnoughData {
|
|
position: u64,
|
|
data: Bytes,
|
|
}
|
|
|
|
impl NotEnoughData {
|
|
pub fn check(data: &mut io::Cursor<Bytes>, need: usize) -> Result<()> {
|
|
if data.remaining() < need {
|
|
bail!(NotEnoughData{
|
|
position: data.position(),
|
|
data: data.get_ref().clone(),
|
|
})
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for NotEnoughData {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "not enough data at position {} in {:?}", self.position, self.data)
|
|
}
|
|
}
|
|
|
|
impl failure::Fail for NotEnoughData {}
|
|
|
|
macro_rules! check_enough_data {
|
|
($data:ident, $n:expr, $context:expr) => {
|
|
{
|
|
use $crate::failure::ResultExt;
|
|
$crate::errors::NotEnoughData::check($data, $n).context($context)?;
|
|
}
|
|
};
|
|
}
|