Skip to main content

finance_query/models/trending/
response.rs

1//! Trending Response Model
2//!
3//! Represents trending ticker data from Yahoo Finance
4
5use serde::{Deserialize, Serialize};
6
7/// A trending stock/symbol quote
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[cfg_attr(feature = "dataframe", derive(crate::ToDataFrame))]
10#[serde(rename_all = "camelCase")]
11#[non_exhaustive]
12pub struct TrendingQuote {
13    /// Stock symbol
14    pub symbol: String,
15}
16
17/// Raw response from trending endpoint
18#[derive(Debug, Clone, Deserialize)]
19#[serde(rename_all = "camelCase")]
20pub(crate) struct RawTrendingResponse {
21    pub finance: Option<TrendingFinance>,
22}
23
24#[derive(Debug, Clone, Deserialize)]
25#[serde(rename_all = "camelCase")]
26pub(crate) struct TrendingFinance {
27    pub result: Option<Vec<TrendingResult>>,
28}
29
30#[derive(Debug, Clone, Deserialize)]
31#[serde(rename_all = "camelCase")]
32pub(crate) struct TrendingResult {
33    pub quotes: Option<Vec<TrendingQuote>>,
34}
35
36impl TrendingQuote {
37    /// Parse trending quotes from the raw JSON response
38    pub(crate) fn from_response(value: serde_json::Value) -> Result<Vec<Self>, serde_json::Error> {
39        let raw: RawTrendingResponse = serde_json::from_value(value)?;
40        Ok(raw
41            .finance
42            .and_then(|f| f.result)
43            .and_then(|r| r.into_iter().next())
44            .and_then(|r| r.quotes)
45            .unwrap_or_default())
46    }
47}