Mercurial > templog
annotate rust/src/types.rs @ 615:f153aec221be rust
move Params, epoch code
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 07 Mar 2017 23:56:12 +0800 |
parents | 5fc41e0833b4 |
children | 87a78343140e |
rev | line source |
---|---|
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
1 use std::collections::HashMap; |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
2 use std::time::{Duration,Instant}; |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
3 use std::error::Error; |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
4 use std::fmt; |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
5 use std::io; |
610 | 6 use std::cmp; |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
7 use std::cell::Cell; |
610 | 8 use std::iter::FromIterator; |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
9 use std; |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
10 |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
11 use futures::{Stream,IntoFuture}; |
595 | 12 use serde::{Deserialize,Serialize}; |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
13 use toml; |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
14 use curl; |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
15 use serde_json; |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
16 |
592 | 17 #[derive(Debug)] |
18 pub struct Readings { | |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
596
diff
changeset
|
19 pub temps: HashMap<String, f32>, |
592 | 20 } |
21 | |
22 impl Readings { | |
23 pub fn new() -> Readings { | |
24 Readings { | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
25 temps: HashMap::new(), |
592 | 26 } |
27 } | |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
28 |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
596
diff
changeset
|
29 pub fn add(&mut self, name: &str, v: f32) { |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
596
diff
changeset
|
30 self.temps.insert(name.to_string(), v); |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
31 } |
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
32 |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
596
diff
changeset
|
33 pub fn get_temp(&self, name: &str) -> Option<f32> { |
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
596
diff
changeset
|
34 self.temps.get(name).map(|f| *f) |
592 | 35 } |
36 } | |
37 | |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
38 #[derive(Debug)] |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
39 pub enum TemplogErrorKind { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
40 None, |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
41 Io(io::Error), |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
42 ParseFloat(std::num::ParseFloatError), |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
43 TomlDe(toml::de::Error), |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
44 SerdeJson(serde_json::Error), |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
45 Curl(curl::Error), |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
46 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
47 |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
48 #[derive(Debug)] |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
49 pub struct TemplogError { |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
50 msg: String, |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
51 desc: String, |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
52 kind: TemplogErrorKind, |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
53 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
54 |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
55 impl Error for TemplogError { |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
56 fn description(&self) -> &str { |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
57 &self.desc |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
58 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
59 |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
60 fn cause(&self) -> Option<&Error> { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
61 match self.kind { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
62 TemplogErrorKind::None => None, |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
63 TemplogErrorKind::Io(ref e) => Some(e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
64 TemplogErrorKind::ParseFloat(ref e) => Some(e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
65 TemplogErrorKind::TomlDe(ref e) => Some(e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
66 TemplogErrorKind::SerdeJson(ref e) => Some(e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
67 TemplogErrorKind::Curl(ref e) => Some(e), |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
68 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
69 } |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
70 |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
71 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
72 |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
73 impl fmt::Display for TemplogError { |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
74 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
75 write!(f, "{}", self.kind_str()); |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
76 if !self.msg.is_empty() { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
77 write!(f, ": {}", self.msg); |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
78 } |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
79 match self.kind { |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
80 TemplogErrorKind::None => Ok(()), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
81 TemplogErrorKind::Io(ref e) => write!(f, ": {}", e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
82 TemplogErrorKind::TomlDe(ref e) => write!(f, ": {}", e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
83 TemplogErrorKind::SerdeJson(ref e) => write!(f, ": {}", e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
84 TemplogErrorKind::ParseFloat(ref e) => write!(f, ": {}", e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
85 TemplogErrorKind::Curl(ref e) => write!(f, ": {}", e), |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
86 }; |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
87 Ok(()) |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
88 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
89 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
90 |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
91 impl TemplogError { |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
92 pub fn new(msg: &str) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
93 TemplogError::new_kind(msg, TemplogErrorKind::None) |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
94 } |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
95 |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
96 pub fn new_io(msg: &str, e: io::Error) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
97 TemplogError::new_kind(msg, TemplogErrorKind::Io(e)) |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
98 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
99 |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
100 pub fn new_toml_de(msg: &str, e: toml::de::Error) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
101 TemplogError::new_kind(msg, TemplogErrorKind::TomlDe(e)) |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
102 } |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
103 |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
104 pub fn new_parse_float(msg: &str, e: std::num::ParseFloatError) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
105 TemplogError::new_kind(msg, TemplogErrorKind::ParseFloat(e)) |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
106 } |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
107 |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
108 pub fn new_curl(msg: &str, e: curl::Error) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
109 TemplogError::new_kind(msg, TemplogErrorKind::Curl(e)) |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
110 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
111 |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
112 pub fn new_serde_json(msg: &str, e: serde_json::Error) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
113 TemplogError::new_kind(msg, TemplogErrorKind::SerdeJson(e)) |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
114 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
115 |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
116 fn new_kind(msg: &str, k: TemplogErrorKind) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
117 let mut s = TemplogError { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
118 msg: msg.to_string(), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
119 desc: String::new(), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
120 kind: k, |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
121 }; |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
122 s.desc = if s.msg.is_empty() { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
123 s.kind_str().to_string() |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
124 } else { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
125 format!("{}: {}", s.kind_str(), s.msg) |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
126 }; |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
127 s |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
128 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
129 |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
130 fn kind_str(&self) -> &str { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
131 match self.kind { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
132 TemplogErrorKind::None => "Templog Error", |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
133 TemplogErrorKind::Io(_) => "Templog IO error", |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
134 TemplogErrorKind::TomlDe(_) => "Templog toml error", |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
135 TemplogErrorKind::SerdeJson(_) => "Templog Json decode error", |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
136 TemplogErrorKind::ParseFloat(_) => "Templog parse error", |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
137 TemplogErrorKind::Curl(_) => "Templog curl http error", |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
138 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
139 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
140 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
141 |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
142 impl From<io::Error> for TemplogError { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
143 fn from(e: io::Error) -> Self { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
144 TemplogError::new_io("", e) |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
145 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
146 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
147 |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
148 impl From<toml::de::Error> for TemplogError { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
149 fn from(e: toml::de::Error) -> Self { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
150 TemplogError::new_toml_de("", e) |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
151 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
152 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
153 |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
154 impl From<std::num::ParseFloatError> for TemplogError { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
155 fn from(e: std::num::ParseFloatError) -> Self { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
156 TemplogError::new_parse_float("", e) |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
157 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
158 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
159 |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
160 impl From<curl::Error> for TemplogError { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
161 fn from(e: curl::Error) -> Self { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
162 TemplogError::new_curl("", e) |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
163 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
164 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
165 |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
166 impl From<serde_json::Error> for TemplogError { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
167 fn from(e: serde_json::Error) -> Self { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
168 TemplogError::new_serde_json("", e) |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
169 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
170 } |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
171 |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
172 /// Call closures with a rate limit. Useful for log message ratelimiting |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
173 #[derive(Clone)] |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
174 pub struct NotTooOften { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
175 last: Cell<Instant>, |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
176 limit: Duration, |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
177 } |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
178 |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
179 impl NotTooOften { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
180 pub fn new(limit_secs: u64) -> Self { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
181 NotTooOften { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
182 limit: Duration::new(limit_secs, 0), |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
183 last: Cell::new(Instant::now() - Duration::new(limit_secs+1, 0)), |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
184 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
185 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
186 |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
187 pub fn and_then<F>(&self, op: F) |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
188 where F: Fn() { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
189 let now = Instant::now(); |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
190 if now - self.last.get() > self.limit { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
191 self.last.set(now); |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
192 op(); |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
193 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
194 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
195 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
196 |
610 | 197 struct Period { |
198 start: Instant, | |
199 end: Option<Instant>, | |
200 } | |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
201 |
610 | 202 pub struct StepIntegrator { |
203 on_periods: Vec<Period>, | |
204 limit: Duration, | |
205 } | |
206 | |
207 impl StepIntegrator { | |
208 pub fn new(limit: Duration) -> Self { | |
209 StepIntegrator { | |
210 on_periods: Vec::new(), | |
211 limit: limit, | |
212 } | |
213 } | |
214 | |
215 pub fn turn(&mut self, on: bool) { | |
216 self.trim(); | |
217 | |
218 if self.on_periods.is_empty() { | |
219 self.on_periods.push( Period { start: Instant::now(), end: None }); | |
220 return; | |
221 } | |
222 | |
223 let current_on = self.on_periods.last().unwrap().end.is_none(); | |
224 if on == current_on { | |
225 // state is unchanged | |
226 return; | |
227 } | |
228 | |
229 if on { | |
230 self.on_periods.push( Period { start: Instant::now(), end: None }); | |
231 } else { | |
232 self.on_periods.last_mut().unwrap().end = Some(Instant::now()); | |
233 } | |
234 } | |
235 | |
236 pub fn set_limit(&mut self, limit: Duration) { | |
237 self.limit = limit; | |
238 self.trim(); | |
239 } | |
240 | |
241 fn integrate(&self) -> Duration { | |
242 let durs = self.on_periods.iter().map(|p| { | |
243 let end = p.end.unwrap_or_else(|| Instant::now()); | |
244 end - p.start | |
245 }); | |
246 // TODO rust 1.16: impl Sum for Duration | |
247 // durs.sum() | |
248 durs.fold(Duration::new(0,0), |acc, d| acc + d) | |
249 } | |
250 | |
251 fn trim(&mut self) { | |
252 let begin = Instant::now() - self.limit; | |
253 | |
254 self.on_periods.retain(|p| { | |
255 // remove expired endtimes | |
256 if let Some(e) = p.end { | |
257 e >= begin && e != p.start | |
258 } else { | |
259 true | |
260 } | |
261 }); | |
262 | |
263 for p in &mut self.on_periods { | |
264 p.start = cmp::max(p.start, begin); | |
265 } | |
266 } | |
267 } | |
268 | |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
269 /// Takes a stream and returns a stream without errors. |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
270 pub fn consume_errors<S>(s: S) -> Box<Stream<Item=S::Item, Error=S::Error>> |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
271 // XXX not sure why 'static is really necessary here? |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
272 where |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
273 S: Stream+Send+'static, |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
274 <S as Stream>::Error: std::fmt::Display+Send+'static, |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
275 <S as Stream>::Item: Send+'static, |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
276 { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
277 s.then(|r| { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
278 match r { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
279 Ok(v) => Ok(Some(v)), |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
280 Err(e) => { |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
281 debug!("Stream error: {}", e); |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
282 Ok(None) |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
283 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
284 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
285 }) |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
286 .filter_map(|p| p).boxed() |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
287 } |