comparison rust/src/types.rs @ 634:a5721c02d3ee rust

build succeeds
author Matt Johnston <matt@ucc.asn.au>
date Sun, 22 Sep 2019 20:35:40 +0800
parents 490e9e15b98c
children 89818a14648b
comparison
equal deleted inserted replaced
633:490e9e15b98c 634:a5721c02d3ee
6 use std::cmp; 6 use std::cmp;
7 use std::cell::Cell; 7 use std::cell::Cell;
8 8
9 use std; 9 use std;
10 10
11 use serde::{Deserialize,Serialize};
12 use toml;
13 use hyper; 11 use hyper;
14 use serde_json; 12 use serde_json;
15 13
16 #[derive(Debug)] 14 #[derive(Debug,Clone)]
17 pub struct Readings { 15 pub struct Readings {
18 pub temps: HashMap<String, f32>, 16 pub temps: HashMap<String, f32>,
19 } 17 }
20 18
21 impl Readings { 19 impl Readings {
37 #[derive(Debug)] 35 #[derive(Debug)]
38 pub enum TemplogErrorKind { 36 pub enum TemplogErrorKind {
39 None, 37 None,
40 Io(io::Error), 38 Io(io::Error),
41 ParseFloat(std::num::ParseFloatError), 39 ParseFloat(std::num::ParseFloatError),
42 TomlDe(toml::de::Error),
43 SerdeJson(serde_json::Error), 40 SerdeJson(serde_json::Error),
44 Hyper(hyper::Error), 41 Hyper(hyper::Error),
45 } 42 }
46 43
47 #[derive(Debug)] 44 #[derive(Debug)]
59 fn cause(&self) -> Option<&dyn Error> { 56 fn cause(&self) -> Option<&dyn Error> {
60 match self.kind { 57 match self.kind {
61 TemplogErrorKind::None => None, 58 TemplogErrorKind::None => None,
62 TemplogErrorKind::Io(ref e) => Some(e), 59 TemplogErrorKind::Io(ref e) => Some(e),
63 TemplogErrorKind::ParseFloat(ref e) => Some(e), 60 TemplogErrorKind::ParseFloat(ref e) => Some(e),
64 TemplogErrorKind::TomlDe(ref e) => Some(e),
65 TemplogErrorKind::SerdeJson(ref e) => Some(e), 61 TemplogErrorKind::SerdeJson(ref e) => Some(e),
66 TemplogErrorKind::Hyper(ref e) => Some(e), 62 TemplogErrorKind::Hyper(ref e) => Some(e),
67 } 63 }
68 } 64 }
69 65
70 } 66 }
71 67
72 impl fmt::Display for TemplogError { 68 impl fmt::Display for TemplogError {
73 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 69 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74 write!(f, "{}", self.kind_str()); 70 write!(f, "{}", self.kind_str())?;
75 if !self.msg.is_empty() { 71 if !self.msg.is_empty() {
76 write!(f, ": {}", self.msg); 72 write!(f, ": {}", self.msg)?;
77 } 73 }
78 match self.kind { 74 match self.kind {
79 TemplogErrorKind::None => Ok(()), 75 TemplogErrorKind::None => Ok(()),
80 TemplogErrorKind::Io(ref e) => write!(f, ": {}", e), 76 TemplogErrorKind::Io(ref e) => write!(f, ": {}", e),
81 TemplogErrorKind::TomlDe(ref e) => write!(f, ": {}", e),
82 TemplogErrorKind::SerdeJson(ref e) => write!(f, ": {}", e), 77 TemplogErrorKind::SerdeJson(ref e) => write!(f, ": {}", e),
83 TemplogErrorKind::ParseFloat(ref e) => write!(f, ": {}", e), 78 TemplogErrorKind::ParseFloat(ref e) => write!(f, ": {}", e),
84 TemplogErrorKind::Hyper(ref e) => write!(f, ": {}", e), 79 TemplogErrorKind::Hyper(ref e) => write!(f, ": {}", e),
85 }; 80 }?;
86 Ok(()) 81 Ok(())
87 } 82 }
88 } 83 }
89 84
90 impl TemplogError { 85 impl TemplogError {
92 TemplogError::new_kind(msg, TemplogErrorKind::None) 87 TemplogError::new_kind(msg, TemplogErrorKind::None)
93 } 88 }
94 89
95 pub fn new_io(msg: &str, e: io::Error) -> Self { 90 pub fn new_io(msg: &str, e: io::Error) -> Self {
96 TemplogError::new_kind(msg, TemplogErrorKind::Io(e)) 91 TemplogError::new_kind(msg, TemplogErrorKind::Io(e))
97 }
98
99 pub fn new_toml_de(msg: &str, e: toml::de::Error) -> Self {
100 TemplogError::new_kind(msg, TemplogErrorKind::TomlDe(e))
101 } 92 }
102 93
103 pub fn new_parse_float(msg: &str, e: std::num::ParseFloatError) -> Self { 94 pub fn new_parse_float(msg: &str, e: std::num::ParseFloatError) -> Self {
104 TemplogError::new_kind(msg, TemplogErrorKind::ParseFloat(e)) 95 TemplogError::new_kind(msg, TemplogErrorKind::ParseFloat(e))
105 } 96 }
132 123
133 fn kind_str(&self) -> &str { 124 fn kind_str(&self) -> &str {
134 match self.kind { 125 match self.kind {
135 TemplogErrorKind::None => "Templog Error", 126 TemplogErrorKind::None => "Templog Error",
136 TemplogErrorKind::Io(_) => "Templog IO error", 127 TemplogErrorKind::Io(_) => "Templog IO error",
137 TemplogErrorKind::TomlDe(_) => "Templog toml error",
138 TemplogErrorKind::SerdeJson(_) => "Templog Json decode error", 128 TemplogErrorKind::SerdeJson(_) => "Templog Json decode error",
139 TemplogErrorKind::ParseFloat(_) => "Templog parse error", 129 TemplogErrorKind::ParseFloat(_) => "Templog parse error",
140 TemplogErrorKind::Hyper(_) => "Templog http error", 130 TemplogErrorKind::Hyper(_) => "Templog http error",
141 } 131 }
142 } 132 }
143 } 133 }
144 134
145 impl From<io::Error> for TemplogError { 135 impl From<io::Error> for TemplogError {
146 fn from(e: io::Error) -> Self { 136 fn from(e: io::Error) -> Self {
147 TemplogError::new_io("", e) 137 TemplogError::new_io("", e)
148 }
149 }
150
151 impl From<toml::de::Error> for TemplogError {
152 fn from(e: toml::de::Error) -> Self {
153 TemplogError::new_toml_de("", e)
154 } 138 }
155 } 139 }
156 140
157 impl From<std::num::ParseFloatError> for TemplogError { 141 impl From<std::num::ParseFloatError> for TemplogError {
158 fn from(e: std::num::ParseFloatError) -> Self { 142 fn from(e: std::num::ParseFloatError) -> Self {
181 165
182 impl NotTooOften { 166 impl NotTooOften {
183 pub fn new(limit_secs: u64) -> Self { 167 pub fn new(limit_secs: u64) -> Self {
184 NotTooOften { 168 NotTooOften {
185 limit: Duration::new(limit_secs, 0), 169 limit: Duration::new(limit_secs, 0),
170 // XXX why +1?
186 last: Cell::new(Instant::now() - Duration::new(limit_secs+1, 0)), 171 last: Cell::new(Instant::now() - Duration::new(limit_secs+1, 0)),
187 } 172 }
188 } 173 }
189 174
190 pub fn and_then<F>(&self, op: F) 175 pub fn and_then<F, U>(&self, op: F) -> Option<U>
191 where F: Fn() { 176 where F: Fn() -> U {
192 let now = Instant::now(); 177 let now = Instant::now();
193 if now - self.last.get() > self.limit { 178 if now - self.last.get() > self.limit {
194 self.last.set(now); 179 self.last.set(now);
195 op(); 180 Some(op())
181 } else {
182 None
196 } 183 }
197 } 184 }
198 } 185 }
199 186
200 struct Period { 187 struct Period {