libfmp 1.3.1

Async, typed Rust client for the Financial Modeling Prep API
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//! Calendar endpoint and query contracts.

use crate::{
    Client, Result,
    endpoints::{
        EndpointSpec, QueryEncoder, QueryParameters,
        metadata::{EndpointBounds, EndpointMetadata, GeographicAvailability},
    },
    responses::calendar::{
        DividendEvent, EarningsEvent, IpoCalendarEvent, IpoDisclosure, IpoProspectus,
        StockSplitEvent,
    },
    types::{Date, Limit, Page, Ticker},
};

macro_rules! symbol_limit_query {
    ($name:ident, $description:literal) => {
        #[doc = $description]
        #[derive(Debug, Clone, PartialEq, Eq)]
        pub struct $name {
            symbol: Ticker,
            limit: Option<Limit>,
        }

        impl $name {
            /// Creates a query for one ticker without an undocumented limit.
            pub fn new(symbol: Ticker) -> Self {
                Self {
                    symbol,
                    limit: None,
                }
            }

            /// Sets the optional provider result limit.
            pub const fn with_limit(mut self, limit: Limit) -> Self {
                self.limit = Some(limit);
                self
            }

            /// Borrows the requested ticker.
            pub const fn symbol(&self) -> &Ticker {
                &self.symbol
            }

            /// Returns the optional provider result limit.
            pub const fn limit(&self) -> Option<Limit> {
                self.limit
            }
        }

        impl From<Ticker> for $name {
            fn from(symbol: Ticker) -> Self {
                Self::new(symbol)
            }
        }

        impl From<&Ticker> for $name {
            fn from(symbol: &Ticker) -> Self {
                Self::new(symbol.clone())
            }
        }

        impl QueryParameters for $name {
            fn encode(&self, encoder: &mut QueryEncoder<'_>) {
                encoder.required("symbol", &self.symbol);
                encoder.optional("limit", self.limit);
            }
        }
    };
}

macro_rules! date_query {
    ($name:ident, $description:literal) => {
        #[doc = $description]
        #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
        pub struct $name {
            from: Option<Date>,
            to: Option<Date>,
        }

        impl $name {
            /// Creates a query without undocumented date defaults.
            pub const fn new() -> Self {
                Self {
                    from: None,
                    to: None,
                }
            }

            /// Sets the optional independent start date.
            pub const fn with_from(mut self, from: Date) -> Self {
                self.from = Some(from);
                self
            }

            /// Sets the optional independent end date.
            pub const fn with_to(mut self, to: Date) -> Self {
                self.to = Some(to);
                self
            }

            /// Returns the optional independent start date.
            pub const fn from(&self) -> Option<Date> {
                self.from
            }

            /// Returns the optional independent end date.
            pub const fn to(&self) -> Option<Date> {
                self.to
            }
        }

        impl QueryParameters for $name {
            fn encode(&self, encoder: &mut QueryEncoder<'_>) {
                encoder.optional("from", self.from);
                encoder.optional("to", self.to);
            }
        }
    };
}

macro_rules! date_page_query {
    ($name:ident, $description:literal) => {
        #[doc = $description]
        #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
        pub struct $name {
            from: Option<Date>,
            to: Option<Date>,
            page: Option<Page>,
        }

        impl $name {
            /// Creates a query without undocumented date or page defaults.
            pub const fn new() -> Self {
                Self {
                    from: None,
                    to: None,
                    page: None,
                }
            }

            /// Sets the optional independent start date.
            pub const fn with_from(mut self, from: Date) -> Self {
                self.from = Some(from);
                self
            }

            /// Sets the optional independent end date.
            pub const fn with_to(mut self, to: Date) -> Self {
                self.to = Some(to);
                self
            }

            /// Sets the optional provider page index.
            pub const fn with_page(mut self, page: Page) -> Self {
                self.page = Some(page);
                self
            }

            /// Returns the optional independent start date.
            pub const fn from(&self) -> Option<Date> {
                self.from
            }

            /// Returns the optional independent end date.
            pub const fn to(&self) -> Option<Date> {
                self.to
            }

            /// Returns the optional provider page index.
            pub const fn page(&self) -> Option<Page> {
                self.page
            }
        }

        impl QueryParameters for $name {
            fn encode(&self, encoder: &mut QueryEncoder<'_>) {
                encoder.optional("from", self.from);
                encoder.optional("to", self.to);
                encoder.optional("page", self.page);
            }
        }
    };
}

