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