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
//! `indexkit` -- index constituent service for Rust.
//!
//! Daily / monthly snapshots of the S&P 500, S&P MidCap 400, S&P SmallCap
//! 600, Nasdaq-100, and Dow Jones Industrial Average, served from bundled
//! parquet files with runtime GitHub fetch and local cache. No API keys.
//! Offline after the first successful fetch.
//!
//! Data sources layer by priority (see [`types::DataSource`]):
//! sponsor CDNs (iShares / Invesco / SPDR) > OSS GitHub mirrors
//! ([fja05680/sp500], [yfiua/index-constituents],
//! [hanshof/sp500_constituents]) > Internet Archive Wayback > SEC EDGAR
//! N-PORT. The S&P 500 now has daily rows from 1996-01-02 onward via the
//! GitHub mirrors; other indices still start 2019-11 via N-PORT (plus
//! sponsor-CDN forward-going).
//!
//! [fja05680/sp500]: https://github.com/fja05680/sp500
//! [yfiua/index-constituents]: https://github.com/yfiua/index-constituents
//! [hanshof/sp500_constituents]: https://github.com/hanshof/sp500_constituents
//!
//! # Quick start -- one-off scripts
//!
//! ```no_run
//! use indexkit::{ym, IndexId};
//!
//! #[tokio::main]
//! async fn main() -> indexkit::Result<()> {
//! // Free functions -- no client setup needed
//! let sp500 = indexkit::sp500_latest().await?;
//! let ndx = indexkit::constituents_for(IndexId::Ndx, ym!(2024, 1)).await?;
//!
//! println!("S&P 500 latest: {} holdings", sp500.len());
//! println!("Top: {} at {:.2}%", sp500[0].name, sp500[0].weight * 100.0);
//! println!("NDX Jan 2024: {} holdings", ndx.len());
//! Ok(())
//! }
//! ```
//!
//! # Client pattern -- connection pool + cache reuse
//!
//! ```no_run
//! use indexkit::{Indexkit, ym, YearMonth};
//!
//! #[tokio::main]
//! async fn main() -> indexkit::Result<()> {
//! let client = Indexkit::new(); // infallible, no ?
//!
//! // Any month form works -- no chrono import needed
//! let a = client.sp500("2024-01").await?;
//! let b = client.sp500(202401u32).await?;
//! let c = client.sp500((2024i32, 1u32)).await?;
//! let d = client.sp500(ym!(2024, 1)).await?;
//! let e = client.sp500(YearMonth::new(2024, 1)?).await?;
//!
//! // All equivalent
//! assert_eq!(a.len(), b.len());
//! assert_eq!(c.len(), d.len());
//! let _ = e;
//! Ok(())
//! }
//! ```
//!
//! # Major types
//!
//! - [`Indexkit`] -- stateful client; create once, call many times.
//! - [`YearMonth`] -- year-month newtype; accepts strings, integers, tuples.
//! - [`Constituent`] -- one holding.
//! - [`IndexSnapshot`] -- constituents + metadata for one month.
//! - [`IndexId`] -- typed index identifier (Sp500, Sp400, Sp600, Ndx, Dji).
//! - [`Error`] -- unified error type; match on this, never on sub-types.
//!
//! # Environment overrides
//!
//! | Variable | Effect |
//! |---|---|
//! | `INDEXKIT_BASE_URL` | Replace the GitHub raw origin URL |
//! | `INDEXKIT_CACHE_DIR` | Override `~/.cache/indexkit/` |
//! | `INDEXKIT_MIRROR_URL` | CDN mirror fallback URL (default: jsDelivr) |
//!
//! # Field coverage per source
//!
//! Which columns a given [`Constituent`] carries depends on the row's
//! [`DataSource`]. Sponsor-CDN / Wayback / N-PORT rows are full-field
//! (weight, shares, market value, CUSIP). GitHub mirror rows
//! ([`DataSource::GithubFja05680`], [`DataSource::GithubYfiua`],
//! [`DataSource::GithubHanshof`]) are ticker-only: `weight` is
//! `f64::NAN`, `cusip` is empty, `shares` / `market_value_usd` are `0.0`.
//! Use [`Constituent::weight_opt`] for an `Option<f64>` accessor that
//! returns `None` on NaN.
//!
//! # Limitations (v1.0.x)
//!
//! - **No ticker from N-PORT**: SEC N-PORT does not include ticker
//! symbols. `SecNport` rows set [`Constituent::ticker`] to `None`.
//! GitHub mirror rows populate ticker.
//! - **No weight/shares from GitHub mirrors**: the three GitHub OSS
//! mirrors are ticker-only. They provide composition history but no
//! per-holding weight vector.
//! - **No GICS sector**: reserved for v1.1 via SIC -> GICS cross-walk.
//! - **60-90 day filing lag** for N-PORT: unavoidable regulatory
//! constraint. GitHub mirrors and sponsor-CDN close the recency gap.
//!
//! # Modules
//!
//! - [`client`] -- [`Indexkit`] async client with blocking wrappers.
//! - [`date`] -- [`YearMonth`] newtype for month inputs.
//! - [`types`] -- [`Constituent`], [`IndexSnapshot`], [`IndexId`],
//! [`types::DataSource`].
//! - [`github_mirror`] -- OSS GitHub CSV fetchers (fja05680, yfiua,
//! hanshof) with ticker parsers and forward-fill helper.
//! - [`nport`] -- N-PORT `primary_doc.xml` parser.
//! - [`sponsor`] -- sponsor-CDN CSV parsers.
//! - [`wayback`] -- Internet Archive CDX + snapshot client.
//! - [`cik`] -- ETF -> CIK / series mapping (verified against live SEC).
//! - [`parquet_io`] -- parquet writer + reader.
//! - [`sec`] -- SEC EDGAR client used by the CLI for backfill.
//! - [`coalesce`] -- merge rows from multiple sources into one snapshot.
//! - [`error`] -- unified [`Error`] enum and [`Result`] alias.
pub
// ---- Top-level re-exports ----
pub use Indexkit;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
// ---- Free-function shortcuts ----
//
// Each function internally uses a process-wide `Indexkit` instance so that
// multiple calls share one HTTP client and cache.
use OnceLock;
/// Constituents for any index at any month (uses shared global client).
///
/// # Example
///
/// ```no_run
/// use indexkit::{IndexId, ym};
///
/// #[tokio::main]
/// async fn main() -> indexkit::Result<()> {
/// let cs = indexkit::constituents_for(IndexId::Sp500, ym!(2024, 1)).await?;
/// println!("{} holdings", cs.len());
/// Ok(())
/// }
/// ```
pub async
/// Latest S&P 500 snapshot (uses shared global client).
///
/// # Example
///
/// ```no_run
/// #[tokio::main]
/// async fn main() -> indexkit::Result<()> {
/// let cs = indexkit::sp500_latest().await?;
/// println!("top holding: {}", cs[0].name);
/// Ok(())
/// }
/// ```
pub async
/// Latest S&P 500 ticker list (uses shared global client).
///
/// Always returns an empty vector in v1.0 because N-PORT does not include
/// ticker symbols; retained for API compatibility with downstream consumers.
///
/// # Example
///
/// ```no_run
/// #[tokio::main]
/// async fn main() -> indexkit::Result<()> {
/// let _tickers = indexkit::sp500_tickers_latest().await?;
/// Ok(())
/// }
/// ```
pub async
/// Latest Nasdaq-100 snapshot (uses shared global client).
pub async
/// Latest DJIA snapshot (uses shared global client).
pub async