symbol_limit_query!(
    DividendsQuery,
    "Required ticker and optional limit for company dividend events."
);
date_page_query!(
    DividendsCalendarQuery,
    "Optional independent dates and page for the dividend calendar."
);

/// Required ticker and optional limit/report-time flag for company earnings.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EarningsQuery {
    symbol: Ticker,
    limit: Option<Limit>,
    include_report_times: Option<bool>,
}

impl EarningsQuery {
    /// Creates a query for one ticker without undocumented optional defaults.
    pub fn new(symbol: Ticker) -> Self {
        Self {
            symbol,
            limit: None,
            include_report_times: None,
        }
    }

    /// Sets the optional provider result limit.
    pub const fn with_limit(mut self, limit: Limit) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Sets whether the provider should include report-time detail.
    pub const fn with_include_report_times(mut self, include_report_times: bool) -> Self {
        self.include_report_times = Some(include_report_times);
        self
    }

    /// Borrows the requested ticker.
    pub const fn symbol(&self) -> &Ticker {
        &self.symbol
    }

    /// Returns the optional provider result limit.
    pub const fn limit(&self) -> Option<Limit> {
        self.limit
    }

    /// Returns the optional report-time flag.
    pub const fn include_report_times(&self) -> Option<bool> {
        self.include_report_times
    }
}

impl From<Ticker> for EarningsQuery {
    fn from(symbol: Ticker) -> Self {
        Self::new(symbol)
    }
}

impl From<&Ticker> for EarningsQuery {
    fn from(symbol: &Ticker) -> Self {
        Self::new(symbol.clone())
    }
}

impl QueryParameters for EarningsQuery {
    fn encode(&self, encoder: &mut QueryEncoder<'_>) {
        encoder.required("symbol", &self.symbol);
        encoder.optional("limit", self.limit);
        encoder.optional("includeReportTimes", self.include_report_times);
    }
}

/// Optional independent dates, page, and report-time flag for the earnings calendar.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct EarningsCalendarQuery {
    from: Option<Date>,
    to: Option<Date>,
    page: Option<Page>,
    include_report_times: Option<bool>,
}

impl EarningsCalendarQuery {
    /// Creates a query without undocumented optional defaults.
    pub const fn new() -> Self {
        Self {
            from: None,
            to: None,
            page: None,
            include_report_times: None,
        }
    }

    /// Sets the optional independent start date.
    pub const fn with_from(mut self, from: Date) -> Self {
        self.from = Some(from);
        self
    }

    /// Sets the optional independent end date.
    pub const fn with_to(mut self, to: Date) -> Self {
        self.to = Some(to);
        self
    }

    /// Sets the optional provider page index.
    pub const fn with_page(mut self, page: Page) -> Self {
        self.page = Some(page);
        self
    }

    /// Sets whether the provider should include report-time detail.
    pub const fn with_include_report_times(mut self, include_report_times: bool) -> Self {
        self.include_report_times = Some(include_report_times);
        self
    }

    /// Returns the optional independent start date.
    pub const fn from(&self) -> Option<Date> {
        self.from
    }

    /// Returns the optional independent end date.
    pub const fn to(&self) -> Option<Date> {
        self.to
    }

    /// Returns the optional provider page index.
    pub const fn page(&self) -> Option<Page> {
        self.page
    }

