rust-dnsbox/lib/dnsbox-base/src/errors.rs

43 lines
884 B
Rust
Raw Normal View History

2017-12-16 20:58:18 +00:00
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 {
2019-07-01 15:43:34 +00:00
failure::bail!(NotEnoughData{
2017-12-16 20:58:18 +00:00
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) => {
{
2019-07-01 15:43:34 +00:00
use $crate::_failure::ResultExt;
2017-12-16 20:58:18 +00:00
$crate::errors::NotEnoughData::check($data, $n).context($context)?;
}
};
}