@@ -3084,7 +3084,7 @@ pub struct ChildKeyDetails<'a> {
30843084 /// Column of the child table that sorting is done on
30853085 pub sort_by_column : dsl:: Column < ' a > ,
30863086 /// Either `asc` or `desc`
3087- pub direction : & ' static str ,
3087+ pub direction : SortDirection ,
30883088}
30893089
30903090#[ derive( Debug , Clone ) ]
@@ -3102,7 +3102,7 @@ pub struct ChildKeyAndIdSharedDetails<'a> {
31023102 /// Column of the child table that sorting is done on
31033103 pub sort_by_column : dsl:: Column < ' a > ,
31043104 /// Either `asc` or `desc`
3105- pub direction : & ' static str ,
3105+ pub direction : SortDirection ,
31063106}
31073107
31083108#[ allow( unused) ]
@@ -3158,7 +3158,7 @@ pub enum SortKey<'a> {
31583158 Key {
31593159 column : dsl:: Column < ' a > ,
31603160 value : Option < & ' a str > ,
3161- direction : & ' static str ,
3161+ direction : SortDirection ,
31623162 } ,
31633163 /// Order by some other column; `column` will never be `id`
31643164 ChildKey ( ChildKey < ' a > ) ,
@@ -3260,8 +3260,26 @@ impl<'a> fmt::Display for SortKey<'a> {
32603260 }
32613261}
32623262
3263- const ASC : & str = "asc" ;
3264- const DESC : & str = "desc" ;
3263+ #[ derive( Debug , Clone , Copy ) ]
3264+ pub enum SortDirection {
3265+ Asc ,
3266+ Desc ,
3267+ }
3268+
3269+ impl SortDirection {
3270+ fn as_str ( & self ) -> & ' static str {
3271+ match self {
3272+ SortDirection :: Asc => "asc" ,
3273+ SortDirection :: Desc => "desc" ,
3274+ }
3275+ }
3276+ }
3277+
3278+ impl std:: fmt:: Display for SortDirection {
3279+ fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
3280+ write ! ( f, "{}" , self . as_str( ) )
3281+ }
3282+ }
32653283
32663284impl < ' a > SortKey < ' a > {
32673285 fn new (
@@ -3274,7 +3292,7 @@ impl<'a> SortKey<'a> {
32743292 fn sort_key_from_value < ' a > (
32753293 column : dsl:: Column < ' a > ,
32763294 value : & ' a Value ,
3277- direction : & ' static str ,
3295+ direction : SortDirection ,
32783296 ) -> Result < SortKey < ' a > , QueryExecutionError > {
32793297 let sort_value = value. as_str ( ) ;
32803298
@@ -3289,7 +3307,7 @@ impl<'a> SortKey<'a> {
32893307 table : dsl:: Table < ' a > ,
32903308 attribute : String ,
32913309 filter : Option < & ' a EntityFilter > ,
3292- direction : & ' static str ,
3310+ direction : SortDirection ,
32933311 use_block_column : UseBlockColumn ,
32943312 ) -> Result < SortKey < ' a > , QueryExecutionError > {
32953313 let column = table. column_for_field ( & attribute) ?;
@@ -3308,10 +3326,10 @@ impl<'a> SortKey<'a> {
33083326 }
33093327 } else if column. is_primary_key ( ) {
33103328 let block_column = use_block_column. block_column ( table) ;
3329+ use SortDirection :: * ;
33113330 match direction {
3312- ASC => Ok ( SortKey :: IdAsc ( block_column) ) ,
3313- DESC => Ok ( SortKey :: IdDesc ( block_column) ) ,
3314- _ => unreachable ! ( "direction is 'asc' or 'desc'" ) ,
3331+ Asc => Ok ( SortKey :: IdAsc ( block_column) ) ,
3332+ Desc => Ok ( SortKey :: IdDesc ( block_column) ) ,
33153333 }
33163334 } else {
33173335 Ok ( SortKey :: Key {
@@ -3330,7 +3348,7 @@ impl<'a> SortKey<'a> {
33303348 derived : bool ,
33313349 attribute : String ,
33323350 use_block_column : UseBlockColumn ,
3333- direction : & ' static str ,
3351+ direction : SortDirection ,
33343352 ) -> Result < SortKey < ' a > , QueryExecutionError > {
33353353 let child_table = child_table. child ( 1 ) ;
33363354 let sort_by_column = child_table. column_for_field ( & attribute) ?;
@@ -3369,8 +3387,9 @@ impl<'a> SortKey<'a> {
33693387 let child_pk = child_table. primary_key ( ) ;
33703388 let child_br = child_table. block_column ( ) ;
33713389 let child_at_block = child_table. at_block ( block) ;
3390+ use SortDirection :: * ;
33723391 return match direction {
3373- ASC => Ok ( SortKey :: ChildKey ( ChildKey :: IdAsc (
3392+ Asc => Ok ( SortKey :: ChildKey ( ChildKey :: IdAsc (
33743393 ChildIdDetails {
33753394 child_table,
33763395 child_from,
@@ -3382,7 +3401,7 @@ impl<'a> SortKey<'a> {
33823401 } ,
33833402 use_block_column,
33843403 ) ) ) ,
3385- DESC => Ok ( SortKey :: ChildKey ( ChildKey :: IdDesc (
3404+ Desc => Ok ( SortKey :: ChildKey ( ChildKey :: IdDesc (
33863405 ChildIdDetails {
33873406 child_table,
33883407 child_from,
@@ -3394,7 +3413,6 @@ impl<'a> SortKey<'a> {
33943413 } ,
33953414 use_block_column,
33963415 ) ) ) ,
3397- _ => unreachable ! ( "direction is 'asc' or 'desc'" ) ,
33983416 } ;
33993417 }
34003418
@@ -3420,7 +3438,7 @@ impl<'a> SortKey<'a> {
34203438 parent_table : dsl:: Table < ' a > ,
34213439 entity_types : Vec < EntityType > ,
34223440 child : EntityOrderByChildInfo ,
3423- direction : & ' static str ,
3441+ direction : SortDirection ,
34243442 ) -> Result < Vec < ChildKeyAndIdSharedDetails < ' a > > , QueryExecutionError > {
34253443 assert ! ( entity_types. len( ) < 255 ) ;
34263444 return entity_types
@@ -3491,7 +3509,7 @@ impl<'a> SortKey<'a> {
34913509 child : EntityOrderByChildInfo ,
34923510 entity_types : Vec < EntityType > ,
34933511 use_block_column : UseBlockColumn ,
3494- direction : & ' static str ,
3512+ direction : SortDirection ,
34953513 ) -> Result < SortKey < ' a > , QueryExecutionError > {
34963514 if entity_types. is_empty ( ) {
34973515 return Err ( QueryExecutionError :: ConstraintViolation (
@@ -3508,8 +3526,9 @@ impl<'a> SortKey<'a> {
35083526 "Sorting by fulltext fields" . to_string ( ) ,
35093527 ) )
35103528 } else if sort_by_column. is_primary_key ( ) {
3511- if direction == ASC {
3512- Ok ( SortKey :: ChildKey ( ChildKey :: ManyIdAsc (
3529+ use SortDirection :: * ;
3530+ match direction {
3531+ Asc => Ok ( SortKey :: ChildKey ( ChildKey :: ManyIdAsc (
35133532 build_children_vec (
35143533 layout,
35153534 block,
@@ -3530,9 +3549,8 @@ impl<'a> SortKey<'a> {
35303549 } )
35313550 . collect ( ) ,
35323551 use_block_column,
3533- ) ) )
3534- } else {
3535- Ok ( SortKey :: ChildKey ( ChildKey :: ManyIdDesc (
3552+ ) ) ) ,
3553+ Desc => Ok ( SortKey :: ChildKey ( ChildKey :: ManyIdDesc (
35363554 build_children_vec (
35373555 layout,
35383556 block,
@@ -3553,7 +3571,7 @@ impl<'a> SortKey<'a> {
35533571 } )
35543572 . collect ( ) ,
35553573 use_block_column,
3556- ) ) )
3574+ ) ) ) ,
35573575 }
35583576 } else {
35593577 Ok ( SortKey :: ChildKey ( ChildKey :: Many (
@@ -3595,10 +3613,11 @@ impl<'a> SortKey<'a> {
35953613 UseBlockColumn :: No
35963614 } ;
35973615
3616+ use SortDirection :: * ;
35983617 match order {
3599- EntityOrder :: Ascending ( attr, _) => with_key ( table, attr, filter, ASC , use_block_column) ,
3618+ EntityOrder :: Ascending ( attr, _) => with_key ( table, attr, filter, Asc , use_block_column) ,
36003619 EntityOrder :: Descending ( attr, _) => {
3601- with_key ( table, attr, filter, DESC , use_block_column)
3620+ with_key ( table, attr, filter, Desc , use_block_column)
36023621 }
36033622 EntityOrder :: Default => Ok ( SortKey :: IdAsc ( use_block_column. block_column ( table) ) ) ,
36043623 EntityOrder :: Unordered => Ok ( SortKey :: None ) ,
@@ -3611,7 +3630,7 @@ impl<'a> SortKey<'a> {
36113630 child. derived ,
36123631 child. sort_by_attribute ,
36133632 use_block_column,
3614- ASC ,
3633+ Asc ,
36153634 ) ,
36163635 EntityOrderByChild :: Interface ( child, entity_types) => with_child_interface_key (
36173636 layout,
@@ -3620,7 +3639,7 @@ impl<'a> SortKey<'a> {
36203639 child,
36213640 entity_types,
36223641 use_block_column,
3623- ASC ,
3642+ Asc ,
36243643 ) ,
36253644 } ,
36263645 EntityOrder :: ChildDescending ( kind) => match kind {
@@ -3632,7 +3651,7 @@ impl<'a> SortKey<'a> {
36323651 child. derived ,
36333652 child. sort_by_attribute ,
36343653 use_block_column,
3635- DESC ,
3654+ Desc ,
36363655 ) ,
36373656 EntityOrderByChild :: Interface ( child, entity_types) => with_child_interface_key (
36383657 layout,
@@ -3641,7 +3660,7 @@ impl<'a> SortKey<'a> {
36413660 child,
36423661 entity_types,
36433662 use_block_column,
3644- DESC ,
3663+ Desc ,
36453664 ) ,
36463665 } ,
36473666 }
@@ -3752,6 +3771,7 @@ impl<'a> SortKey<'a> {
37523771 out : & mut AstPass < ' _ , ' b , Pg > ,
37533772 use_sort_key_alias : bool ,
37543773 ) -> QueryResult < ( ) > {
3774+ use SortDirection :: * ;
37553775 match self {
37563776 SortKey :: None => Ok ( ( ) ) ,
37573777 SortKey :: IdAsc ( br_column) => {
@@ -3798,23 +3818,23 @@ impl<'a> SortKey<'a> {
37983818 ChildKey :: Single ( child) => SortKey :: sort_expr (
37993819 & child. sort_by_column ,
38003820 & None ,
3801- child. direction ,
3821+ & child. direction ,
38023822 Some ( "c" ) ,
38033823 use_sort_key_alias,
38043824 out,
38053825 ) ,
38063826 ChildKey :: Many ( parent_pk, children) => SortKey :: multi_sort_expr (
38073827 parent_pk,
38083828 children,
3809- children. first ( ) . unwrap ( ) . direction ,
3829+ & children. first ( ) . unwrap ( ) . direction ,
38103830 out,
38113831 ) ,
38123832
38133833 ChildKey :: ManyIdAsc ( children, use_block_column) => {
3814- SortKey :: multi_sort_id_expr ( children, ASC , * use_block_column, out)
3834+ SortKey :: multi_sort_id_expr ( children, Asc , * use_block_column, out)
38153835 }
38163836 ChildKey :: ManyIdDesc ( children, use_block_column) => {
3817- SortKey :: multi_sort_id_expr ( children, DESC , * use_block_column, out)
3837+ SortKey :: multi_sort_id_expr ( children, Desc , * use_block_column, out)
38183838 }
38193839
38203840 ChildKey :: IdAsc ( child, use_block_column) => {
@@ -3887,7 +3907,7 @@ impl<'a> SortKey<'a> {
38873907 fn sort_expr < ' b > (
38883908 column : & ' b dsl:: Column < ' b > ,
38893909 value : & ' b Option < & str > ,
3890- direction : & str ,
3910+ direction : & ' b SortDirection ,
38913911 rest_prefix : Option < & str > ,
38923912 use_sort_key_alias : bool ,
38933913 out : & mut AstPass < ' _ , ' b , Pg > ,
@@ -3933,14 +3953,14 @@ impl<'a> SortKey<'a> {
39333953 }
39343954 }
39353955 out. push_sql ( " " ) ;
3936- out. push_sql ( direction) ;
3956+ out. push_sql ( direction. as_str ( ) ) ;
39373957 out. push_sql ( ", " ) ;
39383958 if !use_sort_key_alias {
39393959 push_prefix ( rest_prefix, out) ;
39403960 }
39413961 out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
39423962 out. push_sql ( " " ) ;
3943- out. push_sql ( direction) ;
3963+ out. push_sql ( direction. as_str ( ) ) ;
39443964 Ok ( ( ) )
39453965 }
39463966
@@ -3949,7 +3969,7 @@ impl<'a> SortKey<'a> {
39493969 fn multi_sort_expr < ' b > (
39503970 parent_pk : & ' b dsl:: Column < ' b > ,
39513971 children : & ' b [ ChildKeyDetails < ' b > ] ,
3952- direction : & str ,
3972+ direction : & ' b SortDirection ,
39533973 out : & mut AstPass < ' _ , ' b , Pg > ,
39543974 ) -> QueryResult < ( ) > {
39553975 for child in children {
@@ -3984,20 +4004,20 @@ impl<'a> SortKey<'a> {
39844004
39854005 out. push_sql ( ") " ) ;
39864006
3987- out. push_sql ( direction) ;
4007+ out. push_sql ( direction. as_str ( ) ) ;
39884008 out. push_sql ( ", " ) ;
39894009
39904010 parent_pk. walk_ast ( out. reborrow ( ) ) ?;
39914011 out. push_sql ( " " ) ;
3992- out. push_sql ( direction) ;
4012+ out. push_sql ( direction. as_str ( ) ) ;
39934013 Ok ( ( ) )
39944014 }
39954015
39964016 /// Generate
39974017 /// COALESCE(id1, id2) direction, [COALESCE(br_column1, br_column2) direction]
39984018 fn multi_sort_id_expr < ' b > (
39994019 children : & ' b [ ChildIdDetails < ' b > ] ,
4000- direction : & str ,
4020+ direction : SortDirection ,
40014021 use_block_column : UseBlockColumn ,
40024022 out : & mut AstPass < ' _ , ' b , Pg > ,
40034023 ) -> QueryResult < ( ) > {
@@ -4014,7 +4034,7 @@ impl<'a> SortKey<'a> {
40144034 }
40154035 out. push_sql ( ") " ) ;
40164036
4017- out. push_sql ( direction) ;
4037+ out. push_sql ( direction. as_str ( ) ) ;
40184038
40194039 if UseBlockColumn :: Yes == use_block_column {
40204040 out. push_sql ( ", coalesce(" ) ;
@@ -4029,7 +4049,7 @@ impl<'a> SortKey<'a> {
40294049 child. child_br . walk_ast ( out. reborrow ( ) ) ?;
40304050 }
40314051 out. push_sql ( ") " ) ;
4032- out. push_sql ( direction) ;
4052+ out. push_sql ( direction. as_str ( ) ) ;
40334053 }
40344054
40354055 Ok ( ( ) )
0 commit comments