use rust_decimal::Decimal;
use serde::{Serialize, Serializer};
use crate::{
Nominatim, Result,
prelude::CountryCodeIso3166_1Alpha2,
search::{SearchFormat, SearchQuery},
types::{FeatureType, Layer, PlaceId, ViewBox},
};
#[derive(Debug, Clone)]
pub struct SearchQueryBuilder<'a, Q> {
query: Q,
client: &'a Nominatim,
email: &'a str,
parameters: SearchParameters,
}
#[allow(missing_docs)]
impl<'a, Q> SearchQueryBuilder<'a, Q> {
pub(crate) fn new(query: Q, client: &'a Nominatim, email: &'a str) -> Self {
Self {
query,
client,
email,
parameters: SearchParameters::default(),
}
}
pub fn limit(mut self, limit: impl Into<u8>) -> Self {
self.parameters.limit = Some(limit.into());
self
}
pub fn address_details(mut self, address_details: impl Into<bool>) -> Self {
self.parameters.address_details = Some(address_details.into());
self
}
pub fn extra_tags(mut self, exta_tags: impl Into<bool>) -> Self {
self.parameters.extra_tags = Some(exta_tags.into());
self
}
pub fn name_details(mut self, name_details: impl Into<bool>) -> Self {
self.parameters.name_details = Some(name_details.into());
self
}
pub fn entrances(mut self, entrances: impl Into<bool>) -> Self {
self.parameters.entrances = Some(entrances.into());
self
}
pub fn accept_language(mut self, accept_language: impl Into<String>) -> Self {
self.parameters.accept_language = Some(accept_language.into());
self
}
pub fn country_codes(
mut self,
country_codes: impl IntoIterator<Item = CountryCodeIso3166_1Alpha2>,
) -> Self {
self.parameters.country_codes = country_codes.into_iter().collect();
self
}
pub fn layer(mut self, layer: impl IntoIterator<Item = Layer>) -> Self {
self.parameters.layer = layer.into_iter().collect();
self
}
pub fn feature_type(mut self, feature_type: impl IntoIterator<Item = FeatureType>) -> Self {
self.parameters.feature_type = feature_type.into_iter().collect();
self
}
pub fn exclude_place_ids(
mut self,
exclude_place_ids: impl IntoIterator<Item = PlaceId>,
) -> Self {
self.parameters.exclude_place_ids = exclude_place_ids.into_iter().collect();
self
}
pub fn viewbox(mut self, viewbox: impl Into<ViewBox>) -> Self {
self.parameters.viewbox = Some(viewbox.into());
self
}
pub fn bounded(mut self, bounded: impl Into<bool>) -> Self {
self.parameters.bounded = Some(bounded.into());
self
}
}
impl<Q: SearchQuery> SearchQueryBuilder<'_, Q> {
pub async fn send<T: SearchFormat>(self) -> Result<T::Output> {
self.query
.search::<T>(self.client, self.email, &self.parameters)
.await
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct SearchParameters {
#[serde(skip_serializing_if = "Option::is_none")]
limit: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none", rename = "addressdetails")]
address_details: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none", rename = "extratags")]
extra_tags: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none", rename = "namedetails")]
name_details: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
entrances: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none", rename = "accept-language")]
accept_language: Option<String>,
#[serde(
skip_serializing_if = "Vec::is_empty",
rename = "countrycodes",
serialize_with = "serialize_array_comma_str"
)]
country_codes: Vec<CountryCodeIso3166_1Alpha2>,
#[serde(
skip_serializing_if = "Vec::is_empty",
serialize_with = "serialize_array_comma_str"
)]
layer: Vec<Layer>,
#[serde(
skip_serializing_if = "Vec::is_empty",
rename = "featureType",
serialize_with = "serialize_array_comma_str"
)]
feature_type: Vec<FeatureType>,
#[serde(
skip_serializing_if = "Vec::is_empty",
serialize_with = "serialize_array_comma_str"
)]
exclude_place_ids: Vec<PlaceId>,
#[serde(
skip_serializing_if = "Option::is_none",
serialize_with = "serialize_viewbox"
)]
viewbox: Option<ViewBox>,
#[serde(skip_serializing_if = "Option::is_none")]
bounded: Option<bool>,
}
fn serialize_array_comma_str<S, T: ToString>(
arr: &[T],
serializer: S,
) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
arr.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join(",")
.serialize(serializer)
}
fn serialize_viewbox<S>(
viewbox: &Option<ViewBox>,
serializer: S,
) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
viewbox
.map(|viewbox| {
let decarr: [Decimal; 4] = viewbox.into();
decarr
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join(",")
})
.serialize(serializer)
}
#[cfg(test)]
mod tests {
use rust_decimal::prelude::dec;
use crate::prelude::{CountryCode, Point};
use super::*;
#[test]
fn searchparams_serialization_empty() {
let actual = serde_urlencoded::to_string(SearchParameters::default()).unwrap();
assert_eq!("", actual);
}
#[test]
fn searchparams_serialization_everything() {
let actual = serde_urlencoded::to_string(&SearchParameters {
limit: Some(3),
address_details: Some(true),
extra_tags: Some(false),
name_details: Some(false),
entrances: Some(true),
accept_language: None,
country_codes: vec![CountryCode::US, CountryCode::GB],
layer: vec![Layer::Poi, Layer::Natural, Layer::Address],
feature_type: vec![FeatureType::City],
exclude_place_ids: vec![98, 8],
viewbox: Some(ViewBox::new(
Point::new(dec!(40.92702), dec!(-74.33417)),
Point::new(dec!(40.59833), dec!(-73.47312)),
)),
bounded: Some(true),
})
.unwrap();
assert_eq!(
concat!(
"limit=3&addressdetails=true&extratags=false&namedetails=false&entrances=true&",
"countrycodes=us%2Cgb&layer=poi%2Cnatural%2Caddress&featureType=city&",
"exclude_place_ids=98%2C8&viewbox=-74.33417%2C40.92702%2C-73.47312%2C40.59833&",
"bounded=true"
),
actual
);
}
}