Add error type implementation
This commit is contained in:
parent
fd204ee05e
commit
15b1adbc20
1 changed files with 63 additions and 0 deletions
|
@ -0,0 +1,63 @@
|
|||
use std::error::Error;
|
||||
use std::fmt::Error as FmtError;
|
||||
use std::clone::Clone;
|
||||
use std::fmt::{Debug, Display, Formatter};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum TagErrorKind {
|
||||
}
|
||||
|
||||
fn tag_error_type_as_str(e: &TagErrorKind) -> &'static str {
|
||||
match e {
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for TagErrorKind {
|
||||
|
||||
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
||||
try!(write!(fmt, "{}", tag_error_type_as_str(self)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TagError {
|
||||
kind: TagErrorKind,
|
||||
cause: Option<Box<Error>>,
|
||||
}
|
||||
|
||||
impl TagError {
|
||||
|
||||
pub fn new(errtype: TagErrorKind, cause: Option<Box<Error>>) -> TagError {
|
||||
TagError {
|
||||
kind: errtype,
|
||||
cause: cause,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Display for TagError {
|
||||
|
||||
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
|
||||
try!(write!(fmt, "[{}]", tag_error_type_as_str(&self.kind.clone())));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl Error for TagError {
|
||||
|
||||
fn description(&self) -> &str {
|
||||
tag_error_type_as_str(&self.kind.clone())
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&Error> {
|
||||
self.cause.as_ref().map(|e| &**e)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in a new issue