Mercurial > templog
diff 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 |
line wrap: on
line diff
--- a/rust/src/types.rs Wed Sep 04 23:24:13 2019 +0800 +++ b/rust/src/types.rs Sun Sep 22 20:35:40 2019 +0800 @@ -8,12 +8,10 @@ use std; -use serde::{Deserialize,Serialize}; -use toml; use hyper; use serde_json; -#[derive(Debug)] +#[derive(Debug,Clone)] pub struct Readings { pub temps: HashMap<String, f32>, } @@ -39,7 +37,6 @@ None, Io(io::Error), ParseFloat(std::num::ParseFloatError), - TomlDe(toml::de::Error), SerdeJson(serde_json::Error), Hyper(hyper::Error), } @@ -61,7 +58,6 @@ TemplogErrorKind::None => None, 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::Hyper(ref e) => Some(e), } @@ -71,18 +67,17 @@ impl fmt::Display for TemplogError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.kind_str()); + write!(f, "{}", self.kind_str())?; if !self.msg.is_empty() { - write!(f, ": {}", self.msg); + write!(f, ": {}", self.msg)?; } match self.kind { 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::Hyper(ref e) => write!(f, ": {}", e), - }; + }?; Ok(()) } } @@ -96,10 +91,6 @@ TemplogError::new_kind(msg, 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)) } @@ -134,7 +125,6 @@ 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::Hyper(_) => "Templog http error", @@ -148,12 +138,6 @@ } } -impl From<toml::de::Error> for TemplogError { - fn from(e: toml::de::Error) -> Self { - TemplogError::new_toml_de("", e) - } -} - impl From<std::num::ParseFloatError> for TemplogError { fn from(e: std::num::ParseFloatError) -> Self { TemplogError::new_parse_float("", e) @@ -183,16 +167,19 @@ pub fn new(limit_secs: u64) -> Self { NotTooOften { limit: Duration::new(limit_secs, 0), + // XXX why +1? last: Cell::new(Instant::now() - Duration::new(limit_secs+1, 0)), } } - pub fn and_then<F>(&self, op: F) - where F: Fn() { + pub fn and_then<F, U>(&self, op: F) -> Option<U> + where F: Fn() -> U { let now = Instant::now(); if now - self.last.get() > self.limit { self.last.set(now); - op(); + Some(op()) + } else { + None } } }