changeset 612:5fc41e0833b4 rust

fix TemplogError references
author Matt Johnston <matt@ucc.asn.au>
date Thu, 02 Mar 2017 00:06:03 +0800
parents f3e39e2107fd
children 5c2b0d47bb83
files rust/src/types.rs
diffstat 1 files changed, 56 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/rust/src/types.rs	Tue Feb 28 22:58:47 2017 +0800
+++ b/rust/src/types.rs	Thu Mar 02 00:06:03 2017 +0800
@@ -94,81 +94,94 @@
 
 #[derive(Debug)]
 pub struct TemplogError {
+    msg: String,
     desc: String,
     kind: TemplogErrorKind,
 }
 
 impl Error for TemplogError {
     fn description(&self) -> &str { 
-        &format!("{}", self)
+        &self.desc
     }
 
     fn cause(&self) -> Option<&Error> { 
         match self.kind {
             TemplogErrorKind::None => None,
-            TemplogErrorKind::Io(e) => Some(&e),
-            TemplogErrorKind::ParseFloat(e) => Some(&e),
-            TemplogErrorKind::TomlDe(e) => Some(&e),
-            TemplogErrorKind::SerdeJson(e) => Some(&e),
-            TemplogErrorKind::Curl(e) => Some(&e),
+            TemplogErrorKind::Io(ref e) => Some(e),
+            TemplogErrorKind::ParseFloat(ref e) => Some(e),
+            TemplogErrorKind::TomlDe(ref e) => Some(e),
+            TemplogErrorKind::SerdeJson(ref e) => Some(e),
+            TemplogErrorKind::Curl(ref e) => Some(e),
         }
     }
+
 }
 
 impl fmt::Display for TemplogError {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "{}", self.kind_str());
+        if !self.msg.is_empty() {
+            write!(f, ": {}", self.msg);
+        }
         match self.kind {
-            TemplogErrorKind::None => write!(f, "Templog Error: {}", self.desc),
-            TemplogErrorKind::Io(e) => write!(f, "Templog IO error {}: {}", self.desc, e),
-            TemplogErrorKind::TomlDe(e) => write!(f, "Templog toml error {}: {}", self.desc, e),
-            TemplogErrorKind::SerdeJson(e) => write!(f, "Json decode error {}: {}", self.desc, e),
-            TemplogErrorKind::ParseFloat(e) => write!(f, "Templog parse error {}: {}", self.desc, e),
-            TemplogErrorKind::Curl(e) => write!(f, "Templog curl http error {}: {}", self.desc, e),
+            TemplogErrorKind::None => Ok(()),
+            TemplogErrorKind::Io(ref e) => write!(f, ": {}", e),
+            TemplogErrorKind::TomlDe(ref e) => write!(f, ": {}", e),
+            TemplogErrorKind::SerdeJson(ref e) => write!(f, ": {}", e),
+            TemplogErrorKind::ParseFloat(ref e) => write!(f, ": {}", e),
+            TemplogErrorKind::Curl(ref e) => write!(f, ": {}", e),
         };
         Ok(())
     }
 }
 
 impl TemplogError {
-    pub fn new(desc: &str) -> Self {
-        TemplogError { 
-            desc: desc.to_string(),
-            kind: TemplogErrorKind::None,
-        }
+    pub fn new(msg: &str) -> Self {
+        TemplogError::new_kind(msg, TemplogErrorKind::None)
+    }
+
+    pub fn new_io(msg: &str, e: io::Error) -> Self {
+        TemplogError::new_kind(msg, TemplogErrorKind::Io(e))
     }
 
-    pub fn new_io(desc: &str, e: io::Error) -> Self {
-        TemplogError { 
-            desc: desc.to_string(),
-            kind: TemplogErrorKind::Io(e),
-        }
+    pub fn new_toml_de(msg: &str, e: toml::de::Error) -> Self {
+        TemplogError::new_kind(msg, TemplogErrorKind::TomlDe(e))
+    }
+
+    pub fn new_parse_float(msg: &str, e: std::num::ParseFloatError) -> Self {
+        TemplogError::new_kind(msg, TemplogErrorKind::ParseFloat(e))
+    }
+
+    pub fn new_curl(msg: &str, e: curl::Error) -> Self {
+        TemplogError::new_kind(msg, TemplogErrorKind::Curl(e))
     }
 
-    pub fn new_toml_de(desc: &str, e: toml::de::Error) -> Self {
-        TemplogError { 
-            desc: desc.to_string(),
-            kind: TemplogErrorKind::TomlDe(e),
-        }
+    pub fn new_serde_json(msg: &str, e: serde_json::Error) -> Self {
+        TemplogError::new_kind(msg, TemplogErrorKind::SerdeJson(e))
     }
 
-    pub fn new_parse_float(desc: &str, e: std::num::ParseFloatError) -> Self {
-        TemplogError { 
-            desc: desc.to_string(),
-            kind: TemplogErrorKind::ParseFloat(e),
-        }
+    fn new_kind(msg: &str, k: TemplogErrorKind) -> Self {
+        let mut s = TemplogError { 
+            msg: msg.to_string(),
+            desc: String::new(),
+            kind: k,
+        };
+        s.desc = if s.msg.is_empty() {
+            s.kind_str().to_string()
+        } else {
+            format!("{}: {}", s.kind_str(), s.msg)
+        };
+        s
     }
 
-    pub fn new_curl(desc: &str, e: curl::Error) -> Self {
-        TemplogError { 
-            desc: desc.to_string(),
-            kind: TemplogErrorKind::Curl(e),
-        }
-    }
-
-    pub fn new_serde_json(desc: &str, e: serde_json::Error) -> Self {
-        TemplogError { 
-            desc: desc.to_string(),
-            kind: TemplogErrorKind::SerdeJson(e),
+    fn kind_str(&self) -> &str {
+        match self.kind {
+            TemplogErrorKind::None => "Templog Error",
+            TemplogErrorKind::Io(_) => "Templog IO error",
+            TemplogErrorKind::TomlDe(_) => "Templog toml error",
+            TemplogErrorKind::SerdeJson(_) => "Templog Json decode error",
+            TemplogErrorKind::ParseFloat(_) => "Templog parse error",
+            TemplogErrorKind::Curl(_) => "Templog curl http error",
         }
     }
 }