    /// Returns the optional report-time flag.
    pub const fn include_report_times(&self) -> Option<bool> {
        self.include_report_times
    }
}

impl QueryParameters for EarningsCalendarQuery {
    fn encode(&self, encoder: &mut QueryEncoder<'_>) {
        encoder.optional("from", self.from);
        encoder.optional("to", self.to);
        encoder.optional("page", self.page);
        encoder.optional("includeReportTimes", self.include_report_times);
    }
}

date_query!(
    IposCalendarQuery,
    "Optional independent dates for the worldwide IPO calendar."
);
date_query!(
    IposDisclosureQuery,
    "Optional independent dates for US IPO disclosures."
);
date_query!(
    IposProspectusQuery,
    "Optional independent dates for US IPO prospectuses."
);

symbol_limit_query!(
    StockSplitsQuery,
    "Required ticker and optional limit for company stock-split events."
);
date_page_query!(
    StockSplitsCalendarQuery,
    "Optional independent dates and page for the stock-splits calendar."
);

const COMPANY_EVENT_METADATA: EndpointMetadata = EndpointMetadata::new()
    .with_geography(GeographicAvailability::Worldwide)
    .with_bounds(EndpointBounds::new().with_response_rows(1_000));
const MARKET_CALENDAR_METADATA: EndpointMetadata = EndpointMetadata::new()
    .with_geography(GeographicAvailability::Worldwide)
    .with_bounds(
        EndpointBounds::new()
            .with_response_rows(4_000)
            .with_date_range_days(90),
    );
const IPO_CALENDAR_METADATA: EndpointMetadata = EndpointMetadata::new()
    .with_geography(GeographicAvailability::Worldwide)
    .with_bounds(EndpointBounds::new().with_date_range_days(90));
const US_ONLY_METADATA: EndpointMetadata =
    EndpointMetadata::new().with_geography(GeographicAvailability::UsOnly);

/// Describes `GET dividends` without binding a transport.
pub fn dividends(query: DividendsQuery) -> EndpointSpec<DividendsQuery, Vec<DividendEvent>> {
    EndpointSpec::get("dividends", "dividends", query).with_metadata(COMPANY_EVENT_METADATA)
}

/// Describes `GET dividends-calendar` without binding a transport.
pub fn dividends_calendar(
    query: DividendsCalendarQuery,
) -> EndpointSpec<DividendsCalendarQuery, Vec<DividendEvent>> {
    EndpointSpec::get("dividends-calendar", "dividends-calendar", query)
        .with_metadata(MARKET_CALENDAR_METADATA)
}

/// Describes `GET earnings` without binding a transport.
pub fn earnings(query: EarningsQuery) -> EndpointSpec<EarningsQuery, Vec<EarningsEvent>> {
    EndpointSpec::get("earnings", "earnings", query).with_metadata(COMPANY_EVENT_METADATA)
}

/// Describes `GET earnings-calendar` without binding a transport.
pub fn earnings_calendar(
    query: EarningsCalendarQuery,
) -> EndpointSpec<EarningsCalendarQuery, Vec<EarningsEvent>> {
    EndpointSpec::get("earnings-calendar", "earnings-calendar", query)
        .with_metadata(MARKET_CALENDAR_METADATA)
}

/// Describes `GET ipos-calendar` without binding a transport.
pub fn ipos_calendar(
    query: IposCalendarQuery,
) -> EndpointSpec<IposCalendarQuery, Vec<IpoCalendarEvent>> {
    EndpointSpec::get("ipos-calendar", "ipos-calendar", query).with_metadata(IPO_CALENDAR_METADATA)
}

/// Describes `GET ipos-disclosure` without binding a transport.
pub fn ipos_disclosure(
    query: IposDisclosureQuery,
) -> EndpointSpec<IposDisclosureQuery, Vec<IpoDisclosure>> {
    EndpointSpec::get("ipos-disclosure", "ipos-disclosure", query).with_metadata(US_ONLY_METADATA)
}

