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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
//! Financial statements endpoints
use crate::client::FmpClient;
use crate::error::Result;
use crate::models::common::Period;
use crate::models::financials::{
BalanceSheet, CashFlowStatement, FinancialAsReported, FinancialGrowth, FinancialRatios,
IncomeStatement, KeyMetrics, RevenueGeographicSegmentation, RevenueProductSegmentation,
};
use serde::Serialize;
/// Financial statements API endpoints
pub struct Financials {
client: FmpClient,
}
impl Financials {
pub(crate) fn new(client: FmpClient) -> Self {
Self { client }
}
/// Get income statements
pub async fn get_income_statement(
&self,
symbol: &str,
period: Period,
limit: Option<u32>,
) -> Result<Vec<IncomeStatement>> {
#[derive(Serialize)]
struct Query<'a> {
symbol: &'a str,
period: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
limit: Option<u32>,
apikey: &'a str,
}
let url = self.client.build_url("/income-statement");
self.client
.get_with_query(
&url,
&Query {
symbol,
period: &period.to_string(),
limit,
apikey: self.client.api_key(),
},
)
.await
}
/// Get balance sheet
pub async fn get_balance_sheet(
&self,
symbol: &str,
period: Period,
limit: Option<u32>,
) -> Result<Vec<BalanceSheet>> {
#[derive(Serialize)]
struct Query<'a> {
symbol: &'a str,
period: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
limit: Option<u32>,
apikey: &'a str,
}
let url = self.client.build_url("/balance-sheet-statement");
self.client
.get_with_query(
&url,
&Query {
symbol,
period: &period.to_string(),
limit,
apikey: self.client.api_key(),
},
)
.await
}
/// Get cash flow statement
pub async fn get_cash_flow_statement(
&self,
symbol: &str,
period: Period,
limit: Option<u32>,
) -> Result<Vec<CashFlowStatement>> {
#[derive(Serialize)]
struct Query<'a> {
symbol: &'a str,
period: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
limit: Option<u32>,
apikey: &'a str,
}
let url = self.client.build_url("/cash-flow-statement");
self.client
.get_with_query(
&url,
&Query {
symbol,
period: &period.to_string(),
limit,
apikey: self.client.api_key(),
},
)
.await
}
/// Get financial ratios
pub async fn get_ratios(
&self,
symbol: &str,
period: Period,
limit: Option<u32>,
) -> Result<Vec<FinancialRatios>> {
#[derive(Serialize)]
struct Query<'a> {
symbol: &'a str,
period: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
limit: Option<u32>,
apikey: &'a str,
}
let url = self.client.build_url("/ratios");
self.client
.get_with_query(
&url,
&Query {
symbol,
period: &period.to_string(),
limit,
apikey: self.client.api_key(),
},
)
.await
}
/// Get key metrics
pub async fn get_key_metrics(
&self,
symbol: &str,
period: Period,
limit: Option<u32>,
) -> Result<Vec<KeyMetrics>> {
#[derive(Serialize)]
struct Query<'a> {
symbol: &'a str,
period: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
limit: Option<u32>,
apikey: &'a str,
}
let url = self.client.build_url("/key-metrics");
self.client
.get_with_query(
&url,
&Query {
symbol,
period: &period.to_string(),
limit,
apikey: self.client.api_key(),
},
)
.await
}
/// Get key metrics TTM (Trailing Twelve Months)
pub async fn get_key_metrics_ttm(&self, symbol: &str) -> Result<Vec<KeyMetrics>> {
#[derive(Serialize)]
struct Query<'a> {
symbol: &'a str,
apikey: &'a str,
}
let url = self.client.build_url("/key-metrics-ttm");
self.client
.get_with_query(
&url,
&Query {
symbol,
apikey: self.client.api_key(),
},
)
.await
}
/// Get financial ratios TTM (Trailing Twelve Months)
pub async fn get_ratios_ttm(&self, symbol: &str) -> Result<Vec<FinancialRatios>> {
#[derive(Serialize)]
struct Query<'a> {
symbol: &'a str,
apikey: &'a str,
}
let url = self.client.build_url("/ratios-ttm");
self.client
.get_with_query(
&url,
&Query {
symbol,
apikey: self.client.api_key(),
},
)
.await
}
/// Get financial growth metrics
///
/// Returns year-over-year and multi-year growth rates for key financial metrics.
///
/// # Arguments
/// * `symbol` - Stock symbol (e.g., "AAPL")
/// * `period` - Period (Annual or Quarter)
/// * `limit` - Number of results (optional)
///
/// # Example
/// ```no_run
/// # use fmp_rs::{FmpClient, models::common::Period};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let growth = client.financials().get_financial_growth("AAPL", Period::Annual, Some(5)).await?;
/// for g in growth {
/// println!("{}: Revenue growth: {:.2}%, Net income growth: {:.2}%",
/// g.date, g.revenue_growth * 100.0, g.net_income_growth * 100.0);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_financial_growth(
&self,
symbol: &str,
period: Period,
limit: Option<u32>,
) -> Result<Vec<FinancialGrowth>> {
#[derive(Serialize)]
struct Query<'a> {
symbol: &'a str,
period: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
limit: Option<u32>,
apikey: &'a str,
}
let url = self.client.build_url("/financial-growth");
self.client
.get_with_query(
&url,
&Query {
symbol,
period: &period.to_string(),
limit,
apikey: self.client.api_key(),
},
)
.await
}
/// Get financial statement as reported (XBRL data)
///
/// Returns financial statements as reported to the SEC with XBRL tags.
///
/// # Arguments
/// * `symbol` - Stock symbol (e.g., "AAPL")
/// * `period` - Period (Annual or Quarter)
/// * `limit` - Number of results (optional)
///
/// # Example
/// ```no_run
/// # use fmp_rs::{FmpClient, models::common::Period};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let reported = client.financials().get_financial_as_reported("AAPL", Period::Annual, Some(1)).await?;
/// for statement in reported {
/// println!("{}: {} fields reported", statement.date, statement.data.len());
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_financial_as_reported(
&self,
symbol: &str,
period: Period,
limit: Option<u32>,
) -> Result<Vec<FinancialAsReported>> {
#[derive(Serialize)]
struct Query<'a> {
symbol: &'a str,
period: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
limit: Option<u32>,
apikey: &'a str,
}
let url = self
.client
.build_url("/financial-statement-full-as-reported");
self.client
.get_with_query(
&url,
&Query {
symbol,
period: &period.to_string(),
limit,
apikey: self.client.api_key(),
},
)
.await
}
/// Get revenue product segmentation
///
/// Returns revenue breakdown by product or service line.
///
/// # Arguments
/// * `symbol` - Stock symbol (e.g., "AAPL")
/// * `period` - Period (Annual or Quarter)
/// * `structure` - "flat" for simple structure (optional)
///
/// # Example
/// ```no_run
/// # use fmp_rs::{FmpClient, models::common::Period};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let segments = client.financials().get_revenue_product_segmentation("AAPL", Period::Annual, None).await?;
/// for seg in segments {
/// println!("{}: {} product segments", seg.date, seg.segments.len());
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_revenue_product_segmentation(
&self,
symbol: &str,
period: Period,
structure: Option<&str>,
) -> Result<Vec<RevenueProductSegmentation>> {
#[derive(Serialize)]
struct Query<'a> {
symbol: &'a str,
period: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
structure: Option<&'a str>,
apikey: &'a str,
}
let url = self.client.build_url("/revenue-product-segmentation");
self.client
.get_with_query(
&url,
&Query {
symbol,
period: &period.to_string(),
structure,
apikey: self.client.api_key(),
},
)
.await
}
/// Get revenue geographic segmentation
///
/// Returns revenue breakdown by geographic region.
///
/// # Arguments
/// * `symbol` - Stock symbol (e.g., "AAPL")
/// * `period` - Period (Annual or Quarter)
/// * `structure` - "flat" for simple structure (optional)
///
/// # Example
/// ```no_run
/// # use fmp_rs::{FmpClient, models::common::Period};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let segments = client.financials().get_revenue_geographic_segmentation("AAPL", Period::Annual, None).await?;
/// for seg in segments {
/// println!("{}: {} geographic segments", seg.date, seg.segments.len());
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_revenue_geographic_segmentation(
&self,
symbol: &str,
period: Period,
structure: Option<&str>,
) -> Result<Vec<RevenueGeographicSegmentation>> {
#[derive(Serialize)]
struct Query<'a> {
symbol: &'a str,
period: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
structure: Option<&'a str>,
apikey: &'a str,
}
let url = self.client.build_url("/revenue-geographic-segmentation");
self.client
.get_with_query(
&url,
&Query {
symbol,
period: &period.to_string(),
structure,
apikey: self.client.api_key(),
},
)
.await
}
/// Get full financial statement as reported (comprehensive XBRL data)
///
/// Returns the complete set of as-reported financial data from SEC filings.
/// This is more comprehensive than `get_financial_as_reported()`.
///
/// # Arguments
/// * `symbol` - Stock symbol (e.g., "AAPL")
/// * `period` - Period (Annual or Quarter)
///
/// # Example
/// ```no_run
/// # use fmp_rs::{FmpClient, models::common::Period};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let statements = client.financials().get_financial_full_as_reported("AAPL", Period::Annual).await?;
/// for stmt in statements.iter().take(1) {
/// println!("Date: {}, Period: {}", stmt.date, stmt.period);
/// println!("XBRL fields: {}", stmt.data.len());
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_financial_full_as_reported(
&self,
symbol: &str,
period: Period,
) -> Result<Vec<FinancialAsReported>> {
#[derive(Serialize)]
struct Query<'a> {
symbol: &'a str,
period: &'a str,
apikey: &'a str,
}
let url = self
.client
.build_url("/financial-statement-full-as-reported");
self.client
.get_with_query(
&url,
&Query {
symbol,
period: &period.to_string(),
apikey: self.client.api_key(),
},
)
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let client = FmpClient::builder().api_key("test_key").build().unwrap();
let _ = Financials::new(client);
}
// Golden path tests
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_income_statement() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_income_statement("AAPL", Period::Annual, Some(5))
.await;
assert!(result.is_ok());
let statements = result.unwrap();
assert!(!statements.is_empty());
assert!(statements.len() <= 5);
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_balance_sheet() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_balance_sheet("AAPL", Period::Annual, Some(5))
.await;
assert!(result.is_ok());
let statements = result.unwrap();
assert!(!statements.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_cash_flow_statement() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_cash_flow_statement("AAPL", Period::Annual, Some(5))
.await;
assert!(result.is_ok());
let statements = result.unwrap();
assert!(!statements.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_ratios() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_ratios("AAPL", Period::Annual, Some(5))
.await;
assert!(result.is_ok());
let ratios = result.unwrap();
assert!(!ratios.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_key_metrics() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_key_metrics("AAPL", Period::Annual, Some(5))
.await;
assert!(result.is_ok());
let metrics = result.unwrap();
assert!(!metrics.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_key_metrics_ttm() {
let client = FmpClient::new().unwrap();
let result = client.financials().get_key_metrics_ttm("AAPL").await;
assert!(result.is_ok());
let metrics = result.unwrap();
assert!(!metrics.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_ratios_ttm() {
let client = FmpClient::new().unwrap();
let result = client.financials().get_ratios_ttm("AAPL").await;
assert!(result.is_ok());
let ratios = result.unwrap();
assert!(!ratios.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_financial_growth() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_financial_growth("AAPL", Period::Annual, Some(5))
.await;
assert!(result.is_ok());
let growth = result.unwrap();
assert!(!growth.is_empty());
assert!(growth.len() <= 5);
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_financial_as_reported() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_financial_as_reported("AAPL", Period::Annual, Some(1))
.await;
assert!(result.is_ok());
let statements = result.unwrap();
assert!(!statements.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_revenue_product_segmentation() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_revenue_product_segmentation("AAPL", Period::Annual, None)
.await;
assert!(result.is_ok());
let segments = result.unwrap();
assert!(!segments.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_revenue_geographic_segmentation() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_revenue_geographic_segmentation("AAPL", Period::Annual, None)
.await;
assert!(result.is_ok());
let segments = result.unwrap();
assert!(!segments.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_financial_full_as_reported() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_financial_full_as_reported("AAPL", Period::Annual)
.await;
assert!(result.is_ok());
let statements = result.unwrap();
assert!(!statements.is_empty());
// Full as reported should have comprehensive XBRL data
assert!(!statements[0].data.is_empty());
}
// Test quarterly period
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_income_statement_quarterly() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_income_statement("AAPL", Period::Quarter, Some(4))
.await;
assert!(result.is_ok());
let statements = result.unwrap();
assert!(!statements.is_empty());
assert!(statements.len() <= 4);
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_financial_growth_quarterly() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_financial_growth("AAPL", Period::Quarter, Some(4))
.await;
assert!(result.is_ok());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_financial_full_as_reported_quarterly() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_financial_full_as_reported("AAPL", Period::Quarter)
.await;
assert!(result.is_ok());
let statements = result.unwrap();
assert!(!statements.is_empty());
}
// Edge case tests
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_income_statement_invalid_symbol() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_income_statement("INVALID_SYMBOL_XYZ123", Period::Annual, Some(5))
.await;
// Should either return empty vec or error
if let Ok(statements) = result {
assert!(statements.is_empty());
}
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_financial_growth_zero_limit() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_financial_growth("AAPL", Period::Annual, Some(0))
.await;
// Should handle gracefully
assert!(result.is_ok());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_revenue_segmentation_with_structure() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_revenue_product_segmentation("AAPL", Period::Annual, Some("flat"))
.await;
assert!(result.is_ok());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_revenue_geographic_with_structure() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_revenue_geographic_segmentation("AAPL", Period::Annual, Some("flat"))
.await;
assert!(result.is_ok());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_balance_sheet_no_limit() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_balance_sheet("AAPL", Period::Annual, None)
.await;
assert!(result.is_ok());
let statements = result.unwrap();
assert!(!statements.is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_cash_flow_quarterly() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_cash_flow_statement("AAPL", Period::Quarter, Some(8))
.await;
assert!(result.is_ok());
let statements = result.unwrap();
assert!(!statements.is_empty());
assert!(statements.len() <= 8);
}
// Error handling tests
#[tokio::test]
async fn test_invalid_api_key() {
let client = FmpClient::builder()
.api_key("invalid_key_12345")
.build()
.unwrap();
let result = client
.financials()
.get_income_statement("AAPL", Period::Annual, Some(5))
.await;
assert!(result.is_err());
}
#[tokio::test]
async fn test_empty_symbol() {
let client = FmpClient::builder().api_key("test_key").build().unwrap();
let result = client
.financials()
.get_income_statement("", Period::Annual, Some(5))
.await;
// Should handle gracefully
assert!(result.is_err() || result.unwrap().is_empty());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_company_without_segmentation() {
let client = FmpClient::new().unwrap();
// Some companies may not have segmentation data
let result = client
.financials()
.get_revenue_product_segmentation("TSLA", Period::Annual, None)
.await;
// Should succeed but may be empty
if let Ok(_segments) = result {
// No assertion on emptiness as it depends on company
}
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_financial_full_as_reported_invalid_symbol() {
let client = FmpClient::new().unwrap();
let result = client
.financials()
.get_financial_full_as_reported("INVALID_SYMBOL_XYZ123", Period::Annual)
.await;
// Should succeed but return empty for invalid symbol
assert!(result.is_ok());
if let Ok(statements) = result {
assert!(statements.is_empty());
}
}
}