imag/libimagentrylist/src/error.rs

85 lines
1.7 KiB
Rust
Raw Normal View History

2016-03-26 13:41:34 +00:00
use std::error::Error;
use std::fmt::Error as FmtError;
use std::fmt::{Display, Formatter};
/**
* Kind of error
*/
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ListErrorKind {
FormatError,
EntryError,
2016-04-06 09:48:01 +00:00
IterationError,
CLIError,
2016-03-26 13:41:34 +00:00
}
fn counter_error_type_as_str(err: &ListErrorKind) -> &'static str{
match *err {
ListErrorKind::FormatError => "FormatError",
ListErrorKind::EntryError => "EntryError",
ListErrorKind::IterationError => "IterationError",
ListErrorKind::CLIError => "No CLI subcommand for listing entries",
2016-03-26 13:41:34 +00:00
}
}
impl Display for ListErrorKind {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
try!(write!(fmt, "{}", counter_error_type_as_str(self)));
Ok(())
}
}
/**
* Store error type
*/
#[derive(Debug)]
pub struct ListError {
err_type: ListErrorKind,
cause: Option<Box<Error>>,
}
impl ListError {
/**
* Build a new ListError from an ListErrorKind, optionally with cause
*/
pub fn new(errtype: ListErrorKind, cause: Option<Box<Error>>) -> ListError {
ListError {
err_type: errtype,
cause: cause,
}
}
/**
* Get the error type of this ListError
*/
pub fn err_type(&self) -> ListErrorKind {
self.err_type
2016-03-26 13:41:34 +00:00
}
}
impl Display for ListError {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
try!(write!(fmt, "[{}]", counter_error_type_as_str(&self.err_type)));
2016-03-26 13:41:34 +00:00
Ok(())
}
}
impl Error for ListError {
fn description(&self) -> &str {
counter_error_type_as_str(&self.err_type)
2016-03-26 13:41:34 +00:00
}
fn cause(&self) -> Option<&Error> {
self.cause.as_ref().map(|e| &**e)
}
}