indexkit 1.0.1

Offline-first bundled-parquet index constituent library — S&P 500, Nasdaq-100, DJIA, S&P 400/600 from SEC EDGAR N-PORT filings
Documentation
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! [`YearMonth`] -- month resolution for index constituent snapshots.
//!
//! N-PORT filings are published monthly, so indexkit's date resolution is
//! year-month, not calendar-day. The [`YearMonth`] type is a thin value type
//! supporting many construction paths so callers never need to import `chrono`.
//!
//! # Examples
//!
//! ```
//! use indexkit::YearMonth;
//!
//! // From string (ISO-like)
//! let ym1: YearMonth = "2024-01".parse().unwrap();
//! // From compact string
//! let ym2: YearMonth = "202401".parse().unwrap();
//! // From components
//! let ym3 = YearMonth::new(2024, 1).unwrap();
//! // From u32 YYYYMM
//! let ym4 = YearMonth::from_yyyymm(202401).unwrap();
//!
//! assert_eq!(ym1, ym3);
//! assert_eq!(ym1.to_string(), "2024-01");
//! ```
//!
//! # Macro
//!
//! The `ym!` macro provides a terse literal form in Rust code:
//!
//! ```
//! use indexkit::{ym, YearMonth};
//!
//! let y = ym!(2024, 1);
//! assert_eq!(y, YearMonth::new(2024, 1).unwrap());
//! ```

use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;

/// Error produced when a value cannot be converted into a [`YearMonth`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct YearMonthError(String);

impl fmt::Display for YearMonthError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "invalid year-month: {}", self.0)
    }
}

impl std::error::Error for YearMonthError {}

/// Year + month pair (month in `1..=12`).
///
/// Used by indexkit public API for snapshot selection since N-PORT filings
/// are monthly. Ordered, comparable, hashable.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct YearMonth {
    year: i32,
    month: u32,
}

impl YearMonth {
    /// Construct from year + month components.
    ///
    /// # Errors
    ///
    /// Returns [`YearMonthError`] if `month` is not in `1..=12` or `year` is
    /// unreasonable (`< 1900` or `> 2200`).
    ///
    /// # Example
    ///
    /// ```
    /// use indexkit::YearMonth;
    /// let ym = YearMonth::new(2024, 1).unwrap();
    /// assert_eq!(ym.to_string(), "2024-01");
    /// ```
    pub fn new(year: i32, month: u32) -> Result<Self, YearMonthError> {
        if !(1..=12).contains(&month) {
            return Err(YearMonthError(format!("month {month} not in 1..=12")));
        }
        if !(1900..=2200).contains(&year) {
            return Err(YearMonthError(format!(
                "year {year} out of plausible range 1900..=2200"
            )));
        }
        Ok(Self { year, month })
    }

    /// Construct from a compact `u32` in `YYYYMM` form (e.g. `202401`).
    ///
    /// # Example
    ///
    /// ```
    /// use indexkit::YearMonth;
    /// let ym = YearMonth::from_yyyymm(202401).unwrap();
    /// assert_eq!(ym.to_string(), "2024-01");
    /// ```
    pub fn from_yyyymm(v: u32) -> Result<Self, YearMonthError> {
        let y = (v / 100) as i32;
        let m = v % 100;
        Self::new(y, m)
    }

    /// The year component.
    #[inline]
    pub fn year(self) -> i32 {
        self.year
    }

    /// The month component (`1..=12`).
    #[inline]
    pub fn month(self) -> u32 {
        self.month
    }

    /// Current month in UTC.
    pub fn current_utc() -> Self {
        use chrono::{Datelike, Utc};
        let now = Utc::now();
        Self {
            year: now.year(),
            month: now.month(),
        }
    }

    /// The month preceding `self`. Wraps from Jan -> Dec of previous year.
    pub fn prev(self) -> Self {
        if self.month == 1 {
            Self {
                year: self.year - 1,
                month: 12,
            }
        } else {
            Self {
                year: self.year,
                month: self.month - 1,
            }
        }
    }

