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
//! 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 {
/// 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.
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
)
}
}