Merge pull request #416 from matthiasbeyer/libimagerror/into-error

Libimagerror/into error
This commit is contained in:
Matthias Beyer 2016-05-20 18:13:13 +02:00
commit 88e9519a00
3 changed files with 32 additions and 0 deletions

View file

@ -1,9 +1,13 @@
use into::IntoError;
#[macro_export] #[macro_export]
macro_rules! generate_error_imports { macro_rules! generate_error_imports {
() => { () => {
use std::error::Error; use std::error::Error;
use std::fmt::Error as FmtError; use std::fmt::Error as FmtError;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use $crate::into::IntoError;
} }
} }
@ -41,6 +45,19 @@ macro_rules! generate_error_types {
} }
impl IntoError for $kindname {
type Target = $name;
fn into_error(self) -> Self::Target {
$name::new(self, None)
}
fn into_error_with_cause(self, cause: Box<Error>) -> Self::Target {
$name::new(self, Some(cause))
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct $name { pub struct $name {
err_type: $kindname, err_type: $kindname,

14
libimagerror/src/into.rs Normal file
View file

@ -0,0 +1,14 @@
use std::error::Error;
/// Trait to help converting Error kinds into Error instances
pub trait IntoError {
type Target: Error;
/// Convert the type into an error with no cause
fn into_error(self) -> Self::Target;
/// Convert the type into an error with cause
fn into_error_with_cause(self, cause: Box<Error>) -> Self::Target;
}

View file

@ -1,5 +1,6 @@
#[macro_use] extern crate log; #[macro_use] extern crate log;
extern crate ansi_term; extern crate ansi_term;
pub mod into;
pub mod error_gen; pub mod error_gen;
pub mod trace; pub mod trace;