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
//! DCF (Discounted Cash Flow) and valuation endpoints
use crate::{
client::FmpClient,
error::Result,
models::valuation::{AdvancedDcf, Dcf as DcfModel, EnterpriseValue, HistoricalDcf, LeveredDcf},
};
use serde::Serialize;
/// DCF and Valuation API endpoints
pub struct Dcf {
client: FmpClient,
}
impl Dcf {
pub(crate) fn new(client: FmpClient) -> Self {
Self { client }
}
/// Get current DCF valuation for a symbol
///
/// Returns the discounted cash flow (DCF) valuation which estimates
/// the intrinsic value of a stock based on its future cash flows.
///
/// # Arguments
/// * `symbol` - Stock symbol (e.g., "AAPL")
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let dcf = client.dcf().get_dcf("AAPL").await?;
/// for valuation in dcf {
/// println!("Symbol: {}, DCF: ${:.2}, Stock Price: ${:.2}",
/// valuation.symbol,
/// valuation.dcf.unwrap_or(0.0),
/// valuation.stock_price.unwrap_or(0.0));
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_dcf(&self, symbol: &str) -> Result<Vec<DcfModel>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self
.client
.build_url(&format!("/discounted-cash-flow/{}", symbol));
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
/// Get historical DCF valuations for a symbol
///
/// Returns DCF valuations over time, allowing comparison of
/// historical intrinsic values vs. actual stock prices.
///
/// # Arguments
/// * `symbol` - Stock symbol (e.g., "AAPL")
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let history = client.dcf().get_historical_dcf("AAPL").await?;
/// for record in history.iter().take(5) {
/// println!("{}: DCF ${:.2} vs Stock ${:.2}",
/// record.date,
/// record.dcf.unwrap_or(0.0),
/// record.stock_price.unwrap_or(0.0));
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_historical_dcf(&self, symbol: &str) -> Result<Vec<HistoricalDcf>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self.client.build_url(&format!(
"/historical-discounted-cash-flow-statement/{}",
symbol
));
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
/// Get advanced DCF with detailed assumptions
///
/// Returns DCF valuation with detailed modeling assumptions including
/// revenue growth, margins, tax rates, WACC, and projected cash flows.
///
/// # Arguments
/// * `symbol` - Stock symbol (e.g., "AAPL")
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let dcf = client.dcf().get_advanced_dcf("AAPL").await?;
/// for valuation in dcf {
/// println!("DCF: ${:.2}", valuation.dcf.unwrap_or(0.0));
/// println!("Revenue Growth: {:.2}%", valuation.revenue_growth.unwrap_or(0.0) * 100.0);
/// println!("WACC: {:.2}%", valuation.wacc.unwrap_or(0.0) * 100.0);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_advanced_dcf(&self, symbol: &str) -> Result<Vec<AdvancedDcf>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self
.client
.build_url(&format!("/advanced_discounted_cash_flow/{}", symbol));
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
/// Get levered DCF (equity value after debt)
///
/// Returns DCF valuation accounting for debt, providing equity value
/// rather than enterprise value.
///
/// # Arguments
/// * `symbol` - Stock symbol (e.g., "AAPL")
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let dcf = client.dcf().get_levered_dcf("AAPL").await?;
/// for valuation in dcf {
/// println!("Levered DCF: ${:.2}", valuation.levered_dcf.unwrap_or(0.0));
/// println!("Enterprise Value: ${:.2}B", valuation.enterprise_value.unwrap_or(0.0) / 1_000_000_000.0);
/// println!("Total Debt: ${:.2}B", valuation.total_debt.unwrap_or(0.0) / 1_000_000_000.0);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_levered_dcf(&self, symbol: &str) -> Result<Vec<LeveredDcf>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self
.client
.build_url(&format!("/levered_discounted_cash_flow/{}", symbol));
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
/// Get company enterprise value
///
/// Returns the enterprise value calculation, which represents the total
/// value of a company (market cap + debt - cash).
///
/// # Arguments
/// * `symbol` - Stock symbol (e.g., "AAPL")
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let ev = client.dcf().get_enterprise_value("AAPL").await?;
/// for value in ev {
/// println!("Market Cap: ${:.2}B", value.market_capitalization.unwrap_or(0.0) / 1_000_000_000.0);
/// println!("+ Debt: ${:.2}B", value.add_total_debt.unwrap_or(0.0) / 1_000_000_000.0);
/// println!("- Cash: ${:.2}B", value.minus_cash_and_cash_equivalents.unwrap_or(0.0) / 1_000_000_000.0);
/// println!("= Enterprise Value: ${:.2}B", value.enterprise_value.unwrap_or(0.0) / 1_000_000_000.0);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_enterprise_value(&self, symbol: &str) -> Result<Vec<EnterpriseValue>> {
#[derive(Serialize)]
struct Query<'a> {
apikey: &'a str,
}
let url = self
.client
.build_url(&format!("/enterprise-values/{}", symbol));
self.client
.get_with_query(
&url,
&Query {
apikey: self.client.api_key(),
},
)
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
// Golden path tests
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_dcf() {
let client = FmpClient::new().unwrap();
let result = client.dcf().get_dcf("AAPL").await;
assert!(result.is_ok());
let dcf = result.unwrap();
assert!(!dcf.is_empty());
assert_eq!(dcf[0].symbol, "AAPL");
assert!(dcf[0].dcf.is_some());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_historical_dcf() {
let client = FmpClient::new().unwrap();
let result = client.dcf().get_historical_dcf("AAPL").await;
assert!(result.is_ok());
let history = result.unwrap();
assert!(!history.is_empty());
assert_eq!(history[0].symbol, "AAPL");
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_advanced_dcf() {
let client = FmpClient::new().unwrap();
let result = client.dcf().get_advanced_dcf("AAPL").await;
assert!(result.is_ok());
let dcf = result.unwrap();
assert!(!dcf.is_empty());
assert!(dcf[0].wacc.is_some());
assert!(dcf[0].revenue_growth.is_some());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_levered_dcf() {
let client = FmpClient::new().unwrap();
let result = client.dcf().get_levered_dcf("AAPL").await;
assert!(result.is_ok());
let dcf = result.unwrap();
assert!(!dcf.is_empty());
assert!(dcf[0].levered_dcf.is_some());
assert!(dcf[0].enterprise_value.is_some());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_enterprise_value() {
let client = FmpClient::new().unwrap();
let result = client.dcf().get_enterprise_value("AAPL").await;
assert!(result.is_ok());
let ev = result.unwrap();
assert!(!ev.is_empty());
assert!(ev[0].enterprise_value.is_some());
assert!(ev[0].market_capitalization.is_some());
}
// Edge case tests
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_dcf_invalid_symbol() {
let client = FmpClient::new().unwrap();
let result = client.dcf().get_dcf("INVALID_XYZ123").await;
// Should succeed but return empty for invalid symbol
assert!(result.is_ok());
if let Ok(dcf) = result {
assert!(dcf.is_empty());
}
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_historical_dcf_multiple_records() {
let client = FmpClient::new().unwrap();
let result = client.dcf().get_historical_dcf("MSFT").await;
assert!(result.is_ok());
let history = result.unwrap();
// Should have multiple historical records
assert!(history.len() > 1);
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_enterprise_value_calculations() {
let client = FmpClient::new().unwrap();
let result = client.dcf().get_enterprise_value("GOOGL").await;
assert!(result.is_ok());
let ev = result.unwrap();
assert!(!ev.is_empty());
// Verify enterprise value components are present
let first = &ev[0];
assert!(first.market_capitalization.is_some());
assert!(first.add_total_debt.is_some());
assert!(first.minus_cash_and_cash_equivalents.is_some());
}
// 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.dcf().get_dcf("AAPL").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.dcf().get_dcf("").await;
// Should fail or return empty
assert!(result.is_err() || result.unwrap().is_empty());
}
}