use serde::Deserialize;
use crate::api::ApiClient;
use crate::deserialize::from_str;
use crate::error::{Error, Result};
#[derive(Debug, Clone, Deserialize, Default)]
pub struct Match {
#[serde(rename = "1. symbol")]
symbol: String,
#[serde(rename = "2. name")]
name: String,
#[serde(rename = "3. type")]
stock_type: String,
#[serde(rename = "4. region")]
region: String,
#[serde(rename = "5. marketOpen")]
market_open: String,
#[serde(rename = "6. marketClose")]
market_close: String,
#[serde(rename = "7. timezone")]
time_zone: String,
#[serde(rename = "8. currency")]
currency: String,
#[serde(rename = "9. matchScore", deserialize_with = "from_str")]
match_score: f64,
}
impl Match {
#[must_use]
pub fn symbol(&self) -> &str {
&self.symbol
}
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub fn stock_type(&self) -> &str {
&self.stock_type
}
#[must_use]
pub fn region(&self) -> &str {
&self.region
}
#[must_use]
pub fn market_open(&self) -> &str {
&self.market_open
}
#[must_use]
pub fn market_close(&self) -> &str {
&self.market_close
}
#[must_use]
pub fn time_zone(&self) -> &str {
&self.time_zone
}
#[must_use]
pub fn currency(&self) -> &str {
&self.currency
}
#[must_use]
pub fn match_score(&self) -> f64 {
self.match_score
}
}
#[derive(Default)]
pub struct Search {
matches: Vec<Match>,
}
impl Search {
#[must_use]
pub fn matches(&self) -> &Vec<Match> {
&self.matches
}
}
#[derive(Debug, Deserialize)]
pub(crate) struct SearchHelper {
#[serde(rename = "bestMatches")]
matches: Option<Vec<Match>>,
}
impl SearchHelper {
fn convert(self) -> Result<Search> {
let mut search = Search::default();
if self.matches.is_none() {
return Err(Error::EmptyResponse);
}
search.matches = self.matches.unwrap();
Ok(search)
}
}
pub struct SearchBuilder<'a> {
api_client: &'a ApiClient,
keywords: &'a str,
}
impl<'a> SearchBuilder<'a> {
crate::json_data_struct!(Search, SearchHelper);
#[must_use]
pub fn new(api_client: &'a ApiClient, keywords: &'a str) -> Self {
Self {
api_client,
keywords,
}
}
fn create_url(&self) -> String {
format!("query?function=SYMBOL_SEARCH&keywords={}", self.keywords)
}
}