(
&self,
currency: &str,
source_name: &str,
)
| 536 | } |
| 537 | |
| 538 | pub async fn get_source_rate( |
| 539 | &self, |
| 540 | currency: &str, |
| 541 | source_name: &str, |
| 542 | ) -> Result<f64, anyhow::Error> { |
| 543 | self.refresh_currency(currency).await?; |
| 544 | |
| 545 | let inner = self.inner.lock().await; |
| 546 | |
| 547 | // Give a helpful error if the source name is unknown entirely |
| 548 | if !inner.sources.contains_key(source_name) { |
| 549 | let available = inner |
| 550 | .sources |
| 551 | .keys() |
| 552 | .cloned() |
| 553 | .collect::<Vec<_>>() |
| 554 | .join(", "); |
| 555 | return Err(anyhow!( |
| 556 | "Unknown source `{source_name}`. Available sources: {available}" |
| 557 | )); |
| 558 | } |
| 559 | |
| 560 | let currency_cache = inner |
| 561 | .currencies |
| 562 | .get(currency) |
| 563 | .ok_or_else(|| anyhow!("No rates available for `{currency}`"))?; |
| 564 | |
| 565 | let price_cache = currency_cache |
| 566 | .prices |
| 567 | .get(source_name) |
| 568 | .ok_or_else(|| anyhow!( |
| 569 | "Source `{source_name}` has no data for `{currency}`. \ |
| 570 | The source may not support this currency or is currently backing off." |
| 571 | ))?; |
| 572 | |
| 573 | if price_cache.timestamp + SERVE_TTL <= Instant::now() { |
| 574 | return Err(anyhow!("Cached rate from `{source_name}` is expired")); |
| 575 | } |
| 576 | |
| 577 | Ok(price_cache.price) |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | fn get_median(source_results: Vec<SourceResult>) -> f64 { |
no test coverage detected