finql_data/
quote.rs

1///! Implementation of a container for basic asset data
2use std::cmp::Ordering;
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6use crate::currency::Currency;
7use super::{DataError, DataItem};
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct Ticker {
11    pub id: Option<usize>,
12    pub asset: usize,
13    pub name: String,
14    pub currency: Currency,
15    pub source: String,
16    pub priority: i32,
17    pub factor: f64,
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct Quote {
22    pub id: Option<usize>,
23    pub ticker: usize,
24    pub price: f64,
25    pub time: DateTime<Utc>,
26    pub volume: Option<f64>,
27}
28
29impl Ord for Quote {
30    fn cmp(&self, other: &Self) -> Ordering {
31        (self.time, &self.ticker).cmp(&(other.time, &other.ticker))
32    }
33}
34
35impl PartialOrd for Quote {
36    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
37        Some(self.cmp(other))
38    }
39}
40
41impl PartialEq for Quote {
42    fn eq(&self, other: &Self) -> bool {
43        (self.time, &self.ticker) == (other.time, &other.ticker)
44    }
45}
46
47impl Eq for Quote { }
48
49
50impl DataItem for Quote {
51    // get id or return error if id hasn't been set yet
52    fn get_id(&self) -> Result<usize, DataError> {
53        match self.id {
54            Some(id) => Ok(id),
55            None => Err(DataError::DataAccessFailure(
56                "tried to get id of temporary quote".to_string(),
57            )),
58        }
59    }
60    // set id or return error if id has already been set
61    fn set_id(&mut self, id: usize) -> Result<(), DataError> {
62        match self.id {
63            Some(_) => Err(DataError::DataAccessFailure(
64                "tried to change valid quote id".to_string(),
65            )),
66            None => {
67                self.id = Some(id);
68                Ok(())
69            }
70        }
71    }
72}
73
74impl DataItem for Ticker {
75    // get id or return error if id hasn't been set yet
76    fn get_id(&self) -> Result<usize, DataError> {
77        match self.id {
78            Some(id) => Ok(id),
79            None => Err(DataError::DataAccessFailure(
80                "tried to get id of temporary ticker".to_string(),
81            )),
82        }
83    }
84    // set id or return error if id has already been set
85    fn set_id(&mut self, id: usize) -> Result<(), DataError> {
86        match self.id {
87            Some(_) => Err(DataError::DataAccessFailure(
88                "tried to change valid ticker id".to_string(),
89            )),
90            None => {
91                self.id = Some(id);
92                Ok(())
93            }
94        }
95    }
96}