comparison rust/src/types.rs @ 612:5fc41e0833b4 rust

fix TemplogError references
author Matt Johnston <matt@ucc.asn.au>
date Thu, 02 Mar 2017 00:06:03 +0800
parents f3e39e2107fd
children f153aec221be
comparison
equal deleted inserted replaced
611:f3e39e2107fd 612:5fc41e0833b4
92 Curl(curl::Error), 92 Curl(curl::Error),
93 } 93 }
94 94
95 #[derive(Debug)] 95 #[derive(Debug)]
96 pub struct TemplogError { 96 pub struct TemplogError {
97 msg: String,
97 desc: String, 98 desc: String,
98 kind: TemplogErrorKind, 99 kind: TemplogErrorKind,
99 } 100 }
100 101
101 impl Error for TemplogError { 102 impl Error for TemplogError {
102 fn description(&self) -> &str { 103 fn description(&self) -> &str {
103 &format!("{}", self) 104 &self.desc
104 } 105 }
105 106
106 fn cause(&self) -> Option<&Error> { 107 fn cause(&self) -> Option<&Error> {
107 match self.kind { 108 match self.kind {
108 TemplogErrorKind::None => None, 109 TemplogErrorKind::None => None,
109 TemplogErrorKind::Io(e) => Some(&e), 110 TemplogErrorKind::Io(ref e) => Some(e),
110 TemplogErrorKind::ParseFloat(e) => Some(&e), 111 TemplogErrorKind::ParseFloat(ref e) => Some(e),
111 TemplogErrorKind::TomlDe(e) => Some(&e), 112 TemplogErrorKind::TomlDe(ref e) => Some(e),
112 TemplogErrorKind::SerdeJson(e) => Some(&e), 113 TemplogErrorKind::SerdeJson(ref e) => Some(e),
113 TemplogErrorKind::Curl(e) => Some(&e), 114 TemplogErrorKind::Curl(ref e) => Some(e),
114 } 115 }
115 } 116 }
117
116 } 118 }
117 119
118 impl fmt::Display for TemplogError { 120 impl fmt::Display for TemplogError {
119 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 121 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
122 write!(f, "{}", self.kind_str());
123 if !self.msg.is_empty() {
124 write!(f, ": {}", self.msg);
125 }
120 match self.kind { 126 match self.kind {
121 TemplogErrorKind::None => write!(f, "Templog Error: {}", self.desc), 127 TemplogErrorKind::None => Ok(()),
122 TemplogErrorKind::Io(e) => write!(f, "Templog IO error {}: {}", self.desc, e), 128 TemplogErrorKind::Io(ref e) => write!(f, ": {}", e),
123 TemplogErrorKind::TomlDe(e) => write!(f, "Templog toml error {}: {}", self.desc, e), 129 TemplogErrorKind::TomlDe(ref e) => write!(f, ": {}", e),
124 TemplogErrorKind::SerdeJson(e) => write!(f, "Json decode error {}: {}", self.desc, e), 130 TemplogErrorKind::SerdeJson(ref e) => write!(f, ": {}", e),
125 TemplogErrorKind::ParseFloat(e) => write!(f, "Templog parse error {}: {}", self.desc, e), 131 TemplogErrorKind::ParseFloat(ref e) => write!(f, ": {}", e),
126 TemplogErrorKind::Curl(e) => write!(f, "Templog curl http error {}: {}", self.desc, e), 132 TemplogErrorKind::Curl(ref e) => write!(f, ": {}", e),
127 }; 133 };
128 Ok(()) 134 Ok(())
129 } 135 }
130 } 136 }
131 137
132 impl TemplogError { 138 impl TemplogError {
133 pub fn new(desc: &str) -> Self { 139 pub fn new(msg: &str) -> Self {
134 TemplogError { 140 TemplogError::new_kind(msg, TemplogErrorKind::None)
135 desc: desc.to_string(), 141 }
136 kind: TemplogErrorKind::None, 142
137 } 143 pub fn new_io(msg: &str, e: io::Error) -> Self {
138 } 144 TemplogError::new_kind(msg, TemplogErrorKind::Io(e))
139 145 }
140 pub fn new_io(desc: &str, e: io::Error) -> Self { 146
141 TemplogError { 147 pub fn new_toml_de(msg: &str, e: toml::de::Error) -> Self {
142 desc: desc.to_string(), 148 TemplogError::new_kind(msg, TemplogErrorKind::TomlDe(e))
143 kind: TemplogErrorKind::Io(e), 149 }
144 } 150
145 } 151 pub fn new_parse_float(msg: &str, e: std::num::ParseFloatError) -> Self {
146 152 TemplogError::new_kind(msg, TemplogErrorKind::ParseFloat(e))
147 pub fn new_toml_de(desc: &str, e: toml::de::Error) -> Self { 153 }
148 TemplogError { 154
149 desc: desc.to_string(), 155 pub fn new_curl(msg: &str, e: curl::Error) -> Self {
150 kind: TemplogErrorKind::TomlDe(e), 156 TemplogError::new_kind(msg, TemplogErrorKind::Curl(e))
151 } 157 }
152 } 158
153 159 pub fn new_serde_json(msg: &str, e: serde_json::Error) -> Self {
154 pub fn new_parse_float(desc: &str, e: std::num::ParseFloatError) -> Self { 160 TemplogError::new_kind(msg, TemplogErrorKind::SerdeJson(e))
155 TemplogError { 161 }
156 desc: desc.to_string(), 162
157 kind: TemplogErrorKind::ParseFloat(e), 163 fn new_kind(msg: &str, k: TemplogErrorKind) -> Self {
158 } 164 let mut s = TemplogError {
159 } 165 msg: msg.to_string(),
160 166 desc: String::new(),
161 pub fn new_curl(desc: &str, e: curl::Error) -> Self { 167 kind: k,
162 TemplogError { 168 };
163 desc: desc.to_string(), 169 s.desc = if s.msg.is_empty() {
164 kind: TemplogErrorKind::Curl(e), 170 s.kind_str().to_string()
165 } 171 } else {
166 } 172 format!("{}: {}", s.kind_str(), s.msg)
167 173 };
168 pub fn new_serde_json(desc: &str, e: serde_json::Error) -> Self { 174 s
169 TemplogError { 175 }
170 desc: desc.to_string(), 176
171 kind: TemplogErrorKind::SerdeJson(e), 177 fn kind_str(&self) -> &str {
178 match self.kind {
179 TemplogErrorKind::None => "Templog Error",
180 TemplogErrorKind::Io(_) => "Templog IO error",
181 TemplogErrorKind::TomlDe(_) => "Templog toml error",
182 TemplogErrorKind::SerdeJson(_) => "Templog Json decode error",
183 TemplogErrorKind::ParseFloat(_) => "Templog parse error",
184 TemplogErrorKind::Curl(_) => "Templog curl http error",
172 } 185 }
173 } 186 }
174 } 187 }
175 188
176 impl From<io::Error> for TemplogError { 189 impl From<io::Error> for TemplogError {