Mercurial > templog
annotate 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 |
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; |
633 | 8 |
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 |
626 | 11 use hyper; |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
12 use serde_json; |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
13 |
634 | 14 #[derive(Debug,Clone)] |
592 | 15 pub struct Readings { |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
596
diff
changeset
|
16 pub temps: HashMap<String, f32>, |
592 | 17 } |
18 | |
19 impl Readings { | |
20 pub fn new() -> Readings { | |
21 Readings { | |
594
aff50ee77252
rust working better now with streams and sinks.
Matt Johnston <matt@ucc.asn.au>
parents:
593
diff
changeset
|
22 temps: HashMap::new(), |
592 | 23 } |
24 } | |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
25 |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
596
diff
changeset
|
26 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
|
27 self.temps.insert(name.to_string(), v); |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
28 } |
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
29 |
603
b45b8b4cf0f5
get rid of lazy_static, config is passed around
Matt Johnston <matt@ucc.asn.au>
parents:
596
diff
changeset
|
30 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
|
31 self.temps.get(name).map(|f| *f) |
592 | 32 } |
33 } | |
34 | |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
35 #[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
|
36 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
|
37 None, |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
38 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
|
39 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
|
40 SerdeJson(serde_json::Error), |
626 | 41 Hyper(hyper::Error), |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
42 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
43 |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
44 #[derive(Debug)] |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
45 pub struct TemplogError { |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
46 msg: String, |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
47 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
|
48 kind: TemplogErrorKind, |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
49 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
50 |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
51 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
|
52 fn description(&self) -> &str { |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
53 &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
|
54 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
55 |
633 | 56 fn cause(&self) -> Option<&dyn Error> { |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
57 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
|
58 TemplogErrorKind::None => None, |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
59 TemplogErrorKind::Io(ref e) => Some(e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
60 TemplogErrorKind::ParseFloat(ref e) => Some(e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
61 TemplogErrorKind::SerdeJson(ref e) => Some(e), |
626 | 62 TemplogErrorKind::Hyper(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
|
63 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
64 } |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
65 |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
66 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
67 |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
68 impl fmt::Display for TemplogError { |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
69 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
634 | 70 write!(f, "{}", self.kind_str())?; |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
71 if !self.msg.is_empty() { |
634 | 72 write!(f, ": {}", self.msg)?; |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
73 } |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
74 match self.kind { |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
75 TemplogErrorKind::None => Ok(()), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
76 TemplogErrorKind::Io(ref e) => write!(f, ": {}", e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
77 TemplogErrorKind::SerdeJson(ref e) => write!(f, ": {}", e), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
78 TemplogErrorKind::ParseFloat(ref e) => write!(f, ": {}", e), |
626 | 79 TemplogErrorKind::Hyper(ref e) => write!(f, ": {}", e), |
634 | 80 }?; |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
81 Ok(()) |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
82 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
83 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
84 |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
85 impl TemplogError { |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
86 pub fn new(msg: &str) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
87 TemplogError::new_kind(msg, TemplogErrorKind::None) |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
88 } |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
89 |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
90 pub fn new_io(msg: &str, e: io::Error) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
91 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
|
92 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
93 |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
94 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
|
95 TemplogError::new_kind(msg, TemplogErrorKind::ParseFloat(e)) |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
96 } |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
97 |
626 | 98 pub fn new_hyper(msg: &str, e: hyper::Error) -> Self { |
99 TemplogError::new_kind(msg, TemplogErrorKind::Hyper(e)) | |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
100 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
101 |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
102 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
|
103 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
|
104 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
105 |
623 | 106 pub fn kind(&self) -> &TemplogErrorKind { |
107 return &self.kind; | |
108 } | |
109 | |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
110 fn new_kind(msg: &str, k: TemplogErrorKind) -> Self { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
111 let mut s = TemplogError { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
112 msg: msg.to_string(), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
113 desc: String::new(), |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
114 kind: k, |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
115 }; |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
116 s.desc = if s.msg.is_empty() { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
117 s.kind_str().to_string() |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
118 } else { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
119 format!("{}: {}", s.kind_str(), s.msg) |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
120 }; |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
121 s |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
122 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
123 |
612
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
124 fn kind_str(&self) -> &str { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
125 match self.kind { |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
126 TemplogErrorKind::None => "Templog Error", |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
127 TemplogErrorKind::Io(_) => "Templog IO error", |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
128 TemplogErrorKind::SerdeJson(_) => "Templog Json decode error", |
5fc41e0833b4
fix TemplogError references
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
129 TemplogErrorKind::ParseFloat(_) => "Templog parse error", |
626 | 130 TemplogErrorKind::Hyper(_) => "Templog http error", |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
131 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
132 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
133 } |
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
134 |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
135 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
|
136 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
|
137 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
|
138 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
139 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
140 |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
141 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
|
142 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
|
143 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
|
144 } |
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 |
626 | 147 impl From<hyper::Error> for TemplogError { |
148 fn from(e: hyper::Error) -> Self { | |
149 TemplogError::new_hyper("", e) | |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
150 } |
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 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
|
154 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
|
155 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
|
156 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
610
diff
changeset
|
157 } |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
158 |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
159 /// 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
|
160 #[derive(Clone)] |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
161 pub struct NotTooOften { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
162 last: Cell<Instant>, |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
163 limit: Duration, |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
164 } |
604
278f1002b5c7
sensor regex, custom error type
Matt Johnston <matt@ucc.asn.au>
parents:
603
diff
changeset
|
165 |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
166 impl NotTooOften { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
167 pub fn new(limit_secs: u64) -> Self { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
168 NotTooOften { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
169 limit: Duration::new(limit_secs, 0), |
634 | 170 // XXX why +1? |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
171 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
|
172 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
173 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
174 |
634 | 175 pub fn and_then<F, U>(&self, op: F) -> Option<U> |
176 where F: Fn() -> U { | |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
177 let now = Instant::now(); |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
178 if now - self.last.get() > self.limit { |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
179 self.last.set(now); |
634 | 180 Some(op()) |
181 } else { | |
182 None | |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
183 } |
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 |
610 | 187 struct Period { |
188 start: Instant, | |
189 end: Option<Instant>, | |
190 } | |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
604
diff
changeset
|
191 |
610 | 192 pub struct StepIntegrator { |
193 on_periods: Vec<Period>, | |
194 limit: Duration, | |
195 } | |
196 | |
197 impl StepIntegrator { | |
198 pub fn new(limit: Duration) -> Self { | |
199 StepIntegrator { | |
200 on_periods: Vec::new(), | |
201 limit: limit, | |
202 } | |
203 } | |
204 | |
205 pub fn turn(&mut self, on: bool) { | |
206 self.trim(); | |
207 | |
208 if self.on_periods.is_empty() { | |
209 self.on_periods.push( Period { start: Instant::now(), end: None }); | |
210 return; | |
211 } | |
212 | |
213 let current_on = self.on_periods.last().unwrap().end.is_none(); | |
214 if on == current_on { | |
215 // state is unchanged | |
216 return; | |
217 } | |
218 | |
219 if on { | |
220 self.on_periods.push( Period { start: Instant::now(), end: None }); | |
221 } else { | |
222 self.on_periods.last_mut().unwrap().end = Some(Instant::now()); | |
223 } | |
224 } | |
225 | |
226 pub fn set_limit(&mut self, limit: Duration) { | |
227 self.limit = limit; | |
228 self.trim(); | |
229 } | |
230 | |
621 | 231 pub fn integrate(&self) -> Duration { |
610 | 232 let durs = self.on_periods.iter().map(|p| { |
233 let end = p.end.unwrap_or_else(|| Instant::now()); | |
234 end - p.start | |
235 }); | |
617 | 236 durs.sum() |
610 | 237 } |
238 | |
239 fn trim(&mut self) { | |
240 let begin = Instant::now() - self.limit; | |
241 | |
242 self.on_periods.retain(|p| { | |
243 // remove expired endtimes | |
244 if let Some(e) = p.end { | |
245 e >= begin && e != p.start | |
246 } else { | |
247 true | |
248 } | |
249 }); | |
250 | |
251 for p in &mut self.on_periods { | |
252 p.start = cmp::max(p.start, begin); | |
253 } | |
254 } | |
255 } | |
256 |