/// Describes `GET ipos-prospectus` without binding a transport.
pub fn ipos_prospectus(
    query: IposProspectusQuery,
) -> EndpointSpec<IposProspectusQuery, Vec<IpoProspectus>> {
    EndpointSpec::get("ipos-prospectus", "ipos-prospectus", query).with_metadata(US_ONLY_METADATA)
}

/// Describes `GET splits` without binding a transport.
pub fn stock_splits(
    query: StockSplitsQuery,
) -> EndpointSpec<StockSplitsQuery, Vec<StockSplitEvent>> {
    EndpointSpec::get("splits", "splits", query).with_metadata(COMPANY_EVENT_METADATA)
}

/// Describes `GET splits-calendar` without binding a transport.
pub fn stock_splits_calendar(
    query: StockSplitsCalendarQuery,
) -> EndpointSpec<StockSplitsCalendarQuery, Vec<StockSplitEvent>> {
    EndpointSpec::get("splits-calendar", "splits-calendar", query)
        .with_metadata(MARKET_CALENDAR_METADATA)
}

impl Client {
    /// Retrieves worldwide dividend events for one company.
    pub async fn dividends(&self, query: impl Into<DividendsQuery>) -> Result<Vec<DividendEvent>> {
        self.execute(&dividends(query.into())).await
    }

    /// Retrieves the worldwide dividend calendar.
    pub async fn dividends_calendar(
        &self,
        query: impl Into<DividendsCalendarQuery>,
    ) -> Result<Vec<DividendEvent>> {
        self.execute(&dividends_calendar(query.into())).await
    }

    /// Retrieves worldwide earnings events for one company.
    pub async fn earnings(&self, query: impl Into<EarningsQuery>) -> Result<Vec<EarningsEvent>> {
        self.execute(&earnings(query.into())).await
    }

    /// Retrieves the worldwide earnings calendar.
    pub async fn earnings_calendar(
        &self,
        query: impl Into<EarningsCalendarQuery>,
    ) -> Result<Vec<EarningsEvent>> {
        self.execute(&earnings_calendar(query.into())).await
    }

    /// Retrieves the worldwide IPO calendar.
    ///
    /// The documented 90-day maximum is advisory metadata. Use
    /// [`EndpointBounds::accepts_date_range`](super::metadata::EndpointBounds::accepts_date_range)
    /// for optional preflight validation.
    pub async fn ipos_calendar(
        &self,
        query: impl Into<IposCalendarQuery>,
    ) -> Result<Vec<IpoCalendarEvent>> {
        self.execute(&ipos_calendar(query.into())).await
    }

    /// Retrieves US IPO disclosure filings.
    pub async fn ipos_disclosure(
        &self,
        query: impl Into<IposDisclosureQuery>,
    ) -> Result<Vec<IpoDisclosure>> {
        self.execute(&ipos_disclosure(query.into())).await
    }

    /// Retrieves US IPO prospectus filings and documented offering values.
    pub async fn ipos_prospectus(
        &self,
        query: impl Into<IposProspectusQuery>,
    ) -> Result<Vec<IpoProspectus>> {
        self.execute(&ipos_prospectus(query.into())).await
    }

    /// Retrieves worldwide stock-split events for one company.
    pub async fn stock_splits(
        &self,
        query: impl Into<StockSplitsQuery>,
    ) -> Result<Vec<StockSplitEvent>> {
        self.execute(&stock_splits(query.into())).await
    }

