Implement TempFileError and traits for it

This commit is contained in:
Matthias Beyer 2015-12-05 19:21:27 +01:00
parent 900ffcb7d1
commit 808e44339a

View file

@ -4,6 +4,9 @@ use std::path::PathBuf;
use std::fs::File; use std::fs::File;
use std::error::Error; use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::fmt;
use runtime::Runtime; use runtime::Runtime;
/* /*
@ -49,5 +52,58 @@ impl Drop for TempFile {
pub struct TempFileError { pub struct TempFileError {
cause: Option<Box<Error>>, desc: &'static str,
caused_by: Option<Box<Error>>,
} }
impl TempFileError {
pub fn new(s: &'static str) -> TempFileError {
TempFileError {
desc: s,
caused_by: None,
}
}
pub fn with_cause(s: &'static str, c: Box<Error>) -> TempFileError {
TempFileError {
desc: s,
caused_by: Some(c),
}
}
}
impl Display for TempFileError {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, "TempFileError: '{}'", self.desc);
Ok(())
}
}
impl Debug for TempFileError {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, "TempFileError: '{}'", self.desc);
if let Some(ref e) = self.caused_by {
write!(fmt, " cause: {:?}\n\n", e);
}
Ok(())
}
}
impl Error for TempFileError {
fn description(&self) -> &str {
self.desc
}
fn cause(&self) -> Option<&Error> {
self.caused_by.as_ref().map(|e| &**e)
}
}