From 64b5b2e1f817f919bf2eb89285c92ac5530760f8 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Thu, 2 Jun 2022 11:14:46 +0200 Subject: [PATCH] Use serde-cs::CS with StarOr to reduce the logic duplication --- meilisearch-http/src/routes/indexes/search.rs | 23 +++++++++++-------- meilisearch-http/src/routes/mod.rs | 5 +++- meilisearch-http/src/routes/tasks.rs | 6 ++--- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/meilisearch-http/src/routes/indexes/search.rs b/meilisearch-http/src/routes/indexes/search.rs index eb4ee6d34..2f3f4a83b 100644 --- a/meilisearch-http/src/routes/indexes/search.rs +++ b/meilisearch-http/src/routes/indexes/search.rs @@ -8,11 +8,13 @@ use meilisearch_lib::index::{ }; use meilisearch_lib::MeiliSearch; use serde::Deserialize; +use serde_cs::vec::CS; use serde_json::Value; use crate::analytics::{Analytics, SearchAggregator}; use crate::extractors::authentication::{policies::*, GuardedData}; use crate::extractors::sequential_extractor::SeqHandler; +use crate::routes::{fold_star_or, StarOr}; pub fn configure(cfg: &mut web::ServiceConfig) { cfg.service( @@ -28,16 +30,16 @@ pub struct SearchQueryGet { q: Option, offset: Option, limit: Option, - attributes_to_retrieve: Option, - attributes_to_crop: Option, + attributes_to_retrieve: Option>>, + attributes_to_crop: Option>>, #[serde(default = "DEFAULT_CROP_LENGTH")] crop_length: usize, - attributes_to_highlight: Option, + attributes_to_highlight: Option>>, filter: Option, sort: Option, #[serde(default = "Default::default")] show_matches_position: bool, - facets: Option, + facets: Option>>, #[serde(default = "DEFAULT_HIGHLIGHT_PRE_TAG")] highlight_pre_tag: String, #[serde(default = "DEFAULT_HIGHLIGHT_POST_TAG")] @@ -50,19 +52,20 @@ impl From for SearchQuery { fn from(other: SearchQueryGet) -> Self { let attributes_to_retrieve = other .attributes_to_retrieve - .map(|attrs| attrs.split(',').map(String::from).collect()); + .map(CS::into_inner) + .and_then(fold_star_or); let attributes_to_crop = other .attributes_to_crop - .map(|attrs| attrs.split(',').map(String::from).collect()); + .map(CS::into_inner) + .and_then(fold_star_or); let attributes_to_highlight = other .attributes_to_highlight - .map(|attrs| attrs.split(',').map(String::from).collect()); + .map(CS::into_inner) + .and_then(fold_star_or); - let facets = other - .facets - .map(|attrs| attrs.split(',').map(String::from).collect()); + let facets = other.facets.map(CS::into_inner).and_then(fold_star_or); let filter = match other.filter { Some(f) => match serde_json::from_str(&f) { diff --git a/meilisearch-http/src/routes/mod.rs b/meilisearch-http/src/routes/mod.rs index 1b37396e9..a438d12d7 100644 --- a/meilisearch-http/src/routes/mod.rs +++ b/meilisearch-http/src/routes/mod.rs @@ -49,7 +49,10 @@ impl FromStr for StarOr { /// Extracts the raw values from the `StarOr` types and /// return None if a `StarOr::Star` is encountered. -pub fn fold_star_or(content: impl IntoIterator>) -> Option> { +pub fn fold_star_or(content: impl IntoIterator>) -> Option +where + O: FromIterator, +{ content .into_iter() .map(|value| match value { diff --git a/meilisearch-http/src/routes/tasks.rs b/meilisearch-http/src/routes/tasks.rs index 0ab4678b7..b13c04dc7 100644 --- a/meilisearch-http/src/routes/tasks.rs +++ b/meilisearch-http/src/routes/tasks.rs @@ -81,9 +81,9 @@ async fn get_tasks( // We first transform a potential indexUid=* into a "not specified indexUid filter" // for every one of the filters: type, status, and indexUid. - let type_ = type_.map(CS::into_inner).and_then(fold_star_or); - let status = status.map(CS::into_inner).and_then(fold_star_or); - let index_uid = index_uid.map(CS::into_inner).and_then(fold_star_or); + let type_: Option> = type_.map(CS::into_inner).and_then(fold_star_or); + let status: Option> = status.map(CS::into_inner).and_then(fold_star_or); + let index_uid: Option> = index_uid.map(CS::into_inner).and_then(fold_star_or); // Then we filter on potential indexes and make sure that the search filter // restrictions are also applied.