Skip to content

Commit be29ee3

Browse files
committed
Improve serde code
1 parent 7546a80 commit be29ee3

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

src/search.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,6 @@ pub struct SearchResults<T> {
123123
pub index_uid: Option<String>,
124124
}
125125

126-
pub(crate) fn serialize_with_wildcard<S: Serializer, T: Serialize>(
127-
data: &Option<Selectors<T>>,
128-
s: S,
129-
) -> Result<S::Ok, S::Error> {
130-
match data {
131-
Some(Selectors::All) => ["*"].serialize(s),
132-
Some(Selectors::Some(data)) => data.serialize(s),
133-
None => s.serialize_none(),
134-
}
135-
}
136-
137126
fn serialize_attributes_to_crop_with_wildcard<S: Serializer>(
138127
data: &Option<Selectors<&[AttributeToCrop]>>,
139128
s: S,
@@ -169,6 +158,15 @@ pub enum Selectors<T> {
169158
All,
170159
}
171160

161+
impl Serialize for Selectors<&[&str]> {
162+
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
163+
match self {
164+
Selectors::Some(data) => data.serialize(s),
165+
Selectors::All => ["*"].serialize(s),
166+
}
167+
}
168+
}
169+
172170
/// Configures Meilisearch to return search results based on a query’s meaning and context
173171
#[derive(Debug, Serialize, Clone)]
174172
#[serde(rename_all = "camelCase")]
@@ -291,7 +289,6 @@ pub struct SearchQuery<'a, Http: HttpClient> {
291289
///
292290
/// **Default: all attributes found in the documents.**
293291
#[serde(skip_serializing_if = "Option::is_none")]
294-
#[serde(serialize_with = "serialize_with_wildcard")]
295292
pub facets: Option<Selectors<&'a [&'a str]>>,
296293
/// Attributes to sort.
297294
#[serde(skip_serializing_if = "Option::is_none")]
@@ -309,7 +306,6 @@ pub struct SearchQuery<'a, Http: HttpClient> {
309306
///
310307
/// **Default: all attributes found in the documents.**
311308
#[serde(skip_serializing_if = "Option::is_none")]
312-
#[serde(serialize_with = "serialize_with_wildcard")]
313309
pub attributes_to_retrieve: Option<Selectors<&'a [&'a str]>>,
314310
/// Attributes whose values have to be cropped.
315311
///
@@ -337,7 +333,6 @@ pub struct SearchQuery<'a, Http: HttpClient> {
337333
///
338334
/// Can be set to a [wildcard value](enum.Selectors.html#variant.All) that will select all existing attributes.
339335
#[serde(skip_serializing_if = "Option::is_none")]
340-
#[serde(serialize_with = "serialize_with_wildcard")]
341336
pub attributes_to_highlight: Option<Selectors<&'a [&'a str]>>,
342337
/// Tag in front of a highlighted term.
343338
///

src/similar.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
errors::Error,
33
indexes::Index,
44
request::HttpClient,
5-
search::{serialize_with_wildcard, Filter, Selectors},
5+
search::{Filter, Selectors},
66
};
77
use either::Either;
88
use serde::{de::DeserializeOwned, Deserialize, Serialize};
@@ -100,7 +100,6 @@ pub struct SimilarQuery<'a, Http: HttpClient> {
100100
///
101101
/// **Default: all attributes found in the documents.**
102102
#[serde(skip_serializing_if = "Option::is_none")]
103-
#[serde(serialize_with = "serialize_with_wildcard")]
104103
pub attributes_to_retrieve: Option<Selectors<&'a [&'a str]>>,
105104

106105
/// Defines whether to display the global ranking score of a document

0 commit comments

Comments
 (0)