comparison rust/src/types.rs @ 604:278f1002b5c7 rust

sensor regex, custom error type
author Matt Johnston <matt@ucc.asn.au>
date Thu, 16 Feb 2017 23:53:46 +0800
parents b45b8b4cf0f5
children 7bda01659426
comparison
equal deleted inserted replaced
603:b45b8b4cf0f5 604:278f1002b5c7
1 use std::collections::HashMap; 1 use std::collections::HashMap;
2 use std::time::Duration; 2 use std::time::Duration;
3 use std::error::Error;
4 use std::fmt;
5
3 use serde::{Deserialize,Serialize}; 6 use serde::{Deserialize,Serialize};
4 7
5 #[derive(Deserialize, Serialize, Debug)] 8 #[derive(Deserialize, Serialize, Debug)]
6 pub struct Params { 9 pub struct Params {
7 pub fridge_setpoint: f32, 10 pub fridge_setpoint: f32,
63 pub fn get_temp(&self, name: &str) -> Option<f32> { 66 pub fn get_temp(&self, name: &str) -> Option<f32> {
64 self.temps.get(name).map(|f| *f) 67 self.temps.get(name).map(|f| *f)
65 } 68 }
66 } 69 }
67 70
71 #[derive(Debug)]
72 pub struct TemplogError {
73 desc: String,
74 }
75
76 impl Error for TemplogError {
77 fn description(&self) -> &str { &self.desc }
78 fn cause(&self) -> Option<&Error> { None }
79 }
80
81 impl fmt::Display for TemplogError {
82 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83 write!(f, "TemplogError: {}", self.desc);
84 Ok(())
85 }
86
87
88 }
89
90 impl TemplogError {
91 pub fn new(desc: &str) -> Self {
92 TemplogError {
93 desc: desc.to_string(),
94 }
95 }
96 }
97
98
99