Skip to main content

financial_api/
meta.rs

1use serde::Serialize;
2
3use crate::endpoints;
4use crate::{AssetType, Client, Error, Exchange, Response, SearchQuery, TickerData};
5
6/// Parameters for target search and cross-market disambiguation.
7#[derive(Debug, Clone, Serialize)]
8pub struct TickerSearchRequest {
9    q: SearchQuery,
10    #[serde(skip_serializing_if = "Option::is_none")]
11    exchange: Option<Exchange>,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    asset_type: Option<String>,
14    limit: u8,
15}
16
17impl TickerSearchRequest {
18    #[must_use]
19    pub const fn new(q: SearchQuery) -> Self {
20        Self {
21            q,
22            exchange: None,
23            asset_type: None,
24            limit: 10,
25        }
26    }
27
28    #[must_use]
29    pub const fn exchange(mut self, exchange: Exchange) -> Self {
30        self.exchange = Some(exchange);
31        self
32    }
33
34    #[must_use]
35    pub fn asset_types(mut self, asset_types: impl IntoIterator<Item = AssetType>) -> Self {
36        self.asset_type = join_asset_types(asset_types);
37        self
38    }
39
40    pub fn limit(mut self, limit: u8) -> Result<Self, crate::ValidationError> {
41        if !(1..=50).contains(&limit) {
42            return Err(crate::ValidationError::new(
43                "limit",
44                "must be in the range 1..=50",
45            ));
46        }
47        self.limit = limit;
48        Ok(self)
49    }
50}
51
52impl Client {
53    /// Search targets by complete code, local ticker, or name.
54    pub async fn tickers_search(
55        &self,
56        request: &TickerSearchRequest,
57    ) -> Result<Response<TickerData>, Error> {
58        self.get(endpoints::TICKERS_SEARCH, request).await
59    }
60}
61
62/// Parameters for browsing the target code table.
63#[derive(Debug, Clone, Serialize)]
64pub struct TickerListRequest {
65    #[serde(skip_serializing_if = "Option::is_none")]
66    asset_type: Option<String>,
67    limit: u16,
68    offset: u32,
69}
70
71impl TickerListRequest {
72    #[must_use]
73    pub const fn new() -> Self {
74        Self {
75            asset_type: None,
76            limit: 1_000,
77            offset: 0,
78        }
79    }
80
81    #[must_use]
82    pub fn asset_types(mut self, asset_types: impl IntoIterator<Item = AssetType>) -> Self {
83        self.asset_type = join_asset_types(asset_types);
84        self
85    }
86
87    pub fn page(mut self, limit: u16, offset: u32) -> Result<Self, crate::ValidationError> {
88        if limit == 0 || limit > 10_000 {
89            return Err(crate::ValidationError::new(
90                "limit",
91                "must be in the range 1..=10000",
92            ));
93        }
94        self.limit = limit;
95        self.offset = offset;
96        Ok(self)
97    }
98}
99
100fn join_asset_types(asset_types: impl IntoIterator<Item = AssetType>) -> Option<String> {
101    let mut values = Vec::new();
102    for asset_type in asset_types {
103        let value = asset_type.as_str();
104        if !values.contains(&value) {
105            values.push(value);
106        }
107    }
108    (!values.is_empty()).then(|| values.join(","))
109}
110
111impl Default for TickerListRequest {
112    fn default() -> Self {
113        Self::new()
114    }
115}
116
117impl Client {
118    /// Browse the normalized target code table.
119    pub async fn tickers_list(
120        &self,
121        request: &TickerListRequest,
122    ) -> Result<Response<TickerData>, Error> {
123        self.get(endpoints::TICKERS_LIST, request).await
124    }
125}