    /// Retrieves the worldwide stock-splits calendar.
    pub async fn stock_splits_calendar(
        &self,
        query: impl Into<StockSplitsCalendarQuery>,
    ) -> Result<Vec<StockSplitEvent>> {
        self.execute(&stock_splits_calendar(query.into())).await
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use super::*;

    fn pairs(query: &impl QueryParameters) -> Vec<(String, String)> {
        let mut pairs = Vec::new();
        query.encode(&mut QueryEncoder::new(&mut |name, value| {
            pairs.push((name.to_owned(), value.to_owned()));
        }));
        pairs
    }

    #[test]
    fn symbol_queries_encode_required_symbol_then_optional_values() {
        let symbol = Ticker::new("AAPL").unwrap();
        let dividends = DividendsQuery::new(symbol.clone()).with_limit(Limit(100));
        assert_eq!(
            pairs(&dividends),
            [
                ("symbol".into(), "AAPL".into()),
                ("limit".into(), "100".into())
            ]
        );

        let earnings = EarningsQuery::new(symbol.clone())
            .with_limit(Limit(100))
            .with_include_report_times(false);
        assert_eq!(
            pairs(&earnings),
            [
                ("symbol".into(), "AAPL".into()),
                ("limit".into(), "100".into()),
                ("includeReportTimes".into(), "false".into()),
            ]
        );

        let splits = StockSplitsQuery::from(&symbol).with_limit(Limit(100));
        assert_eq!(
            pairs(&splits),
            [
                ("symbol".into(), "AAPL".into()),
                ("limit".into(), "100".into())
            ]
        );
    }

    #[test]
    fn date_queries_preserve_independent_omission_and_exact_order() {
        let from = Date::from_str("2026-03-06").unwrap();
        let to = Date::from_str("2026-06-06").unwrap();

        let dividends = DividendsCalendarQuery::new()
            .with_from(from)
            .with_to(to)
            .with_page(Page(0));
        assert_eq!(
            pairs(&dividends),
            [
                ("from".into(), "2026-03-06".into()),
                ("to".into(), "2026-06-06".into()),
                ("page".into(), "0".into()),
            ]
        );

        let earnings = EarningsCalendarQuery::new()
            .with_from(from)
            .with_to(to)
            .with_page(Page(0))
            .with_include_report_times(true);
        assert_eq!(
            pairs(&earnings),
            [
                ("from".into(), "2026-03-06".into()),
                ("to".into(), "2026-06-06".into()),
                ("page".into(), "0".into()),
                ("includeReportTimes".into(), "true".into()),
            ]
        );

        let ipo = IposCalendarQuery::new().with_from(from);
        assert_eq!(ipo.to(), None);
        assert_eq!(pairs(&ipo), [("from".into(), "2026-03-06".into())]);

        let disclosure = IposDisclosureQuery::new().with_to(to);
        assert_eq!(disclosure.from(), None);
        assert_eq!(pairs(&disclosure), [("to".into(), "2026-06-06".into())]);

        let prospectus = IposProspectusQuery::new().with_from(from).with_to(to);
        assert_eq!(
            pairs(&prospectus),
            [
                ("from".into(), "2026-03-06".into()),
                ("to".into(), "2026-06-06".into()),
            ]
        );

        let splits = StockSplitsCalendarQuery::new()
            .with_to(to)
            .with_page(Page(0));
        assert_eq!(splits.from(), None);
        assert_eq!(
            pairs(&splits),
            [
                ("to".into(), "2026-06-06".into()),
                ("page".into(), "0".into())
            ]
        );
    }

    #[test]
    fn report_time_flags_distinguish_omitted_false_and_true() {
        let symbol = Ticker::new("AAPL").unwrap();
        assert_eq!(
            pairs(&EarningsQuery::new(symbol.clone())),
            [("symbol".into(), "AAPL".into())]
        );
        assert_eq!(
            pairs(&EarningsQuery::new(symbol).with_include_report_times(false)),
            [
                ("symbol".into(), "AAPL".into()),
                ("includeReportTimes".into(), "false".into())
            ]
        );
        assert!(pairs(&EarningsCalendarQuery::new()).is_empty());
        assert_eq!(
            pairs(&EarningsCalendarQuery::new().with_include_report_times(true)),
            [("includeReportTimes".into(), "true".into())]
        );
    }
}