finance_query/models/chart/
events.rs1use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7
8#[derive(Debug, Clone, Default, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub(crate) struct ChartEvents {
12 #[serde(default)]
14 pub dividends: HashMap<String, DividendEvent>,
15 #[serde(default)]
17 pub splits: HashMap<String, SplitEvent>,
18 #[serde(default)]
20 pub capital_gains: HashMap<String, CapitalGainEvent>,
21}
22
23#[derive(Debug, Clone, Serialize, Deserialize)]
25pub(crate) struct DividendEvent {
26 pub amount: f64,
28 pub date: i64,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
34#[serde(rename_all = "camelCase")]
35pub(crate) struct SplitEvent {
36 pub date: i64,
38 pub numerator: f64,
40 pub denominator: f64,
42 pub split_ratio: String,
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize)]
48pub(crate) struct CapitalGainEvent {
49 pub amount: f64,
51 pub date: i64,
53}
54
55#[non_exhaustive]
59#[derive(Debug, Clone, Serialize, Deserialize)]
60#[cfg_attr(feature = "dataframe", derive(crate::ToDataFrame))]
61pub struct Dividend {
62 pub timestamp: i64,
64 pub amount: f64,
66}
67
68#[non_exhaustive]
72#[derive(Debug, Clone, Serialize, Deserialize)]
73#[cfg_attr(feature = "dataframe", derive(crate::ToDataFrame))]
74pub struct Split {
75 pub timestamp: i64,
77 pub numerator: f64,
79 pub denominator: f64,
81 pub ratio: String,
83}
84
85#[non_exhaustive]
89#[derive(Debug, Clone, Serialize, Deserialize)]
90#[cfg_attr(feature = "dataframe", derive(crate::ToDataFrame))]
91pub struct CapitalGain {
92 pub timestamp: i64,
94 pub amount: f64,
96}
97
98impl ChartEvents {
99 pub(crate) fn to_dividends(&self) -> Vec<Dividend> {
101 let mut dividends: Vec<Dividend> = self
102 .dividends
103 .values()
104 .map(|d| Dividend {
105 timestamp: d.date,
106 amount: d.amount,
107 })
108 .collect();
109 dividends.sort_by_key(|d| d.timestamp);
110 dividends
111 }
112
113 pub(crate) fn to_splits(&self) -> Vec<Split> {
115 let mut splits: Vec<Split> = self
116 .splits
117 .values()
118 .map(|s| Split {
119 timestamp: s.date,
120 numerator: s.numerator,
121 denominator: s.denominator,
122 ratio: s.split_ratio.clone(),
123 })
124 .collect();
125 splits.sort_by_key(|s| s.timestamp);
126 splits
127 }
128
129 pub(crate) fn to_capital_gains(&self) -> Vec<CapitalGain> {
131 let mut gains: Vec<CapitalGain> = self
132 .capital_gains
133 .values()
134 .map(|g| CapitalGain {
135 timestamp: g.date,
136 amount: g.amount,
137 })
138 .collect();
139 gains.sort_by_key(|g| g.timestamp);
140 gains
141 }
142}