comparison rust/src/config.rs @ 598:d4fbfb5c46ff rust

broken update of versions of things
author Matt Johnston <matt@ucc.asn.au>
date Tue, 07 Feb 2017 21:56:58 +0800
parents a440eafa84a9
children f71cf1ad745f
comparison
equal deleted inserted replaced
597:a440eafa84a9 598:d4fbfb5c46ff
1 extern crate toml; 1 extern crate toml;
2 2
3 use serde::Serialize; 3 use std::fs::File;
4 use std::io::Read;
5 use serde::{Serialize,Deserialize,Deserializer,Serializer};
4 6
5 #[derive(Deserialize,Serialize,Debug,Clone)] 7 #[derive(Deserialize,Serialize,Debug,Clone)]
6 #[allow(non_snake_case)] 8 #[allow(non_snake_case)]
7 pub struct Config { 9 pub struct Config {
8 pub SENSOR_SLEEP: u64, 10 pub SENSOR_SLEEP: u64,
29 pub SETTINGS_URL: String, 31 pub SETTINGS_URL: String,
30 } 32 }
31 33
32 impl Config { 34 impl Config {
33 35
34 pub fn new() -> Self { 36 pub fn default() -> Self {
35 Config { 37 Config {
36 SENSOR_SLEEP: 5, 38 SENSOR_SLEEP: 5,
37 UPLOAD_SLEEP: 83, 39 UPLOAD_SLEEP: 83,
38 40
39 FRIDGE_DELAY: 600, // 10 mins, to avoid fridge damage from frequent cycling off/on 41 FRIDGE_DELAY: 600, // 10 mins, to avoid fridge damage from frequent cycling off/on
66 let mut e = toml::Encoder::new(); 68 let mut e = toml::Encoder::new();
67 self.serialize(&mut e).unwrap(); 69 self.serialize(&mut e).unwrap();
68 toml::Value::Table(e.toml).to_string() 70 toml::Value::Table(e.toml).to_string()
69 } 71 }
70 72
71 /*
72 pub fn parse(&mut self, s: &str) {
73 let filename = "tempserver.conf";
74 73
75 let f = File::open(filename) 74 pub fn merge(&self, conf: &str) -> Result<Self, String>{
75 let mut p = toml::Parser::new(&conf);
76 p.parse()
77 .ok_or_else(|| {
78 format!("toml parsing failed: {:?}", p.errors)
79 })
80 .map(|mut new_toml| {
81 let mut e = toml::Encoder::new();
82 self.serialize(&mut e).unwrap(); // XXX what could go wrong?
83 let mut ex_toml = e.toml;
84 ex_toml.append(&mut new_toml);
85 let mut dec = toml::Decoder::new(toml::Value::Table(ex_toml));
86 Self::deserialize(&mut dec).unwrap() // could this go wrong?
87 })
88 }
89
90 pub fn merge_file(&self, filename: &str) -> Result<Self, String>{
91
92 let r = File::open(filename)
76 .map_err(|e| e.to_string()) 93 .map_err(|e| e.to_string())
77 .and_then(|mut f| { 94 .and_then(|mut f| {
78 let mut s = String::new(); 95 let mut s = String::new();
79 f.read_to_string(&mut s) 96 f.read_to_string(&mut s)
80 .map_err(|e| e.to_string()) 97 .map_err(|e| e.to_string())
81 .map(|_| s) 98 .map(|_| s)
82 })
83 .and_then(|s| {
84
85 .map_err(|e| e.to_string())
86 }); 99 });
87 100
88 match f { 101 r.and_then(|s| {
89 Ok() => { 102 self.merge(&s)
90 103 })
91 }, 104 .map_err(|e| {
92 Err(e) => { 105 format!("Error loading config file {}: {}",
93 debug!("Error loading config file {}: {}", 106 filename, e)
94 filename, e); 107 })
95
96 }
97 }
98
99 } 108 }
100 */
101 } 109 }