@@ -2,6 +2,7 @@ use std::collections::{BTreeSet, HashMap, HashSet};
22use std:: result:: Result as StdResult ;
33
44use charabia:: { Tokenizer , TokenizerBuilder } ;
5+ use deserr:: { DeserializeError , DeserializeFromValue } ;
56use itertools:: Itertools ;
67use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
78use time:: OffsetDateTime ;
@@ -22,6 +23,25 @@ pub enum Setting<T> {
2223 NotSet ,
2324}
2425
26+ impl < T , E > DeserializeFromValue < E > for Setting < T >
27+ where
28+ T : DeserializeFromValue < E > ,
29+ E : DeserializeError ,
30+ {
31+ fn deserialize_from_value < V : deserr:: IntoValue > (
32+ value : deserr:: Value < V > ,
33+ location : deserr:: ValuePointerRef ,
34+ ) -> std:: result:: Result < Self , E > {
35+ match value {
36+ deserr:: Value :: Null => Ok ( Setting :: Reset ) ,
37+ _ => T :: deserialize_from_value ( value, location) . map ( Setting :: Set ) ,
38+ }
39+ }
40+ fn default ( ) -> Option < Self > {
41+ Some ( Self :: NotSet )
42+ }
43+ }
44+
2545impl < T > Default for Setting < T > {
2646 fn default ( ) -> Self {
2747 Self :: NotSet
@@ -93,7 +113,7 @@ pub struct Settings<'a, 't, 'u, 'i> {
93113 displayed_fields : Setting < Vec < String > > ,
94114 filterable_fields : Setting < HashSet < String > > ,
95115 sortable_fields : Setting < HashSet < String > > ,
96- criteria : Setting < Vec < String > > ,
116+ criteria : Setting < Vec < Criterion > > ,
97117 stop_words : Setting < BTreeSet < String > > ,
98118 distinct_field : Setting < String > ,
99119 synonyms : Setting < HashMap < String , Vec < String > > > ,
@@ -173,7 +193,7 @@ impl<'a, 't, 'u, 'i> Settings<'a, 't, 'u, 'i> {
173193 self . criteria = Setting :: Reset ;
174194 }
175195
176- pub fn set_criteria ( & mut self , criteria : Vec < String > ) {
196+ pub fn set_criteria ( & mut self , criteria : Vec < Criterion > ) {
177197 self . criteria = Setting :: Set ( criteria) ;
178198 }
179199
@@ -526,14 +546,9 @@ impl<'a, 't, 'u, 'i> Settings<'a, 't, 'u, 'i> {
526546 }
527547
528548 fn update_criteria ( & mut self ) -> Result < ( ) > {
529- match self . criteria {
530- Setting :: Set ( ref fields) => {
531- let mut new_criteria = Vec :: new ( ) ;
532- for name in fields {
533- let criterion: Criterion = name. parse ( ) ?;
534- new_criteria. push ( criterion) ;
535- }
536- self . index . put_criteria ( self . wtxn , & new_criteria) ?;
549+ match & self . criteria {
550+ Setting :: Set ( criteria) => {
551+ self . index . put_criteria ( self . wtxn , criteria) ?;
537552 }
538553 Setting :: Reset => {
539554 self . index . delete_criteria ( self . wtxn ) ?;
@@ -977,7 +992,7 @@ mod tests {
977992 index
978993 . update_settings ( |settings| {
979994 settings. set_displayed_fields ( vec ! [ S ( "name" ) ] ) ;
980- settings. set_criteria ( vec ! [ S ( "age:asc" ) ] ) ;
995+ settings. set_criteria ( vec ! [ Criterion :: Asc ( "age" . to_owned ( ) ) ] ) ;
981996 } )
982997 . unwrap ( ) ;
983998
@@ -1246,7 +1261,7 @@ mod tests {
12461261 . update_settings ( |settings| {
12471262 settings. set_displayed_fields ( vec ! [ "hello" . to_string( ) ] ) ;
12481263 settings. set_filterable_fields ( hashset ! { S ( "age" ) , S ( "toto" ) } ) ;
1249- settings. set_criteria ( vec ! [ "toto:asc" . to_string ( ) ] ) ;
1264+ settings. set_criteria ( vec ! [ Criterion :: Asc ( S ( "toto" ) ) ] ) ;
12501265 } )
12511266 . unwrap ( ) ;
12521267
@@ -1280,7 +1295,7 @@ mod tests {
12801295 . update_settings ( |settings| {
12811296 settings. set_displayed_fields ( vec ! [ "hello" . to_string( ) ] ) ;
12821297 // It is only Asc(toto), there is a facet database but it is denied to filter with toto.
1283- settings. set_criteria ( vec ! [ "toto:asc" . to_string ( ) ] ) ;
1298+ settings. set_criteria ( vec ! [ Criterion :: Asc ( S ( "toto" ) ) ] ) ;
12841299 } )
12851300 . unwrap ( ) ;
12861301
0 commit comments