Merge pull request #416 from matthiasbeyer/libimagerror/into-error
Libimagerror/into error
This commit is contained in:
commit
88e9519a00
3 changed files with 32 additions and 0 deletions
|
@ -1,9 +1,13 @@
|
|||
use into::IntoError;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! generate_error_imports {
|
||||
() => {
|
||||
use std::error::Error;
|
||||
use std::fmt::Error as FmtError;
|
||||
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)]
|
||||
pub struct $name {
|
||||
err_type: $kindname,
|
||||
|
|
14
libimagerror/src/into.rs
Normal file
14
libimagerror/src/into.rs
Normal 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;
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
#[macro_use] extern crate log;
|
||||
extern crate ansi_term;
|
||||
|
||||
pub mod into;
|
||||
pub mod error_gen;
|
||||
pub mod trace;
|
||||
|
|
Loading…
Reference in a new issue