Maraiah/src/durandal/err.rs

71 lines
1.2 KiB
Rust
Raw Normal View History

2018-12-11 00:08:23 -08:00
//! Error handling.
2019-02-10 02:31:57 -08:00
pub use failure::{Error, Fail};
2018-12-11 00:08:23 -08:00
use crate::durandal::traits::PrimInt;
use std::fmt;
2019-02-10 20:33:38 -08:00
macro_rules! ok {
2019-02-10 02:31:57 -08:00
($v:expr, $msg:expr) => {
match $v {
Some(v) => Ok(v),
None => Err(err_msg($msg)),
}
2019-02-10 20:33:38 -08:00
};
2019-02-10 02:31:57 -08:00
}
2019-02-10 20:33:38 -08:00
macro_rules! bail {
2019-02-10 02:31:57 -08:00
($e:expr) => {
2019-02-10 20:33:38 -08:00
return Err(err_msg($e));
};
2019-02-10 02:31:57 -08:00
}
pub fn err_msg(msg: &'static str) -> Error
{
Error::from(ErrMsg(msg))
}
impl<T> Fail for ReprError<T> where T: PrimInt {}
2019-02-10 02:31:57 -08:00
impl Fail for ErrMsg {}
impl<T> fmt::Display for ReprError<T> where T: PrimInt
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
2019-02-08 21:53:27 -08:00
{
write!(f, "representation error (got {})", self.0)
}
}
impl<T> fmt::Debug for ReprError<T> where T: PrimInt
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
2019-02-08 21:53:27 -08:00
{
fmt::Display::fmt(self, f)
}
}
2019-02-10 02:31:57 -08:00
impl fmt::Display for ErrMsg
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
f.write_str(self.0)
}
}
2018-12-11 00:08:23 -08:00
2019-02-10 02:31:57 -08:00
impl fmt::Debug for ErrMsg
2019-02-09 11:01:35 -08:00
{
2019-02-10 02:31:57 -08:00
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{
fmt::Display::fmt(self, f)
}
2019-02-09 11:01:35 -08:00
}
2019-02-10 02:31:57 -08:00
#[derive(PartialEq)]
pub struct ReprError<T>(pub T) where T: PrimInt;
struct ErrMsg(&'static str);
pub type ResultS<T> = Result<T, Error>;
2018-12-11 00:08:23 -08:00
// EOF