Maraiah/src/durandal/err.rs

73 lines
1.3 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 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-21 13:12:26 -08:00
macro_rules! flag_ok {
($t:ident, $v:expr) => {
match $t::from_bits($v) {
Some(v) => Ok(v),
None => Err(err_msg(concat!("bad ", stringify!($t)))),
}
};
}
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
}
2019-02-20 18:33:57 -08:00
pub fn err_msg(msg: &'static str) -> Error {Error::from(ErrMsg(msg))}
2019-02-11 03:28:53 -08:00
impl Fail for ReprError {}
2019-02-10 02:31:57 -08:00
impl Fail for ErrMsg {}
2019-02-11 03:28:53 -08:00
impl fmt::Display for ReprError
{
2019-02-24 20:34:59 -08:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
2019-02-08 21:53:27 -08:00
{
write!(f, "representation error (got {})", self.0)
}
}
2019-02-11 03:28:53 -08:00
impl fmt::Debug for ReprError
{
2019-02-24 20:34:59 -08:00
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
{
2019-02-24 20:34:59 -08:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {f.write_str(self.0)}
2019-02-10 02:31:57 -08:00
}
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-24 20:34:59 -08:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
2019-02-10 02:31:57 -08:00
{
fmt::Display::fmt(self, f)
}
2019-02-09 11:01:35 -08:00
}
2019-02-10 02:31:57 -08:00
#[derive(PartialEq)]
2019-02-11 03:28:53 -08:00
pub struct ReprError(pub i64);
2019-02-10 02:31:57 -08:00
struct ErrMsg(&'static str);
pub type ResultS<T> = Result<T, Error>;
2018-12-11 00:08:23 -08:00
// EOF