use std::fmt::Display;
use crate::{models::LatLng, SearchParams};
#[derive(Debug, Default)]
pub struct Request {
pub token: String,
pub location: LatLng,
pub keyword: Option<String>,
pub language: Option<String>,
pub maxprice: Option<String>,
pub minprice: Option<String>,
pub opennow: Option<bool>,
pub pagetoken: Option<String>,
pub request_type: Option<String>,
}
impl Display for LatLng {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{},{}", self.lat, self.lng)
}
}
impl SearchParams for Request {
fn get_params(&self) -> Vec<(String, String)> {
let mut params = vec![];
params.push(("key".to_owned(), self.token.to_owned()));
params.push(("location".to_owned(), self.location.to_string()));
if let Some(keyword) = &self.keyword {
params.push(("keyword".to_owned(), keyword.to_owned()))
}
if let Some(language) = &self.language {
params.push(("language".to_owned(), language.to_owned()))
}
if let Some(maxprice) = &self.maxprice {
params.push(("maxprice".to_owned(), maxprice.to_owned()))
}
if let Some(minprice) = &self.minprice {
params.push(("minprice".to_owned(), minprice.to_owned()))
}
if let Some(opennow) = &self.opennow {
if *opennow {
params.push(("opennow".to_owned(), "true".to_owned()))
}
}
if let Some(pagetoken) = &self.pagetoken {
params.push(("pagetoken".to_owned(), pagetoken.to_owned()))
}
if let Some(request_type) = &self.request_type {
params.push(("type".to_owned(), request_type.to_owned()))
}
params
}
}
pub struct Prominence {
pub request: Request,
pub radius: u32,
}
impl SearchParams for Prominence {
fn get_params(&self) -> Vec<(String, String)> {
let mut params = self.request.get_params();
params.push(("rankby".to_owned(), "prominence".to_owned()));
params.push(("radius".to_owned(), self.radius.to_string()));
params
}
}
pub struct PendingDistance {
pub request: Request,
}
impl PendingDistance {
pub fn set_type(mut self, request_type: impl Into<String>) -> Distance {
self.request.request_type = Some(request_type.into());
Distance {
request: self.request,
}
}
pub fn set_keyword(mut self, keyword: impl Into<String>) -> Distance {
self.request.keyword = Some(keyword.into());
Distance {
request: self.request,
}
}
}
pub struct Distance {
pub request: Request,
}
impl Distance {
pub fn set_keyword(mut self, keyword: impl Into<String>) -> Distance {
self.request.keyword = Some(keyword.into());
Distance {
request: self.request,
}
}
}
impl SearchParams for Distance {
fn get_params(&self) -> Vec<(String, String)> {
let mut params = self.request.get_params();
params.push(("rankby".to_owned(), "distance".to_owned()));
params
}
}
impl Request {
pub fn prominence(self, radius: u32) -> Prominence {
Prominence {
request: self,
radius,
}
}
pub fn distance(self) -> PendingDistance {
PendingDistance { request: self }
}
}