From 16e0ad1288c794472297254e8c4ccd1ba9a2708a Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Thu, 27 Jun 2019 17:29:24 +0800 Subject: [PATCH] updated price-info to edition 2018 (#10801) --- Cargo.lock | 1 - miner/price-info/Cargo.toml | 4 +-- miner/price-info/src/lib.rs | 69 +++++++++---------------------------- 3 files changed, 18 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7b29f113b..83d8a543b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3237,7 +3237,6 @@ dependencies = [ "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "parity-runtime 0.1.0", - "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/miner/price-info/Cargo.toml b/miner/price-info/Cargo.toml index 0b72fd37b..23f69e37c 100644 --- a/miner/price-info/Cargo.toml +++ b/miner/price-info/Cargo.toml @@ -5,14 +5,14 @@ license = "GPL-3.0" name = "price-info" version = "1.12.0" authors = ["Parity Technologies "] +edition = "2018" [dependencies] fetch = { path = "../../util/fetch" } futures = "0.1" -parity-runtime = { path = "../../util/runtime" } log = "0.4" +parity-runtime = { path = "../../util/runtime" } serde_json = "1.0" [dev-dependencies] -parking_lot = "0.7" fake-fetch = { path = "../../util/fake-fetch" } diff --git a/miner/price-info/src/lib.rs b/miner/price-info/src/lib.rs index 4117f84c5..d249704b5 100644 --- a/miner/price-info/src/lib.rs +++ b/miner/price-info/src/lib.rs @@ -18,28 +18,14 @@ //! A simple client to get the current ETH price using an external API. -extern crate futures; -extern crate serde_json; -extern crate parity_runtime; - -#[macro_use] -extern crate log; - -#[cfg(test)] -extern crate fake_fetch; - -pub extern crate fetch; - -use std::cmp; -use std::fmt; -use std::io; -use std::str; - +use std::{cmp, fmt, io, str}; use fetch::{Client as FetchClient, Fetch}; use futures::{Future, Stream}; -use futures::future::{self, Either}; -use serde_json::Value; +use log::warn; use parity_runtime::Executor; +use serde_json::Value; + +pub use fetch; /// Current ETH price information. #[derive(Debug)] @@ -48,27 +34,6 @@ pub struct PriceInfo { pub ethusd: f32, } -/// Price info error. -#[derive(Debug)] -pub enum Error { - /// The API returned an unexpected status code. - StatusCode(&'static str), - /// The API returned an unexpected status content. - UnexpectedResponse(Option), - /// There was an error when trying to reach the API. - Fetch(fetch::Error), - /// IO error when reading API response. - Io(io::Error), -} - -impl From for Error { - fn from(err: io::Error) -> Self { Error::Io(err) } -} - -impl From for Error { - fn from(err: fetch::Error) -> Self { Error::Fetch(err) } -} - /// A client to get the current ETH price using an external API. pub struct Client { pool: Executor, @@ -100,14 +65,7 @@ impl Client { /// Gets the current ETH price and calls `set_price` with the result. pub fn get(&self, set_price: G) { let future = self.fetch.get(&self.api_endpoint, fetch::Abort::default()) - .from_err() - .and_then(|response| { - if !response.is_success() { - let s = Error::StatusCode(response.status().canonical_reason().unwrap_or("unknown")); - return Either::A(future::err(s)); - } - Either::B(response.concat2().from_err()) - }) + .and_then(|response| response.concat2()) .and_then(move |body| { let body_str = str::from_utf8(&body).ok(); let value: Option = body_str.and_then(|s| serde_json::from_str(s).ok()); @@ -123,7 +81,11 @@ impl Client { set_price(PriceInfo { ethusd }); Ok(()) }, - None => Err(Error::UnexpectedResponse(body_str.map(From::from))), + None => { + let msg = format!("Unexpected response: {}", body_str.unwrap_or_default()); + let err = io::Error::new(io::ErrorKind::Other, msg); + Err(fetch::Error::Io(err)) + } } }) .map_err(|err| { @@ -135,11 +97,12 @@ impl Client { #[cfg(test)] mod test { - use std::sync::Arc; - use parity_runtime::{Runtime, Executor}; - use Client; - use std::sync::atomic::{AtomicBool, Ordering}; + use std::sync::{ + Arc, atomic::{AtomicBool, Ordering} + }; use fake_fetch::FakeFetch; + use parity_runtime::{Runtime, Executor}; + use super::Client; fn price_info_ok(response: &str, executor: Executor) -> Client> { Client::new(FakeFetch::new(Some(response.to_owned())), executor)