diff rust/src/config.rs @ 601:8c21df3711e2 rust

rigid_config more on sensors
author Matt Johnston <matt@ucc.asn.au>
date Wed, 15 Feb 2017 23:58:02 +0800
parents 9c76f3cf01ea
children f3e39e2107fd
line wrap: on
line diff
--- a/rust/src/config.rs	Tue Feb 07 22:57:29 2017 +0800
+++ b/rust/src/config.rs	Wed Feb 15 23:58:02 2017 +0800
@@ -1,5 +1,6 @@
 extern crate toml;
 
+use std::error::Error;
 use std::fs::File;
 use std::io::Read;
 use serde::{Serialize,Deserialize,Deserializer,Serializer};
@@ -68,32 +69,22 @@
         toml::to_string(self).unwrap()
     }
 
-    pub fn merge(&self, conf: &str) -> Result<Self, String>{
+    pub fn merge(&self, conf: &str) -> Result<Self, Box<Error>> {
         // convert existing and new toml into tables, combine them.
-        let mut new_toml = try!(toml::from_str(conf).map_err(|e| e.to_string()));
+        let mut new_toml = toml::from_str(conf)?;
         let mut ex_val = toml::Value::try_from(self).unwrap();
         let mut ex_toml = ex_val.as_table_mut().unwrap();
         ex_toml.append(&mut new_toml);
-        let ret: Self = toml::Value::Table(ex_toml.clone()).try_into().unwrap();
+        // TODO: wrap the error with a better message?
+        let ret = toml::Value::Table(ex_toml.clone()).try_into()?;
         Ok(ret)
     }
 
-    pub fn merge_file(&self, filename: &str) -> Result<Self, String>{
+    pub fn merge_file(&self, filename: &str) -> Result<Self, Box<Error>> {
 
-        let r = File::open(filename)
-            .and_then(|mut f| {
-                let mut s = String::new();
-                f.read_to_string(&mut s)
-                    .map(|_| s)
-            })
-            .map_err(|e| e.to_string());
+        let mut s = String::new();
+        File::open(filename)?.read_to_string(&mut s)?;
 
-        r.and_then(|s| {
-            self.merge(&s)
-        })
-        .map_err(|e| {
-            format!("Error loading config file {}: {}",
-                    filename, e)
-        })
+        self.merge(&s)
     }
 }