comparison 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
comparison
equal deleted inserted replaced
638:a9f353f488d0 639:89818a14648b
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 hyper;
12 use serde_json; 11 use serde_json;
13 12
14 #[derive(Debug,Clone)] 13 #[derive(Debug,Clone)]
15 pub struct Readings { 14 pub struct Readings {
16 pub temps: HashMap<String, f32>, 15 pub temps: HashMap<String, f32>,
36 pub enum TemplogErrorKind { 35 pub enum TemplogErrorKind {
37 None, 36 None,
38 Io(io::Error), 37 Io(io::Error),
39 ParseFloat(std::num::ParseFloatError), 38 ParseFloat(std::num::ParseFloatError),
40 SerdeJson(serde_json::Error), 39 SerdeJson(serde_json::Error),
41 Hyper(hyper::Error),
42 } 40 }
43 41
44 #[derive(Debug)] 42 #[derive(Debug)]
45 pub struct TemplogError { 43 pub struct TemplogError {
46 msg: String, 44 msg: String,
57 match self.kind { 55 match self.kind {
58 TemplogErrorKind::None => None, 56 TemplogErrorKind::None => None,
59 TemplogErrorKind::Io(ref e) => Some(e), 57 TemplogErrorKind::Io(ref e) => Some(e),
60 TemplogErrorKind::ParseFloat(ref e) => Some(e), 58 TemplogErrorKind::ParseFloat(ref e) => Some(e),
61 TemplogErrorKind::SerdeJson(ref e) => Some(e), 59 TemplogErrorKind::SerdeJson(ref e) => Some(e),
62 TemplogErrorKind::Hyper(ref e) => Some(e),
63 } 60 }
64 } 61 }
65 62
66 } 63 }
67 64
74 match self.kind { 71 match self.kind {
75 TemplogErrorKind::None => Ok(()), 72 TemplogErrorKind::None => Ok(()),
76 TemplogErrorKind::Io(ref e) => write!(f, ": {}", e), 73 TemplogErrorKind::Io(ref e) => write!(f, ": {}", e),
77 TemplogErrorKind::SerdeJson(ref e) => write!(f, ": {}", e), 74 TemplogErrorKind::SerdeJson(ref e) => write!(f, ": {}", e),
78 TemplogErrorKind::ParseFloat(ref e) => write!(f, ": {}", e), 75 TemplogErrorKind::ParseFloat(ref e) => write!(f, ": {}", e),
79 TemplogErrorKind::Hyper(ref e) => write!(f, ": {}", e),
80 }?; 76 }?;
81 Ok(()) 77 Ok(())
82 } 78 }
83 } 79 }
84 80
91 TemplogError::new_kind(msg, TemplogErrorKind::Io(e)) 87 TemplogError::new_kind(msg, TemplogErrorKind::Io(e))
92 } 88 }
93 89
94 pub fn new_parse_float(msg: &str, e: std::num::ParseFloatError) -> Self { 90 pub fn new_parse_float(msg: &str, e: std::num::ParseFloatError) -> Self {
95 TemplogError::new_kind(msg, TemplogErrorKind::ParseFloat(e)) 91 TemplogError::new_kind(msg, TemplogErrorKind::ParseFloat(e))
96 }
97
98 pub fn new_hyper(msg: &str, e: hyper::Error) -> Self {
99 TemplogError::new_kind(msg, TemplogErrorKind::Hyper(e))
100 } 92 }
101 93
102 pub fn new_serde_json(msg: &str, e: serde_json::Error) -> Self { 94 pub fn new_serde_json(msg: &str, e: serde_json::Error) -> Self {
103 TemplogError::new_kind(msg, TemplogErrorKind::SerdeJson(e)) 95 TemplogError::new_kind(msg, TemplogErrorKind::SerdeJson(e))
104 } 96 }
125 match self.kind { 117 match self.kind {
126 TemplogErrorKind::None => "Templog Error", 118 TemplogErrorKind::None => "Templog Error",
127 TemplogErrorKind::Io(_) => "Templog IO error", 119 TemplogErrorKind::Io(_) => "Templog IO error",
128 TemplogErrorKind::SerdeJson(_) => "Templog Json decode error", 120 TemplogErrorKind::SerdeJson(_) => "Templog Json decode error",
129 TemplogErrorKind::ParseFloat(_) => "Templog parse error", 121 TemplogErrorKind::ParseFloat(_) => "Templog parse error",
130 TemplogErrorKind::Hyper(_) => "Templog http error",
131 } 122 }
132 } 123 }
133 } 124 }
134 125
135 impl From<io::Error> for TemplogError { 126 impl From<io::Error> for TemplogError {
139 } 130 }
140 131
141 impl From<std::num::ParseFloatError> for TemplogError { 132 impl From<std::num::ParseFloatError> for TemplogError {
142 fn from(e: std::num::ParseFloatError) -> Self { 133 fn from(e: std::num::ParseFloatError) -> Self {
143 TemplogError::new_parse_float("", e) 134 TemplogError::new_parse_float("", e)
144 }
145 }
146
147 impl From<hyper::Error> for TemplogError {
148 fn from(e: hyper::Error) -> Self {
149 TemplogError::new_hyper("", e)
150 } 135 }
151 } 136 }
152 137
153 impl From<serde_json::Error> for TemplogError { 138 impl From<serde_json::Error> for TemplogError {
154 fn from(e: serde_json::Error) -> Self { 139 fn from(e: serde_json::Error) -> Self {