Skip to content

Commit

Permalink
remove trait from last_trade and last_quote
Browse files Browse the repository at this point in the history
  • Loading branch information
sbeam committed Nov 15, 2022
1 parent 4e424a8 commit 20d428f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 77 deletions.
37 changes: 0 additions & 37 deletions src/data/v2/historical_data.rs

This file was deleted.

31 changes: 14 additions & 17 deletions src/data/v2/last_quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -74,6 +70,16 @@ impl<'a> HistoricalData<'a> for Quote {
symbol: symbol.to_string(),
}
}

fn parse(body: &[u8]) -> Result<Vec<Quote>, serde_json::Error> {
from_json::<LastQuoteResponse>(body).map(|response| {
response
.quotes
.into_iter()
.map(|(sym, point)| Quote::from(&sym, point))
.collect()
})
}
}

/// fields for individual data points in the response JSON
Expand All @@ -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<String, QuoteDataPoint>,
}

impl HistoricalDataResponse<QuoteDataPoint> for LatestQuoteResponse {
fn items<'de>(&self) -> &HashMap<String, QuoteDataPoint>
where
Self: 'de,
{
&self.quotes
}
}

EndpointNoParse! {
/// The representation of a GET request to the
/// /v2/stocks/quotes/latest endpoint.
Expand Down
37 changes: 17 additions & 20 deletions src/data/v2/last_trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Vec<Trade>, serde_json::Error> {
from_json::<LastTradeResponse>(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<Utc>,
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<String, TradeDataPoint>,
}

impl HistoricalDataResponse<TradeDataPoint> for LastTradeResponse {
fn items<'de>(&self) -> &HashMap<String, TradeDataPoint>
where
Self: 'de,
{
&self.trades
}
}

EndpointNoParse! {
/// The representation of a GET request to the
/// /v2/stocks/trades/latest endpoint.
Expand Down
3 changes: 0 additions & 3 deletions src/data/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 20d428f

Please sign in to comment.