use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq)]
pub enum GroupFormatting {
#[serde(rename = "grouped")]
Grouped,
#[serde(rename = "simple")]
Simple,
}
impl fmt::Display for GroupFormatting {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", format!("{:?}", self).to_lowercase())
}
}
#[derive(Deserialize, Serialize, Clone, Debug, Default, PartialEq)]
pub struct GroupingComponent {
#[serde(skip_serializing_if = "Option::is_none")]
group: Option<bool>,
#[serde(rename = "group.field", skip_serializing_if = "Option::is_none")]
field: Option<Vec<String>>,
#[serde(rename = "group.query", skip_serializing_if = "Option::is_none")]
queries: Option<Vec<String>>,
#[serde(rename = "group.limit", skip_serializing_if = "Option::is_none")]
limit: Option<usize>,
#[serde(rename = "group.offset", skip_serializing_if = "Option::is_none")]
offset: Option<usize>,
#[serde(rename = "group.sort", skip_serializing_if = "Option::is_none")]
sort: Option<Vec<String>>,
#[serde(rename = "group.format", skip_serializing_if = "Option::is_none")]
format: Option<GroupFormatting>,
#[serde(rename = "group.main", skip_serializing_if = "Option::is_none")]
main: Option<bool>,
#[serde(rename = "group.ngroups", skip_serializing_if = "Option::is_none")]
n_groups: Option<bool>,
#[serde(rename = "group.truncate", skip_serializing_if = "Option::is_none")]
truncate: Option<bool>,
#[serde(rename = "group.facet", skip_serializing_if = "Option::is_none")]
facet: Option<bool>,
}
impl GroupingComponent {
pub fn new() -> Self {
Self {
group: Some(true),
field: None,
queries: None,
limit: None,
offset: None,
sort: None,
format: None,
main: None,
n_groups: None,
truncate: None,
facet: None,
}
}
pub fn fields<S: Into<String>, I: IntoIterator<Item = S>, O: Into<Option<I>>>(
mut self,
fields: O,
) -> Self {
self.field = fields
.into()
.map(|x| x.into_iter().map(|x| x.into()).collect());
self
}
pub fn queries<S: Into<String>, I: IntoIterator<Item = S>, O: Into<Option<I>>>(
mut self,
queries: O,
) -> Self {
self.queries = queries
.into()
.map(|x| x.into_iter().map(|x| x.into()).collect());
self
}
pub fn limit<O: Into<Option<usize>>>(mut self, limit: O) -> Self {
self.limit = limit.into();
self
}
pub fn offset<O: Into<Option<usize>>>(mut self, offset: O) -> Self {
self.offset = offset.into();
self
}
pub fn sort<S: Into<String>, I: IntoIterator<Item = S>, O: Into<Option<I>>>(
mut self,
sort: O,
) -> Self {
self.sort = sort
.into()
.map(|fq| fq.into_iter().map(|s| s.into()).collect());
self
}
pub fn format<O: Into<Option<GroupFormatting>>>(mut self, format: O) -> Self {
self.format = format.into();
self
}
pub fn main<O: Into<Option<bool>>>(mut self, main: O) -> Self {
self.main = main.into();
self
}
pub fn n_groups<O: Into<Option<bool>>>(mut self, n_groups: O) -> Self {
self.n_groups = n_groups.into();
self
}
pub fn truncate<O: Into<Option<bool>>>(mut self, truncate: O) -> Self {
self.truncate = truncate.into();
self
}
pub fn facet<O: Into<Option<bool>>>(mut self, facet: O) -> Self {
self.facet = facet.into();
self
}
}
impl AsRef<GroupingComponent> for GroupingComponent {
fn as_ref(&self) -> &Self {
self
}
}
impl From<&GroupingComponent> for GroupingComponent {
fn from(component: &GroupingComponent) -> Self {
component.clone()
}
}