1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//! Symbol discovery handle.
//!
//! Created via [`Providers::discovery`](crate::Providers::discovery).
use std::sync::Arc;
use crate::error::Result;
use crate::models::discovery::reference::{
ExchangeInfo, ScreenerFilters, ScreenerMatch, SymbolDetails, SymbolMatch,
};
use crate::providers::Capability;
domain_handle! {
/// Symbol discovery backed by configured data providers.
///
/// Unlike [`crate::finance::search`] — a Yahoo-only convenience shortcut —
/// this routes through [`Capability::DISCOVERY`], so it honours the provider
/// priority configured on [`Providers::builder`](crate::Providers::builder)
/// and falls back across providers.
///
/// Created via [`Providers::discovery`](crate::Providers::discovery).
pub struct Discovery
caches: {
cache: Vec<SymbolMatch>,
details_cache: SymbolDetails,
}
}
impl Discovery {
/// Fetch one independently addressable delisted page (zero-based).
///
/// `page` must be below 10,000 and `limit` in 1–100.
///
/// Callers fetching pages in parallel must check contiguous coverage,
/// repeated records and the empty terminal page. Unlike
/// [`delisted_stocks_page`](Self::delisted_stocks_page), this does not carry
/// scan-progress validation.
pub async fn delisted_stocks_page_at(&self, page: u32, limit: u32) -> Result<Vec<SymbolMatch>> {
dispatch_via!(
self,
DISCOVERY,
as_discovery,
DelistedStocksPageAt,
fetch_delisted_stocks_page_at,
[],
page,
limit
)
}
/// Fetch one delisted-stock page of 1–100 rows. Persist the returned cursor
/// with the records and pass it back with the same `limit`.
pub async fn delisted_stocks_page(
&self,
limit: u32,
cursor: Option<&crate::PageCursor>,
) -> Result<crate::ProviderPage<SymbolMatch>> {
self.providers
.fetch_paged(
Capability::DISCOVERY,
crate::Operation::DelistedStocksPage,
&limit.to_string(),
cursor,
|p| {
let p = Arc::clone(p);
async move {
p.as_discovery()
.ok_or_else(|| p.not_supported(crate::Operation::DelistedStocksPage))?
.fetch_delisted_stocks_page(limit, cursor)
.await
}
},
)
.await
}
/// Fetch the current provider-wide symbol directory. Active status can be unknown.
pub async fn stock_list(&self) -> Result<Vec<SymbolMatch>> {
dispatch_via!(
self,
DISCOVERY,
as_discovery,
StockList,
fetch_stock_list,
[]
)
}
/// Fetch one numbered bulk-profile part. No additional parts are downloaded or cached.
/// An empty part is returned as empty; access and transport failures remain errors.
pub async fn company_profiles_bulk(&self, part: u32) -> Result<Vec<crate::CompanyProfile>> {
dispatch_via!(
self,
DISCOVERY,
as_discovery,
CompanyProfilesBulk,
fetch_company_profiles_bulk,
[],
part
)
}
/// Download one dated stock-directory page. Continuations never change providers.
pub async fn stock_listings_page(
&self,
request: &crate::StockListingRequest,
cursor: Option<&crate::PageCursor>,
) -> Result<crate::ProviderPage<crate::StockListing>> {
self.providers
.fetch_paged(
Capability::DISCOVERY,
crate::Operation::StockListingsPage,
&request.cursor_identity(),
cursor,
|p| {
let p = Arc::clone(p);
async move {
p.as_discovery()
.ok_or_else(|| p.not_supported(crate::Operation::StockListingsPage))?
.fetch_stock_listings_page(request, cursor)
.await
}
},
)
.await
}
/// Fetch details as of YYYY-MM-DD, independently of undated detail cache entries.
pub async fn details_at(&self, symbol: &str, date: &str) -> Result<SymbolDetails> {
crate::models::discovery::listings::date(date)?;
let key = serde_json::to_string(&("details_at", symbol, date))?;
self.details_cache
.get_or_try(key, || async {
self.providers
.fetch(Capability::DISCOVERY, |p| {
let p = Arc::clone(p);
async move {
p.as_discovery()
.ok_or_else(|| p.not_supported(crate::Operation::SymbolDetailsAt))?
.fetch_symbol_details_at(symbol, date)
.await
}
})
.await
})
.await
}
/// Every ticker a security has traded under, oldest first. `id` is a ticker
/// or a composite FIGI; a FIGI follows the security through renames, such
/// as FB (from 2012-05-18) becoming META (from 2022-06-09).
pub async fn ticker_changes(&self, id: &str) -> Result<Vec<crate::TickerChange>> {
self.providers
.fetch(Capability::DISCOVERY, |p| {
let p = Arc::clone(p);
async move {
p.as_discovery()
.ok_or_else(|| p.not_supported(crate::Operation::TickerChanges))?
.fetch_ticker_changes(id)
.await
}
})
.await
}
/// Fetch open-ended stock-type codes for a market locale, such as us.
pub async fn stock_types(&self, locale: &str) -> Result<Vec<crate::StockType>> {
self.providers
.fetch(Capability::DISCOVERY, |p| {
let p = Arc::clone(p);
async move {
p.as_discovery()
.ok_or_else(|| p.not_supported(crate::Operation::StockTypes))?
.fetch_stock_types(locale)
.await
}
})
.await
}
/// Search the configured providers' symbol universe.
///
/// Results are cached per `(query, limit)` pair.
pub async fn search(&self, query: &str, limit: u32) -> Result<Vec<SymbolMatch>> {
let key = format!("{query}\u{1f}{limit}");
let providers = Arc::clone(&self.providers);
let query = query.to_string();
self.cache
.get_or_try(key, move || async move {
providers
.fetch(Capability::DISCOVERY, move |p| {
let query = query.clone();
let p = p.clone();
async move {
p.as_discovery()
.ok_or_else(|| {
p.not_supported(crate::providers::Operation::SymbolSearch)
})?
.fetch_symbol_search(&query, limit)
.await
}
})
.await
})
.await
}
/// Fetch detailed reference data for one symbol.
pub async fn details(&self, symbol: &str) -> Result<SymbolDetails> {
let providers = Arc::clone(&self.providers);
let symbol = symbol.to_string();
let key = symbol.clone();
self.details_cache
.get_or_try(key, move || async move {
providers
.fetch(Capability::DISCOVERY, move |p| {
let symbol = symbol.clone();
let p = p.clone();
async move {
p.as_discovery()
.ok_or_else(|| {
p.not_supported(crate::providers::Operation::SymbolDetails)
})?
.fetch_symbol_details(&symbol)
.await
}
})
.await
})
.await
}
/// Fetch the tradable exchange listing.
pub async fn exchanges(&self) -> Result<Vec<ExchangeInfo>> {
dispatch_via!(
self,
DISCOVERY,
as_discovery,
Exchanges,
fetch_exchanges,
[]
)
}
/// Fetch the providers' whole listed-security universe.
///
/// `active = false` asks for delisted securities instead. This is an
/// unfiltered dump — expect thousands of rows in one response — so prefer
/// [`search`](Self::search) when you have a query. Cached per `active`.
/// EDGAR serves `active = true` keylessly from SEC's bulk ticker files
/// (no exchange-listing history, so `active = false` isn't supported
/// there). Alpha Vantage and FMP serve both. FMP's active list includes
/// global instruments; callers must filter their intended stock universe.
/// FMP delisted listings are the first 100 rows, with IPO and delisting
/// dates; page through the rest with
/// [`delisted_stocks_page`](Self::delisted_stocks_page).
pub async fn listing_status(&self, active: bool) -> Result<Vec<SymbolMatch>> {
let providers = Arc::clone(&self.providers);
self.cache
.get_or_try(
format!("listing_status\u{1f}{active}"),
move || async move {
providers
.fetch(Capability::DISCOVERY, move |p| {
let p = p.clone();
async move {
p.as_discovery()
.ok_or_else(|| {
p.not_supported(crate::providers::Operation::ListingStatus)
})?
.fetch_listing_status(active)
.await
}
})
.await
},
)
.await
}
/// Run a screener query over the providers' universe.
///
/// Not cached — screener filters are open-ended and results are
/// price-sensitive, so every call fetches fresh.
pub async fn screener(&self, filters: &ScreenerFilters) -> Result<Vec<ScreenerMatch>> {
let filters = filters.clone();
dispatch_via!(
self,
DISCOVERY,
as_discovery,
Screener,
fetch_screener,
[filters],
&filters
)
}
}