use std::{error,io};
use std::fmt::{self, Display, Formatter};
use operation::Operation;
#[derive(Debug)]
pub struct Error {
cause: Option<io::Error>,
message: String
}
impl Error {
pub fn new(operation: &Operation, path: &str) -> Self {
Error { message: Self::message(operation, path), cause: None }
}
pub fn with_cause(operation: &Operation, path: &str, cause: io::Error) -> Self {
Error { message: Self::message(operation, path), cause: Some(cause) }
}
pub fn cause(&self) -> Option<&io::Error> {
self.cause.as_ref()
}
fn message(operation: &Operation, path: &str) -> String {
format!("Unable to {} {}", operation, path)
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl error::Error for Error {
fn description(&self) -> &str {
&self.message
}
}