Maraiah/maraiah/err.rs

102 lines
2.0 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($crate::err::err_msg($msg)),
2019-02-10 02:31:57 -08:00
}
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 {
2019-04-01 01:28:01 -07:00
($t:ident$(::$tc:ident)*, $v:expr) => {
match $t$(::$tc)*::from_bits($v) {
2019-02-21 13:12:26 -08:00
Some(v) => Ok(v),
None => Err($crate::err::err_msg(concat!("bad ", stringify!($t)))),
2019-02-21 13:12:26 -08:00
}
};
}
2019-02-10 20:33:38 -08:00
macro_rules! bail {
2019-02-10 02:31:57 -08:00
($e:expr) => {
return Err($crate::err::err_msg($e));
2019-02-10 20:33:38 -08:00
};
2019-02-10 02:31:57 -08:00
}
2019-03-01 01:27:14 -08:00
/// Returns an `Error` with a static string.
2019-03-04 18:14:09 -08:00
///
/// # Examples
///
/// ```
2019-06-13 18:09:07 -07:00
/// use maraiah::err::err_msg;
2019-03-04 18:14:09 -08:00
///
2019-04-01 06:05:06 -07:00
/// assert_eq!(format!("{}", err_msg("oh no not things")),
/// "oh no not things");
2019-03-04 18:14:09 -08:00
/// ```
2019-03-12 02:52:31 -07:00
pub fn err_msg(msg: &'static str) -> Error {ErrMsg(msg).into()}
/// Returns an `Error` from a `ReprError`.
///
/// # Examples
///
/// ```
2019-06-13 18:09:07 -07:00
/// use maraiah::err::repr_error;
2019-03-12 02:52:31 -07:00
///
2019-04-01 06:05:06 -07:00
/// assert_eq!(format!("{}", repr_error(77)),
/// "representation error (got 77)");
2019-03-12 02:52:31 -07:00
/// ```
pub fn repr_error<T: Into<i64>>(n: T) -> Error {ReprError::new(n).into()}
2019-03-04 05:59:43 -08:00
impl ReprError
{
2019-03-04 18:14:09 -08:00
/// Creates a new `ReprError`.
///
/// # Examples
///
/// ```
2019-06-13 18:09:07 -07:00
/// use maraiah::err::ReprError;
2019-03-04 18:14:09 -08:00
///
/// let err = ReprError::new(7);
///
/// assert_eq!(format!("{}", err), "representation error (got 7)");
/// ```
2019-03-04 05:59:43 -08:00
#[inline]
2019-03-12 02:52:31 -07:00
pub fn new<T: Into<i64>>(n: T) -> Self {Self(n.into())}
2019-03-04 05:59:43 -08:00
}
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-10 02:31:57 -08:00
impl fmt::Display for ErrMsg
{
2019-03-04 02:18:57 -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-03-01 01:27:14 -08:00
/// A representation error for an integer.
2019-03-13 07:53:30 -07:00
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
2019-03-04 05:59:43 -08:00
pub struct ReprError(i64);
2019-02-10 02:31:57 -08:00
2019-03-04 05:59:43 -08:00
#[derive(Debug)]
2019-02-10 02:31:57 -08:00
struct ErrMsg(&'static str);
2019-03-01 01:27:14 -08:00
/// A generic `failure` based `Result` type.
2019-02-10 02:31:57 -08:00
pub type ResultS<T> = Result<T, Error>;
2018-12-11 00:08:23 -08:00
// EOF