    /// The month following `self`. Wraps from Dec -> Jan of next year.
    pub fn next(self) -> Self {
        if self.month == 12 {
            Self {
                year: self.year + 1,
                month: 1,
            }
        } else {
            Self {
                year: self.year,
                month: self.month + 1,
            }
        }
    }

    /// Iterate every month from `self` up to and including `end`.
    ///
    /// Empty iterator if `end < self`.
    pub fn iter_to(self, end: YearMonth) -> impl Iterator<Item = YearMonth> {
        let mut cur = Some(self);
        std::iter::from_fn(move || {
            let c = cur?;
            if c > end {
                cur = None;
                return None;
            }
            cur = Some(c.next());
            Some(c)
        })
    }
}

// ---- conversions ----

/// Parse a year-month string.
///
/// Accepted forms:
/// - `"YYYY-MM"` (e.g. `"2024-01"`)
/// - `"YYYY/MM"` (e.g. `"2024/01"`)
/// - `"YYYYMM"` compact (e.g. `"202401"`)
impl FromStr for YearMonth {
    type Err = YearMonthError;

    fn from_str(s: &str) -> Result<Self, YearMonthError> {
        let s = s.trim();
        if s.len() == 7 && (s.as_bytes()[4] == b'-' || s.as_bytes()[4] == b'/') {
            let y: i32 = s[..4]
                .parse()
                .map_err(|_| YearMonthError(format!("cannot parse year in {s:?}")))?;
            let m: u32 = s[5..]
                .parse()
                .map_err(|_| YearMonthError(format!("cannot parse month in {s:?}")))?;
            return Self::new(y, m);
        }
        if s.len() == 6 && s.bytes().all(|b| b.is_ascii_digit()) {
            let v: u32 = s
                .parse()
                .map_err(|_| YearMonthError(format!("cannot parse YYYYMM {s:?}")))?;
            return Self::from_yyyymm(v);
        }
        Err(YearMonthError(format!(
            "{s:?} is not a recognised form: expected YYYY-MM, YYYY/MM, or YYYYMM"
        )))
    }
}

impl fmt::Display for YearMonth {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:04}-{:02}", self.year, self.month)
    }
}

// ---- IntoYearMonth sealed trait ----

mod private {
    pub trait Sealed {}
}

/// Convert a value into a [`YearMonth`].
///
/// Implemented for:
/// - `YearMonth` (identity)
/// - `&str`, `String` -- parses the three accepted forms
/// - `u32` -- interpreted as YYYYMM
/// - `(i32, u32)`, `(u32, u32)` -- year, month components
///
/// Used as the bound on all public year-month parameters:
/// ```text
/// pub async fn constituents(&self, index: &str, ym: impl IntoYearMonth) -> ...
/// ```
pub trait IntoYearMonth: private::Sealed {
    fn into_year_month(self) -> Result<YearMonth, YearMonthError>;
}

impl private::Sealed for YearMonth {}
impl IntoYearMonth for YearMonth {
    fn into_year_month(self) -> Result<YearMonth, YearMonthError> {
        Ok(self)
    }
}

impl private::Sealed for &str {}
impl IntoYearMonth for &str {
    fn into_year_month(self) -> Result<YearMonth, YearMonthError> {
        self.parse()
    }
}

impl private::Sealed for String {}
impl IntoYearMonth for String {
    fn into_year_month(self) -> Result<YearMonth, YearMonthError> {
        self.parse()
    }
}

impl private::Sealed for u32 {}
impl IntoYearMonth for u32 {
    fn into_year_month(self) -> Result<YearMonth, YearMonthError> {
        YearMonth::from_yyyymm(self)
    }
}

impl private::Sealed for (i32, u32) {}
impl IntoYearMonth for (i32, u32) {
    fn into_year_month(self) -> Result<YearMonth, YearMonthError> {
        YearMonth::new(self.0, self.1)
    }
}

