Maraiah/maraiah/err.rs

168 lines
3.1 KiB
Rust
Raw Permalink 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-07-05 20:21:11 -07: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-21 13:12:26 -08:00
macro_rules! flag_ok {
2019-07-05 20:21:11 -07:00
($t:ident$(::$tc:ident)*, $v:expr) => {
{
let v = $v;
match $t$(::$tc)*::from_bits(v) {
Some(v) => Ok(v),
None => Err($crate::err::ReprError::new(stringify!($t), v)),
}
}
};
2019-02-21 13:12:26 -08:00
}
2019-02-10 20:33:38 -08:00
macro_rules! bail {
2019-07-05 20:21:11 -07:00
($e:expr) => {
return Err($crate::err::err_msg($e));
};
2019-02-10 02:31:57 -08:00
}
2019-06-30 22:23:08 -07:00
#[macro_export]
macro_rules! backtrace {
2019-07-05 20:21:11 -07:00
($e:expr) => {
if cfg!(debug_assertions) {
dbg!($e.backtrace());
}
}
2019-06-30 22:23:08 -07: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()}
impl ParseEnumError
{
2019-07-05 20:21:11 -07:00
/// Returns an `Error` with a message for enumeration parsing errata.
///
/// # Examples
///
/// ```
/// use maraiah::err::ParseEnumError;
///
/// assert_eq!(format!("{}", ParseEnumError::new("TypeName")),
/// "could not parse TypeName");
/// ```
#[inline]
pub const fn new(t: &'static str) -> Self
{
Self(t)
}
}
2019-07-02 22:16:35 -07:00
impl ParseFlagError
{
2019-07-05 20:21:11 -07:00
/// Returns an `Error` with a message for flag parsing errata.
///
/// # Examples
///
/// ```
/// use maraiah::err::ParseFlagError;
///
/// assert_eq!(format!("{}", ParseFlagError::new("TypeName")),
/// "could not parse TypeName");
/// ```
#[inline]
pub const fn new(t: &'static str) -> Self
{
Self(t)
}
2019-07-02 22:16:35 -07:00
}
2019-03-04 05:59:43 -08:00
impl ReprError
{
2019-07-05 20:21:11 -07:00
/// Returns an `Error` with a message for representation errata.
///
/// # Examples
///
/// ```
/// use maraiah::err::ReprError;
///
/// assert_eq!(format!("{}", ReprError::new("TypeName", 77)),
/// "bad TypeName (77)");
/// ```
pub fn new<T: Into<i64>>(t: &'static str, n: T) -> Self
{
Self(t, n.into())
}
2019-03-04 05:59:43 -08:00
}
2019-02-10 02:31:57 -08:00
impl Fail for ErrMsg {}
impl Fail for ParseEnumError {}
2019-07-02 22:16:35 -07:00
impl Fail for ParseFlagError {}
impl Fail for ReprError {}
impl fmt::Display for ParseEnumError
{
2019-07-05 20:21:11 -07:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
write!(f, "could not parse {}", self.0)
}
}
2019-07-02 22:16:35 -07:00
impl fmt::Display for ParseFlagError
{
2019-07-05 20:21:11 -07:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
write!(f, "could not parse {}", self.0)
}
2019-07-02 22:16:35 -07:00
}
2019-02-11 03:28:53 -08:00
impl fmt::Display for ReprError
{
2019-07-05 20:21:11 -07:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
write!(f, "bad {} ({})", self.0, self.1)
}
}
2019-02-10 02:31:57 -08:00
impl fmt::Display for ErrMsg
{
2019-07-05 20:21:11 -07: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
#[derive(Clone, Debug)]
struct ErrMsg(&'static str);
/// A parser error for an enumeration.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct ParseEnumError(&'static str);
2019-07-02 22:16:35 -07:00
/// A parser error for a bit field.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct ParseFlagError(&'static str);
2019-03-01 01:27:14 -08:00
/// A representation error for an integer.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct ReprError(&'static str, i64);
2019-02-10 02:31:57 -08:00
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