diff --git a/src/data/v2/historical_data.rs b/src/data/v2/historical_data.rs deleted file mode 100644 index 91ffc6aa..00000000 --- a/src/data/v2/historical_data.rs +++ /dev/null @@ -1,37 +0,0 @@ -use serde::Deserialize; -use serde_json::from_slice as from_json; -use std::collections::HashMap; - -/// A struct that represents the response from the API and can retreive generic 'items' -pub trait HistoricalDataResponse { - /// should return the top-level generic 'item' from the response - fn items<'de>(&self) -> &HashMap - where - D: Deserialize<'de>; -} - -/// Trait to implement parsing various historical data for multiple symbols -pub trait HistoricalData<'a> { - /// A struct that represents the response from the API and can retreive generic 'items' - type Response: HistoricalDataResponse; - /// An individual data item from the response - type Point: Deserialize<'a>; - - /// return an instance of our data from a response datapoint - fn from_point(symbol: &str, point: &Self::Point) -> Self; - - /// Parse historical data from a response - fn parse<'de>(body: &'de [u8]) -> Result, serde_json::Error> - where - Self: Sized, - >::Response: Deserialize<'de>, - { - from_json::(body).map(|response| { - response - .items() - .into_iter() - .map(|(sym, point)| Self::from_point(&sym, point)) - .collect() - }) - } -} diff --git a/src/data/v2/last_quote.rs b/src/data/v2/last_quote.rs index 1500ba4d..d3d62451 100644 --- a/src/data/v2/last_quote.rs +++ b/src/data/v2/last_quote.rs @@ -12,7 +12,6 @@ use serde_json::from_slice as from_json; use serde_urlencoded::to_string as to_query; use std::collections::HashMap; -use crate::data::v2::historical_data::{HistoricalData, HistoricalDataResponse}; use crate::data::v2::Feed; use crate::data::DATA_BASE_URL; use crate::Str; @@ -60,12 +59,9 @@ pub struct Quote { pub symbol: String, } -impl<'a> HistoricalData<'a> for Quote { - type Response = LatestQuoteResponse; - type Point = QuoteDataPoint; - - fn from_point(symbol: &str, point: &Self::Point) -> Self { - Quote { +impl Quote { + fn from(symbol: &str, point: QuoteDataPoint) -> Self { + Self { time: point.t, ask_price: point.ap.clone(), ask_size: point.r#as, @@ -74,6 +70,16 @@ impl<'a> HistoricalData<'a> for Quote { symbol: symbol.to_string(), } } + + fn parse(body: &[u8]) -> Result, serde_json::Error> { + from_json::(body).map(|response| { + response + .quotes + .into_iter() + .map(|(sym, point)| Quote::from(&sym, point)) + .collect() + }) + } } /// fields for individual data points in the response JSON @@ -88,19 +94,10 @@ pub struct QuoteDataPoint { /// A representation of the JSON data in the response #[derive(Debug, Deserialize)] -pub struct LatestQuoteResponse { +pub struct LastQuoteResponse { quotes: HashMap, } -impl HistoricalDataResponse for LatestQuoteResponse { - fn items<'de>(&self) -> &HashMap - where - Self: 'de, - { - &self.quotes - } -} - EndpointNoParse! { /// The representation of a GET request to the /// /v2/stocks/quotes/latest endpoint. diff --git a/src/data/v2/last_trade.rs b/src/data/v2/last_trade.rs index 1da47ca6..2c2914da 100644 --- a/src/data/v2/last_trade.rs +++ b/src/data/v2/last_trade.rs @@ -12,7 +12,6 @@ use serde_json::from_slice as from_json; use serde_urlencoded::to_string as to_query; use std::collections::HashMap; -use crate::data::v2::historical_data::{HistoricalData, HistoricalDataResponse}; use crate::data::v2::Feed; use crate::data::DATA_BASE_URL; use crate::Str; @@ -56,43 +55,41 @@ pub struct Trade { pub symbol: String, } -impl<'a> HistoricalData<'a> for Trade { - type Response = LastTradeResponse; - type Point = TradeDataPoint; - - fn from_point(symbol: &str, point: &Self::Point) -> Self { - Trade { +impl Trade { + fn from(symbol: &str, point: TradeDataPoint) -> Self { + Self { time: point.t, - price: point.p.clone(), + price: point.p, size: point.s, symbol: symbol.to_string(), } } + + fn parse(body: &[u8]) -> Result, serde_json::Error> { + from_json::(body).map(|response| { + response + .trades + .into_iter() + .map(|(sym, point)| Trade::from(&sym, point)) + .collect() + }) + } } /// fields for individual data points in the response JSON #[derive(Clone, Debug, Deserialize)] -pub struct TradeDataPoint { +struct TradeDataPoint { t: DateTime, p: Num, s: u64, } /// A representation of the JSON data in the response -#[derive(Debug, Deserialize)] -pub struct LastTradeResponse { +#[derive(Deserialize)] +struct LastTradeResponse { trades: HashMap, } -impl HistoricalDataResponse for LastTradeResponse { - fn items<'de>(&self) -> &HashMap - where - Self: 'de, - { - &self.trades - } -} - EndpointNoParse! { /// The representation of a GET request to the /// /v2/stocks/trades/latest endpoint. diff --git a/src/data/v2/mod.rs b/src/data/v2/mod.rs index c62591b4..afc1a457 100644 --- a/src/data/v2/mod.rs +++ b/src/data/v2/mod.rs @@ -15,7 +15,4 @@ pub mod quotes; /// Definitions for real-time streaming of market data. pub mod stream; -/// Trait for deserializing various historical data multi endpoints -pub mod historical_data; - pub use feed::Feed;