diff --git a/1--transforming-sync-to-async/provided-code/Cargo.lock b/1--transforming-sync-to-async/provided-code/Cargo.lock index 474427b..3d6b30c 100644 --- a/1--transforming-sync-to-async/provided-code/Cargo.lock +++ b/1--transforming-sync-to-async/provided-code/Cargo.lock @@ -423,12 +423,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8b7abd5d659d9b90c8cba917f6ec750a74e2dc23902ef9cd4cc8c8b22e6036a" dependencies = [ "futures-core", - "futures-io", "futures-task", - "memchr", "pin-project-lite", "pin-utils", - "slab", ] [[package]] diff --git a/1--transforming-sync-to-async/provided-code/Cargo.toml b/1--transforming-sync-to-async/provided-code/Cargo.toml index e2fe94a..0ae30cc 100644 --- a/1--transforming-sync-to-async/provided-code/Cargo.toml +++ b/1--transforming-sync-to-async/provided-code/Cargo.toml @@ -9,5 +9,5 @@ version = "0.1.0" [dependencies] chrono = { version = "0.4", features = ["serde"] } clap = {version = "3.1.8", features = ["derive"]} -yahoo_finance_api = { version = "1.1", features = ["blocking"] } +yahoo_finance_api = "1.1" async-std = { version = "1.6", features = ["attributes", "tokio1"] } diff --git a/1--transforming-sync-to-async/provided-code/src/main.rs b/1--transforming-sync-to-async/provided-code/src/main.rs index eda7d33..c2d8ed9 100644 --- a/1--transforming-sync-to-async/provided-code/src/main.rs +++ b/1--transforming-sync-to-async/provided-code/src/main.rs @@ -20,7 +20,6 @@ struct Opts { /// A trait to provide a common interface for all signal calculations. /// trait AsyncStockSignal { - /// /// The signal's data type. /// @@ -97,7 +96,7 @@ fn min(series: &[f64]) -> Option { /// /// Retrieve data from a data source and extract the closing prices. Errors during download are mapped onto io::Errors as InvalidData. /// -fn fetch_closing_data( +async fn fetch_closing_data( symbol: &str, beginning: &DateTime, end: &DateTime, @@ -106,6 +105,7 @@ fn fetch_closing_data( let response = provider .get_quote_history(symbol, *beginning, *end) + .await .map_err(|_| Error::from(ErrorKind::InvalidData))?; let mut quotes = response .quotes() @@ -127,14 +127,14 @@ async fn main() -> std::io::Result<()> { // a simple way to output a CSV header println!("period start,symbol,price,change %,min,max,30d avg"); for symbol in opts.symbols.split(',') { - let closes = fetch_closing_data(&symbol, &from, &to)?; + let closes = fetch_closing_data(&symbol, &from, &to).await?; if !closes.is_empty() { - // min/max of the period. unwrap() because those are Option types - let period_max: f64 = max(&closes).unwrap(); - let period_min: f64 = min(&closes).unwrap(); - let last_price = *closes.last().unwrap_or(&0.0); - let (_, pct_change) = price_diff(&closes).unwrap_or((0.0, 0.0)); - let sma = n_window_sma(30, &closes).unwrap_or_default(); + // min/max of the period. unwrap() because those are Option types + let period_max: f64 = max(&closes).unwrap(); + let period_min: f64 = min(&closes).unwrap(); + let last_price = *closes.last().unwrap_or(&0.0); + let (_, pct_change) = price_diff(&closes).unwrap_or((0.0, 0.0)); + let sma = n_window_sma(30, &closes).unwrap_or_default(); // a simple way to output CSV data println!(