impl private::Sealed for (u32, u32) {}
impl IntoYearMonth for (u32, u32) {
    fn into_year_month(self) -> Result<YearMonth, YearMonthError> {
        YearMonth::new(self.0 as i32, self.1)
    }
}

/// Terse literal form for a [`YearMonth`].
///
/// # Example
///
/// ```
/// use indexkit::{ym, YearMonth};
/// let y = ym!(2024, 1);
/// assert_eq!(y, YearMonth::new(2024, 1).unwrap());
/// ```
///
/// Panics on invalid inputs -- intended for literal constants. For runtime
/// values use [`YearMonth::new`] which returns a `Result`.
#[macro_export]
macro_rules! ym {
    ($y:expr, $m:expr) => {{
        $crate::YearMonth::new($y as i32, $m as u32).expect("invalid year-month literal")
    }};
}

// ---- tests ----

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_iso() {
        let y: YearMonth = "2024-01".parse().unwrap();
        assert_eq!(y.year(), 2024);
        assert_eq!(y.month(), 1);
    }

    #[test]
    fn parse_compact() {
        let y: YearMonth = "202412".parse().unwrap();
        assert_eq!(y.year(), 2024);
        assert_eq!(y.month(), 12);
    }

    #[test]
    fn parse_slashed() {
        let y: YearMonth = "2024/07".parse().unwrap();
        assert_eq!(y.month(), 7);
    }

    #[test]
    fn rejects_bad_month() {
        assert!(YearMonth::new(2024, 0).is_err());
        assert!(YearMonth::new(2024, 13).is_err());
    }

    #[test]
    fn rejects_bad_year() {
        assert!(YearMonth::new(1800, 1).is_err());
        assert!(YearMonth::new(3000, 1).is_err());
    }

    #[test]
    fn display_roundtrip() {
        let y = YearMonth::new(2019, 11).unwrap();
        assert_eq!(y.to_string(), "2019-11");
        let back: YearMonth = y.to_string().parse().unwrap();
        assert_eq!(back, y);
    }

    #[test]
    fn prev_next_wrap() {
        let y = YearMonth::new(2020, 1).unwrap();
        let p = y.prev();
        assert_eq!(p.year(), 2019);
        assert_eq!(p.month(), 12);
        let n = YearMonth::new(2020, 12).unwrap().next();
        assert_eq!(n.year(), 2021);
        assert_eq!(n.month(), 1);
    }

    #[test]
    fn iter_to_inclusive() {
        let a = YearMonth::new(2020, 1).unwrap();
        let b = YearMonth::new(2020, 3).unwrap();
        let v: Vec<_> = a.iter_to(b).collect();
        assert_eq!(v.len(), 3);
        assert_eq!(v[0], a);
        assert_eq!(v[2], b);
    }

    #[test]
    fn iter_to_empty_when_reversed() {
        let a = YearMonth::new(2020, 5).unwrap();
        let b = YearMonth::new(2020, 3).unwrap();
        assert_eq!(a.iter_to(b).count(), 0);
    }

    #[test]
    fn ordering() {
        let a = YearMonth::new(2020, 1).unwrap();
        let b = YearMonth::new(2020, 12).unwrap();
        let c = YearMonth::new(2021, 1).unwrap();
        assert!(a < b);
        assert!(b < c);
    }

    #[test]
    fn ym_macro() {
        let y = ym!(2024, 1);
        assert_eq!(y, YearMonth::new(2024, 1).unwrap());
    }

    #[test]
    fn from_yyyymm() {
        let y = YearMonth::from_yyyymm(202407).unwrap();
        assert_eq!(y.year(), 2024);
        assert_eq!(y.month(), 7);
    }

    #[test]
    fn into_year_month_u32() {
        let y = 202401u32.into_year_month().unwrap();
        assert_eq!(y.to_string(), "2024-01");
    }

    #[test]
    fn into_year_month_tuple() {
        let y = (2024i32, 7u32).into_year_month().unwrap();
        assert_eq!(y.to_string(), "2024-07");
    }
}