use geojson::Geometry;
use serde::{Deserialize, Serialize};
use serde_with::{DisplayFromStr, StringWithSeparator, formats::CommaSeparator};
use crate::common::{Bbox, Datetime};
#[serde_with::serde_as]
#[derive(Serialize, Deserialize, Default, Debug)]
pub struct SearchParams {
pub limit: Option<u64>,
pub offset: Option<u64>,
#[serde(default)]
#[serde_as(as = "Option<DisplayFromStr>")]
pub bbox: Option<Bbox>,
#[serde(default)]
#[serde_as(as = "Option<DisplayFromStr>")]
pub datetime: Option<Datetime>,
#[serde(default)]
#[serde_as(as = "Option<DisplayFromStr>")]
pub intersects: Option<Geometry>,
#[serde(default)]
#[serde_as(as = "Option<StringWithSeparator::<CommaSeparator, String>>")]
pub ids: Option<Vec<String>>,
#[serde(default)]
#[serde_as(as = "Option<StringWithSeparator::<CommaSeparator, String>>")]
pub collections: Option<Vec<String>>,
}
impl SearchParams {
pub fn new() -> SearchParams {
SearchParams::default()
}
pub fn with_bbox(mut self, bbox: Bbox) -> Self {
self.bbox = Some(bbox);
self
}
pub fn with_datetime(mut self, datetime: Datetime) -> Self {
self.datetime = Some(datetime);
self
}
pub fn with_intersects(mut self, intersects: Geometry) -> Self {
self.intersects = Some(intersects);
self
}
pub fn with_ids<S, I>(mut self, ids: I) -> Self
where
S: std::fmt::Display,
I: IntoIterator<Item = S>,
{
self.ids = Some(ids.into_iter().map(|i| i.to_string()).collect());
self
}
pub fn with_collections<S, I>(mut self, collections: I) -> Self
where
S: std::fmt::Display,
I: IntoIterator<Item = S>,
{
self.collections = Some(collections.into_iter().map(|c| c.to_string()).collect());
self
}
}
#[serde_with::serde_as]
#[derive(Serialize, Deserialize, Default, Debug)]
pub struct SearchBody {
pub limit: Option<u64>,
pub offset: Option<u64>,
pub bbox: Option<Bbox>,
#[serde(default)]
#[serde_as(as = "Option<DisplayFromStr>")]
pub datetime: Option<Datetime>,
#[serde(default)]
pub intersects: Option<Geometry>,
#[serde(default)]
pub ids: Option<Vec<String>>,
#[serde(default)]
pub collections: Option<Vec<String>>,
}
impl From<SearchBody> for SearchParams {
fn from(body: SearchBody) -> Self {
SearchParams {
limit: body.limit,
offset: body.offset,
bbox: body.bbox,
datetime: body.datetime,
intersects: body.intersects,
ids: body.ids,
collections: body.collections,
}
}
}