From 808e44339a38b2888bd736d34eac2f99a517dc10 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 5 Dec 2015 19:21:27 +0100 Subject: [PATCH] Implement TempFileError and traits for it --- src/ui/external/editor.rs | 58 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/src/ui/external/editor.rs b/src/ui/external/editor.rs index 4442d0a1..f8b26252 100644 --- a/src/ui/external/editor.rs +++ b/src/ui/external/editor.rs @@ -4,6 +4,9 @@ use std::path::PathBuf; use std::fs::File; use std::error::Error; +use std::fmt::{Debug, Display, Formatter}; +use std::fmt; + use runtime::Runtime; /* @@ -49,5 +52,58 @@ impl Drop for TempFile { pub struct TempFileError { - cause: Option>, + desc: &'static str, + caused_by: Option>, } + +impl TempFileError { + + pub fn new(s: &'static str) -> TempFileError { + TempFileError { + desc: s, + caused_by: None, + } + } + + pub fn with_cause(s: &'static str, c: Box) -> 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) + } + +} +