73 lines
1.2 KiB
Rust
73 lines
1.2 KiB
Rust
//! Error handling.
|
|
|
|
pub use failure::{Error, Fail};
|
|
|
|
use crate::durandal::traits::PrimInt;
|
|
use std::fmt;
|
|
|
|
macro_rules! ok
|
|
{
|
|
($v:expr, $msg:expr) => {
|
|
match $v {
|
|
Some(v) => Ok(v),
|
|
None => Err(err_msg($msg)),
|
|
}
|
|
}
|
|
}
|
|
|
|
macro_rules! bail
|
|
{
|
|
($e:expr) => {
|
|
return Err(err_msg($e))
|
|
}
|
|
}
|
|
|
|
pub fn err_msg(msg: &'static str) -> Error
|
|
{
|
|
Error::from(ErrMsg(msg))
|
|
}
|
|
|
|
impl<T> Fail for ReprError<T> where T: PrimInt {}
|
|
impl Fail for ErrMsg {}
|
|
|
|
impl<T> fmt::Display for ReprError<T> where T: PrimInt
|
|
{
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
|
|
{
|
|
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
|
|
{
|
|
fmt::Display::fmt(self, f)
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for ErrMsg
|
|
{
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
|
|
{
|
|
f.write_str(self.0)
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for ErrMsg
|
|
{
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
|
|
{
|
|
fmt::Display::fmt(self, f)
|
|
}
|
|
}
|
|
|
|
#[derive(PartialEq)]
|
|
pub struct ReprError<T>(pub T) where T: PrimInt;
|
|
|
|
struct ErrMsg(&'static str);
|
|
|
|
pub type ResultS<T> = Result<T, Error>;
|
|
|
|
// EOF
|