trading_charts/data/
series.rs1use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
2use serde::{Deserialize, Serialize};
3use serde_wasm_bindgen::to_value;
4
5#[wasm_bindgen]
6extern "C" {
7 #[wasm_bindgen(js_namespace = Math)]
8 fn random() -> f64;
9}
10
11#[derive(Serialize, Deserialize)]
12pub struct Series<Dat: Serialize + Clone, Opt: Serialize + Clone> {
13 id: Option<String>,
14 r#type: String,
15 data: Vec<Dat>,
16 options: Option<Opt>,
17}
18
19impl<Dat: Serialize + Clone, Opt: Serialize + Clone> Series<Dat, Opt> {
20 pub fn new(r#type: impl AsRef<str>) -> Self {
21 Self {
22 id: None,
23 r#type: r#type.as_ref().to_string(),
24 data: Vec::new(),
25 options: None,
26 }
27 }
28
29 pub fn id(&self) -> Option<&String> {
30 self.id.as_ref()
31 }
32
33 pub fn set_id(&mut self, id: String) {
34 self.id.replace(id);
35 }
36
37 #[allow(dead_code)]
38 pub fn get_type(&self) -> &str {
39 &self.r#type
40 }
41
42 #[allow(dead_code)]
43 pub fn options(&self) -> Option<&Opt> {
44 self.options.as_ref()
45 }
46
47 #[allow(dead_code)]
48 pub fn options_mut(&mut self) -> Option<&mut Opt> {
49 self.options.as_mut()
50 }
51
52 pub fn set_options(&mut self, options: Opt) {
53 self.options.replace(options);
54 }
55
56 #[allow(dead_code)]
57 pub fn data(&self) -> &Vec<Dat> {
58 &self.data
59 }
60
61 #[allow(dead_code)]
62 pub fn data_mut(&mut self) -> &mut Vec<Dat> {
63 &mut self.data
64 }
65
66 #[allow(dead_code)]
67 pub fn get_data(&self, index: usize) -> Option<&Dat> {
68 self.data.get(index)
69 }
70
71 #[allow(dead_code)]
72 pub fn get_data_mut(&mut self, index: usize) -> Option<&mut Dat> {
73 self.data.get_mut(index)
74 }
75
76 #[allow(dead_code)]
77 pub fn set_data(&mut self, data: Vec<Dat>) {
78 self.data = data;
79 }
80
81 #[allow(dead_code)]
82 pub fn push_data(&mut self, data: Dat) {
83 self.data.push(data);
84 }
85
86 #[allow(dead_code)]
87 pub fn remove_data(&mut self, index: usize) {
88 self.data.remove(index);
89 }
90
91 #[allow(dead_code)]
92 pub fn clear_data(&mut self) {
93 self.data.clear();
94 }
95
96 #[allow(dead_code)]
97 pub fn update_data(&mut self, index: usize, data: Dat) -> bool {
98 self.data.get_mut(index).map(|candle| *candle = data).is_some()
99 }
100
101 pub fn to_value(&self) -> Result<JsValue, JsValue> {
102 Ok(to_value(self)?)
103 }
104}