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")]
26#[allow(dead_code)]
27pub(crate) struct TrendingFinance {
28    pub result: Option<Vec<TrendingResult>>,
29    pub error: Option<serde_json::Value>,
30}
31
32#[derive(Debug, Clone, Deserialize)]
33#[serde(rename_all = "camelCase")]
34#[allow(dead_code)]
35pub(crate) struct TrendingResult {
36    pub count: Option<i32>,
37    pub quotes: Option<Vec<TrendingQuote>>,
38    pub job_timestamp: Option<i64>,
39    pub start_interval: Option<i64>,
40}
41
42impl TrendingQuote {
43    /// Parse trending quotes from the raw JSON response
44    pub(crate) fn from_response(value: serde_json::Value) -> Result<Vec<Self>, serde_json::Error> {
45        let raw: RawTrendingResponse = serde_json::from_value(value)?;
46        Ok(raw
47            .finance
48            .and_then(|f| f.result)
49            .and_then(|r| r.into_iter().next())
50            .and_then(|r| r.quotes)
51            .unwrap_or_default())
52    }
53}