11use std:: fmt;
22use std:: str:: FromStr ;
33
4+ use once_cell:: sync:: Lazy ;
5+ use regex:: Regex ;
46use serde:: { Deserialize , Serialize } ;
57
68use crate :: error:: { Error , UserError } ;
79
10+ static ASC_DESC_REGEX : Lazy < Regex > =
11+ Lazy :: new ( || Regex :: new ( r#"(asc|desc)\(([\w_-]+)\)"# ) . unwrap ( ) ) ;
12+
813#[ derive( Debug , Serialize , Deserialize , Clone , PartialEq , Eq ) ]
914pub enum Criterion {
1015 /// Sorted by decreasing number of matched query terms.
1116 /// Query words at the front of an attribute is considered better than if it was at the back.
1217 Words ,
1318 /// Sorted by increasing number of typos.
1419 Typo ,
15- /// Dynamically sort at query time the documents. None, one or multiple Asc/Desc sortable
16- /// attributes can be used in place of this criterion at query time.
17- Sort ,
1820 /// Sorted by increasing distance between matched query terms.
1921 Proximity ,
2022 /// Documents with quey words contained in more important
21- /// attributes are considered better.
23+ /// attributes are considred better.
2224 Attribute ,
2325 /// Sorted by the similarity of the matched words with the query words.
2426 Exactness ,
@@ -41,46 +43,29 @@ impl Criterion {
4143impl FromStr for Criterion {
4244 type Err = Error ;
4345
44- fn from_str ( text : & str ) -> Result < Criterion , Self :: Err > {
45- match text {
46+ fn from_str ( txt : & str ) -> Result < Criterion , Self :: Err > {
47+ match txt {
4648 "words" => Ok ( Criterion :: Words ) ,
4749 "typo" => Ok ( Criterion :: Typo ) ,
48- "sort" => Ok ( Criterion :: Sort ) ,
4950 "proximity" => Ok ( Criterion :: Proximity ) ,
5051 "attribute" => Ok ( Criterion :: Attribute ) ,
5152 "exactness" => Ok ( Criterion :: Exactness ) ,
52- text => match AscDesc :: from_str ( text) {
53- Ok ( AscDesc :: Asc ( field) ) => Ok ( Criterion :: Asc ( field) ) ,
54- Ok ( AscDesc :: Desc ( field) ) => Ok ( Criterion :: Desc ( field) ) ,
55- Err ( error) => Err ( error. into ( ) ) ,
56- } ,
57- }
58- }
59- }
60-
61- #[ derive( Debug , Serialize , Deserialize , Clone , PartialEq , Eq ) ]
62- pub enum AscDesc {
63- Asc ( String ) ,
64- Desc ( String ) ,
65- }
66-
67- impl AscDesc {
68- pub fn field ( & self ) -> & str {
69- match self {
70- AscDesc :: Asc ( field) => field,
71- AscDesc :: Desc ( field) => field,
72- }
73- }
74- }
75-
76- impl FromStr for AscDesc {
77- type Err = UserError ;
78-
79- fn from_str ( text : & str ) -> Result < AscDesc , Self :: Err > {
80- match text. rsplit_once ( ':' ) {
81- Some ( ( field_name, "asc" ) ) => Ok ( AscDesc :: Asc ( field_name. to_string ( ) ) ) ,
82- Some ( ( field_name, "desc" ) ) => Ok ( AscDesc :: Desc ( field_name. to_string ( ) ) ) ,
83- _ => Err ( UserError :: InvalidCriterionName { name : text. to_string ( ) } ) ,
53+ text => {
54+ let caps = ASC_DESC_REGEX
55+ . captures ( text)
56+ . ok_or_else ( || UserError :: InvalidCriterionName { name : text. to_string ( ) } ) ?;
57+ let order = caps. get ( 1 ) . unwrap ( ) . as_str ( ) ;
58+ let field_name = caps. get ( 2 ) . unwrap ( ) . as_str ( ) ;
59+ match order {
60+ "asc" => Ok ( Criterion :: Asc ( field_name. to_string ( ) ) ) ,
61+ "desc" => Ok ( Criterion :: Desc ( field_name. to_string ( ) ) ) ,
62+ text => {
63+ return Err (
64+ UserError :: InvalidCriterionName { name : text. to_string ( ) } . into ( )
65+ )
66+ }
67+ }
68+ }
8469 }
8570 }
8671}
@@ -89,7 +74,6 @@ pub fn default_criteria() -> Vec<Criterion> {
8974 vec ! [
9075 Criterion :: Words ,
9176 Criterion :: Typo ,
92- Criterion :: Sort ,
9377 Criterion :: Proximity ,
9478 Criterion :: Attribute ,
9579 Criterion :: Exactness ,
@@ -103,12 +87,11 @@ impl fmt::Display for Criterion {
10387 match self {
10488 Words => f. write_str ( "words" ) ,
10589 Typo => f. write_str ( "typo" ) ,
106- Sort => f. write_str ( "sort" ) ,
10790 Proximity => f. write_str ( "proximity" ) ,
10891 Attribute => f. write_str ( "attribute" ) ,
10992 Exactness => f. write_str ( "exactness" ) ,
110- Asc ( attr) => write ! ( f, "{}:asc " , attr) ,
111- Desc ( attr) => write ! ( f, "{}:desc " , attr) ,
93+ Asc ( attr) => write ! ( f, "asc({}) " , attr) ,
94+ Desc ( attr) => write ! ( f, "desc({}) " , attr) ,
11295 }
11396 }
11497}
0 commit comments