From 40b97045f0a59846c1508a9657c0b72f4bd20a39 Mon Sep 17 00:00:00 2001 From: Peter Date: Mon, 28 Mar 2016 20:58:59 +0100 Subject: [PATCH] Reorganised price info lookup This helped stop my eyes from hurting. Further simplification --- parity/price_info.rs | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/parity/price_info.rs b/parity/price_info.rs index 1e0c2bfdc..29e7505ee 100644 --- a/parity/price_info.rs +++ b/parity/price_info.rs @@ -11,29 +11,19 @@ pub struct PriceInfo { impl PriceInfo { pub fn get() -> Option { let mut body = String::new(); - // TODO: actually bother dicking around with the errors and make it proper. - match match match Client::new() + // TODO: Handle each error type properly + Client::new() .get("http://api.etherscan.io/api?module=stats&action=ethprice") .header(Connection::close()) - .send() { - Err(_) => { return None; }, - Ok(mut s) => s.read_to_string(&mut body), - } { - Err(_) => { return None; }, - _ => { Json::from_str(&body) } - } { - Err(_) => { return None; }, - Ok(json) => { - let ethusd: f32 = if let Some(&Json::String(ref s)) = json.find_path(&["result", "ethusd"]) { - FromStr::from_str(&s).unwrap() - } else { - return None; - }; - Some(PriceInfo { - ethusd: ethusd, - }) - } - } + .send().ok() + .and_then(|mut s| s.read_to_string(&mut body).ok()) + .and_then(|_| Json::from_str(&body).ok()) + .and_then(|json| json.find_path(&["result", "ethusd"]) + .and_then(|obj| match obj { + &Json::String(ref s) => Some(PriceInfo { + ethusd: FromStr::from_str(&s).unwrap() + }), + _ => None + })) } } -