use crate::errors::KeywordNotSet;
use crate::request_handler::Query;
use crate::{Client, Country};
use serde_json::Value;
#[derive(Debug, Clone)]
pub struct RegionInterest {
pub client: Client,
pub resolution: &'static str,
}
impl Default for RegionInterest {
fn default() -> Self {
Self {
client: Client::default(),
resolution: "REGION",
}
}
}
impl RegionInterest {
pub fn new(client: Client) -> Self {
let res = if client.country.eq(&Country::ALL) {
"COUNTRY"
} else {
"REGION"
};
Self {
client,
resolution: res,
}
}
pub fn with_filter(mut self, scale: &'static str) -> Self {
self.resolution = scale;
self
}
pub fn get(&self) -> Value {
self.send_request()[0].clone()
}
pub fn get_for(&self, keyword: &str) -> Value {
let index = self
.client
.keywords
.keywords
.iter()
.position(|&x| x == keyword);
let keyword_index = match index {
Some(k) => k,
None => Err(KeywordNotSet).unwrap(),
};
self.send_request()[keyword_index].clone()
}
}