twelvedata/reference/
bonds.rs

1use std::error::Error;
2
3use serde::Deserialize;
4
5use crate::internal;
6
7#[derive(Deserialize, Debug)]
8pub struct Bonds {
9    pub result: BondResult,
10    pub status: String,
11
12    #[serde(skip)]
13    symbol: String,
14    #[serde(skip)]
15    exchange: String,
16    #[serde(skip)]
17    country: String,
18    #[serde(skip)]
19    type_field: String,
20    #[serde(skip)]
21    include_delisted: bool,
22    #[serde(skip)]
23    page: u32,
24    #[serde(skip)]
25    outputsize: u32,
26}
27
28#[derive(Deserialize, Debug)]
29pub struct Bond {
30    pub symbol: String,
31    pub name: String,
32    pub country: String,
33    pub currency: String,
34    pub exchange: String,
35    #[serde(rename = "type")]
36    pub bond_type: String,
37}
38
39#[derive(Deserialize, Debug)]
40pub struct BondResult {
41    pub count: u32,
42    pub list: Vec<Bond>,
43}
44
45impl Bonds {
46    pub fn builder() -> Self {
47        Bonds {
48            result: BondResult {
49                count: 0,
50                list: Vec::new(),
51            },
52            status: String::new(),
53            symbol: String::new(),
54            exchange: String::new(),
55            country: String::new(),
56            type_field: String::new(),
57            include_delisted: false,
58            page: 1,
59            outputsize: 5000,
60        }
61    }
62
63    pub fn symbol(&mut self, symbol: &str) -> &mut Self {
64        self.symbol = symbol.to_string();
65        self
66    }
67
68    pub fn exchange(&mut self, exchange: &str) -> &mut Self {
69        self.exchange = exchange.to_string();
70        self
71    }
72
73    pub fn country(&mut self, country: &str) -> &mut Self {
74        self.country = country.to_string();
75        self
76    }
77
78    pub fn bond_type(&mut self, bond_type: &str) -> &mut Self {
79        self.type_field = bond_type.to_string();
80        self
81    }
82
83    pub fn include_delisted(&mut self, include_delisted: bool) -> &mut Self {
84        self.include_delisted = include_delisted;
85        self
86    }
87
88    pub fn page(&mut self, page: u32) -> &mut Self {
89        self.page = page;
90        self
91    }
92
93    pub fn outputsize(&mut self, outputsize: u32) -> &mut Self {
94        self.outputsize = outputsize;
95        self
96    }
97
98    pub async fn execute(&self) -> Result<Bonds, Box<dyn Error>> {
99        let include_delisted = &self.include_delisted.to_string();
100        let page = &self.page.to_string();
101        let outputsize = &self.outputsize.to_string();
102
103        let params = vec![
104            ("symbol", &self.symbol),
105            ("exchange", &self.exchange),
106            ("country", &self.country),
107            ("type", &self.type_field),
108            ("include_delisted", include_delisted),
109            ("page", page),
110            ("outputsize", outputsize),
111        ];
112
113        internal::request::execute("/bonds", params).await
114    }
115}
116
117#[cfg(test)]
118mod test {
119    use super::Bonds;
120
121    #[tokio::test]
122    async fn get_bonds() {
123        let bonds = Bonds::builder().execute().await;
124
125        assert!(bonds.is_ok());
126    }
127}