twelvedata/reference/
indices.rs

1use std::error::Error;
2
3use serde::Deserialize;
4
5use crate::internal;
6
7#[derive(Deserialize, Debug)]
8pub struct Indices {
9    pub data: Vec<Index>,
10    pub status: String,
11
12    #[serde(skip)]
13    symbol: String,
14    #[serde(skip)]
15    exchange: String,
16    #[serde(skip)]
17    mic_code: String,
18    #[serde(skip)]
19    country: String,
20    #[serde(skip)]
21    include_delisted: bool,
22}
23
24#[derive(Deserialize, Debug)]
25pub struct Index {
26    pub symbol: String,
27    pub name: String,
28    pub country: String,
29    pub currency: String,
30    pub exchange: String,
31    pub mic_code: String,
32}
33
34impl Indices {
35    pub fn builder() -> Self {
36        Indices {
37            data: Vec::new(),
38            status: String::new(),
39            symbol: String::new(),
40            exchange: String::new(),
41            mic_code: String::new(),
42            country: String::new(),
43            include_delisted: false,
44        }
45    }
46
47    pub fn symbol(&mut self, symbol: &str) -> &mut Self {
48        self.symbol = symbol.to_string();
49        self
50    }
51
52    pub fn exchange(&mut self, exchange: &str) -> &mut Self {
53        self.exchange = exchange.to_string();
54        self
55    }
56
57    pub fn mic_code(&mut self, mic_code: &str) -> &mut Self {
58        self.mic_code = mic_code.to_string();
59        self
60    }
61
62    pub fn country(&mut self, country: &str) -> &mut Self {
63        self.country = country.to_string();
64        self
65    }
66
67    pub fn include_delisted(&mut self, include_delisted: bool) -> &mut Self {
68        self.include_delisted = include_delisted;
69        self
70    }
71
72    pub async fn execute(&self) -> Result<Indices, Box<dyn Error>> {
73        let include_delisted = &self.include_delisted.to_string();
74
75        let params = vec![
76            ("symbol", &self.symbol),
77            ("exchange", &self.exchange),
78            ("mic_code", &self.mic_code),
79            ("country", &self.country),
80            ("include_delisted", include_delisted),
81        ];
82
83        internal::request::execute("/indices", params).await
84    }
85}
86
87#[cfg(test)]
88mod tests {
89    use super::Indices;
90
91    #[tokio::test]
92    async fn get_indices() {
93        let indices = Indices::builder().execute().await;
94
95        assert!(indices.is_ok());
96    }
97}