ig_client/application/services/
types.rs1use crate::application::models::market::{MarketData, MarketNode};
2use crate::error::AppError;
3use crate::impl_json_display;
4use crate::presentation::InstrumentType;
5use chrono::{DateTime, Utc};
6use serde::{Deserialize, Serialize};
7
8pub type ListenerResult = Result<(), AppError>;
10
11#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, Default)]
26pub struct DBEntry {
27 pub symbol: String,
29 pub epic: String,
31 pub name: String,
33 pub instrument_type: InstrumentType,
35 pub exchange: String,
37 pub expiry: String,
39 pub last_update: DateTime<Utc>,
41}
42
43impl_json_display!(DBEntry);
44
45impl From<MarketNode> for DBEntry {
46 fn from(value: MarketNode) -> Self {
47 let mut entry = DBEntry::default();
48 if !value.markets.is_empty() {
49 let market = &value.markets[0];
50 entry.symbol = market
51 .epic
52 .split('.')
53 .nth(2)
54 .unwrap_or_default()
55 .to_string();
56 entry.epic = market.epic.clone();
57 entry.name = market.instrument_name.clone();
58 entry.instrument_type = market.instrument_type;
59 entry.exchange = "IG".to_string();
60 entry.expiry = market.expiry.clone();
61 entry.last_update = Utc::now();
62 }
63 entry
64 }
65}
66
67impl From<MarketData> for DBEntry {
68 fn from(market: MarketData) -> Self {
69 DBEntry {
70 symbol: market
71 .epic
72 .split('.')
73 .nth(2)
74 .unwrap_or_default()
75 .to_string(),
76 epic: market.epic.clone(),
77 name: market.instrument_name.clone(),
78 instrument_type: market.instrument_type,
79 exchange: "IG".to_string(),
80 expiry: market.expiry.clone(),
81 last_update: Utc::now(),
82 }
83 }
84}
85
86impl From<&MarketNode> for DBEntry {
87 fn from(value: &MarketNode) -> Self {
88 DBEntry::from(value.clone())
89 }
90}
91
92impl From<&MarketData> for DBEntry {
93 fn from(market: &MarketData) -> Self {
94 DBEntry::from(market.clone())
95 }
96}