@@ -3091,7 +3091,7 @@ pub struct ChildKeyDetails<'a> {
30913091 /// Column of the child table that sorting is done on
30923092 pub sort_by_column : dsl:: Column < ' a > ,
30933093 /// Either `asc` or `desc`
3094- pub direction : & ' static str ,
3094+ pub direction : SortDirection ,
30953095}
30963096
30973097#[ derive( Debug , Clone ) ]
@@ -3109,7 +3109,7 @@ pub struct ChildKeyAndIdSharedDetails<'a> {
31093109 /// Column of the child table that sorting is done on
31103110 pub sort_by_column : dsl:: Column < ' a > ,
31113111 /// Either `asc` or `desc`
3112- pub direction : & ' static str ,
3112+ pub direction : SortDirection ,
31133113}
31143114
31153115#[ derive( Debug , Clone ) ]
@@ -3164,7 +3164,7 @@ pub enum SortKey<'a> {
31643164 Key {
31653165 column : dsl:: Column < ' a > ,
31663166 value : Option < & ' a str > ,
3167- direction : & ' static str ,
3167+ direction : SortDirection ,
31683168 } ,
31693169 /// Order by some other column; `column` will never be `id`
31703170 ChildKey ( ChildKey < ' a > ) ,
@@ -3266,8 +3266,26 @@ impl<'a> fmt::Display for SortKey<'a> {
32663266 }
32673267}
32683268
3269- const ASC : & str = "asc" ;
3270- const DESC : & str = "desc" ;
3269+ #[ derive( Debug , Clone , Copy ) ]
3270+ pub enum SortDirection {
3271+ Asc ,
3272+ Desc ,
3273+ }
3274+
3275+ impl SortDirection {
3276+ fn as_str ( & self ) -> & ' static str {
3277+ match self {
3278+ SortDirection :: Asc => "asc" ,
3279+ SortDirection :: Desc => "desc" ,
3280+ }
3281+ }
3282+ }
3283+
3284+ impl std:: fmt:: Display for SortDirection {
3285+ fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
3286+ write ! ( f, "{}" , self . as_str( ) )
3287+ }
3288+ }
32713289
32723290impl < ' a > SortKey < ' a > {
32733291 fn new (
@@ -3280,7 +3298,7 @@ impl<'a> SortKey<'a> {
32803298 fn sort_key_from_value < ' a > (
32813299 column : dsl:: Column < ' a > ,
32823300 value : & ' a Value ,
3283- direction : & ' static str ,
3301+ direction : SortDirection ,
32843302 ) -> Result < SortKey < ' a > , QueryExecutionError > {
32853303 let sort_value = value. as_str ( ) ;
32863304
@@ -3295,7 +3313,7 @@ impl<'a> SortKey<'a> {
32953313 table : dsl:: Table < ' a > ,
32963314 attribute : String ,
32973315 filter : Option < & ' a EntityFilter > ,
3298- direction : & ' static str ,
3316+ direction : SortDirection ,
32993317 use_block_column : UseBlockColumn ,
33003318 ) -> Result < SortKey < ' a > , QueryExecutionError > {
33013319 let column = table. column_for_field ( & attribute) ?;
@@ -3314,10 +3332,10 @@ impl<'a> SortKey<'a> {
33143332 }
33153333 } else if column. is_primary_key ( ) {
33163334 let block_column = use_block_column. block_column ( table) ;
3335+ use SortDirection :: * ;
33173336 match direction {
3318- ASC => Ok ( SortKey :: IdAsc ( block_column) ) ,
3319- DESC => Ok ( SortKey :: IdDesc ( block_column) ) ,
3320- _ => unreachable ! ( "direction is 'asc' or 'desc'" ) ,
3337+ Asc => Ok ( SortKey :: IdAsc ( block_column) ) ,
3338+ Desc => Ok ( SortKey :: IdDesc ( block_column) ) ,
33213339 }
33223340 } else {
33233341 Ok ( SortKey :: Key {
@@ -3336,7 +3354,7 @@ impl<'a> SortKey<'a> {
33363354 derived : bool ,
33373355 attribute : String ,
33383356 use_block_column : UseBlockColumn ,
3339- direction : & ' static str ,
3357+ direction : SortDirection ,
33403358 ) -> Result < SortKey < ' a > , QueryExecutionError > {
33413359 let child_table = child_table. child ( 1 ) ;
33423360 let sort_by_column = child_table. column_for_field ( & attribute) ?;
@@ -3375,8 +3393,9 @@ impl<'a> SortKey<'a> {
33753393 let child_pk = child_table. primary_key ( ) ;
33763394 let child_br = child_table. block_column ( ) ;
33773395 let child_at_block = child_table. at_block ( block) ;
3396+ use SortDirection :: * ;
33783397 return match direction {
3379- ASC => Ok ( SortKey :: ChildKey ( ChildKey :: IdAsc (
3398+ Asc => Ok ( SortKey :: ChildKey ( ChildKey :: IdAsc (
33803399 ChildIdDetails {
33813400 child_table,
33823401 child_from,
@@ -3388,7 +3407,7 @@ impl<'a> SortKey<'a> {
33883407 } ,
33893408 use_block_column,
33903409 ) ) ) ,
3391- DESC => Ok ( SortKey :: ChildKey ( ChildKey :: IdDesc (
3410+ Desc => Ok ( SortKey :: ChildKey ( ChildKey :: IdDesc (
33923411 ChildIdDetails {
33933412 child_table,
33943413 child_from,
@@ -3400,7 +3419,6 @@ impl<'a> SortKey<'a> {
34003419 } ,
34013420 use_block_column,
34023421 ) ) ) ,
3403- _ => unreachable ! ( "direction is 'asc' or 'desc'" ) ,
34043422 } ;
34053423 }
34063424
@@ -3426,7 +3444,7 @@ impl<'a> SortKey<'a> {
34263444 parent_table : dsl:: Table < ' a > ,
34273445 entity_types : Vec < EntityType > ,
34283446 child : EntityOrderByChildInfo ,
3429- direction : & ' static str ,
3447+ direction : SortDirection ,
34303448 ) -> Result < Vec < ChildKeyAndIdSharedDetails < ' a > > , QueryExecutionError > {
34313449 assert ! ( entity_types. len( ) < 255 ) ;
34323450 return entity_types
@@ -3497,7 +3515,7 @@ impl<'a> SortKey<'a> {
34973515 child : EntityOrderByChildInfo ,
34983516 entity_types : Vec < EntityType > ,
34993517 use_block_column : UseBlockColumn ,
3500- direction : & ' static str ,
3518+ direction : SortDirection ,
35013519 ) -> Result < SortKey < ' a > , QueryExecutionError > {
35023520 if entity_types. is_empty ( ) {
35033521 return Err ( QueryExecutionError :: ConstraintViolation (
@@ -3514,8 +3532,9 @@ impl<'a> SortKey<'a> {
35143532 "Sorting by fulltext fields" . to_string ( ) ,
35153533 ) )
35163534 } else if sort_by_column. is_primary_key ( ) {
3517- if direction == ASC {
3518- Ok ( SortKey :: ChildKey ( ChildKey :: ManyIdAsc (
3535+ use SortDirection :: * ;
3536+ match direction {
3537+ Asc => Ok ( SortKey :: ChildKey ( ChildKey :: ManyIdAsc (
35193538 build_children_vec (
35203539 layout,
35213540 block,
@@ -3536,9 +3555,8 @@ impl<'a> SortKey<'a> {
35363555 } )
35373556 . collect ( ) ,
35383557 use_block_column,
3539- ) ) )
3540- } else {
3541- Ok ( SortKey :: ChildKey ( ChildKey :: ManyIdDesc (
3558+ ) ) ) ,
3559+ Desc => Ok ( SortKey :: ChildKey ( ChildKey :: ManyIdDesc (
35423560 build_children_vec (
35433561 layout,
35443562 block,
@@ -3559,7 +3577,7 @@ impl<'a> SortKey<'a> {
35593577 } )
35603578 . collect ( ) ,
35613579 use_block_column,
3562- ) ) )
3580+ ) ) ) ,
35633581 }
35643582 } else {
35653583 Ok ( SortKey :: ChildKey ( ChildKey :: Many (
@@ -3601,10 +3619,11 @@ impl<'a> SortKey<'a> {
36013619 UseBlockColumn :: No
36023620 } ;
36033621
3622+ use SortDirection :: * ;
36043623 match order {
3605- EntityOrder :: Ascending ( attr, _) => with_key ( table, attr, filter, ASC , use_block_column) ,
3624+ EntityOrder :: Ascending ( attr, _) => with_key ( table, attr, filter, Asc , use_block_column) ,
36063625 EntityOrder :: Descending ( attr, _) => {
3607- with_key ( table, attr, filter, DESC , use_block_column)
3626+ with_key ( table, attr, filter, Desc , use_block_column)
36083627 }
36093628 EntityOrder :: Default => Ok ( SortKey :: IdAsc ( use_block_column. block_column ( table) ) ) ,
36103629 EntityOrder :: Unordered => Ok ( SortKey :: None ) ,
@@ -3617,7 +3636,7 @@ impl<'a> SortKey<'a> {
36173636 child. derived ,
36183637 child. sort_by_attribute ,
36193638 use_block_column,
3620- ASC ,
3639+ Asc ,
36213640 ) ,
36223641 EntityOrderByChild :: Interface ( child, entity_types) => with_child_interface_key (
36233642 layout,
@@ -3626,7 +3645,7 @@ impl<'a> SortKey<'a> {
36263645 child,
36273646 entity_types,
36283647 use_block_column,
3629- ASC ,
3648+ Asc ,
36303649 ) ,
36313650 } ,
36323651 EntityOrder :: ChildDescending ( kind) => match kind {
@@ -3638,7 +3657,7 @@ impl<'a> SortKey<'a> {
36383657 child. derived ,
36393658 child. sort_by_attribute ,
36403659 use_block_column,
3641- DESC ,
3660+ Desc ,
36423661 ) ,
36433662 EntityOrderByChild :: Interface ( child, entity_types) => with_child_interface_key (
36443663 layout,
@@ -3647,7 +3666,7 @@ impl<'a> SortKey<'a> {
36473666 child,
36483667 entity_types,
36493668 use_block_column,
3650- DESC ,
3669+ Desc ,
36513670 ) ,
36523671 } ,
36533672 }
@@ -3758,6 +3777,7 @@ impl<'a> SortKey<'a> {
37583777 out : & mut AstPass < ' _ , ' b , Pg > ,
37593778 use_sort_key_alias : bool ,
37603779 ) -> QueryResult < ( ) > {
3780+ use SortDirection :: * ;
37613781 match self {
37623782 SortKey :: None => Ok ( ( ) ) ,
37633783 SortKey :: IdAsc ( br_column) => {
@@ -3804,23 +3824,23 @@ impl<'a> SortKey<'a> {
38043824 ChildKey :: Single ( child) => SortKey :: sort_expr (
38053825 & child. sort_by_column ,
38063826 & None ,
3807- child. direction ,
3827+ & child. direction ,
38083828 Some ( "c" ) ,
38093829 use_sort_key_alias,
38103830 out,
38113831 ) ,
38123832 ChildKey :: Many ( parent_pk, children) => SortKey :: multi_sort_expr (
38133833 parent_pk,
38143834 children,
3815- children. first ( ) . unwrap ( ) . direction ,
3835+ & children. first ( ) . unwrap ( ) . direction ,
38163836 out,
38173837 ) ,
38183838
38193839 ChildKey :: ManyIdAsc ( children, use_block_column) => {
3820- SortKey :: multi_sort_id_expr ( children, ASC , * use_block_column, out)
3840+ SortKey :: multi_sort_id_expr ( children, Asc , * use_block_column, out)
38213841 }
38223842 ChildKey :: ManyIdDesc ( children, use_block_column) => {
3823- SortKey :: multi_sort_id_expr ( children, DESC , * use_block_column, out)
3843+ SortKey :: multi_sort_id_expr ( children, Desc , * use_block_column, out)
38243844 }
38253845
38263846 ChildKey :: IdAsc ( child, use_block_column) => {
@@ -3893,7 +3913,7 @@ impl<'a> SortKey<'a> {
38933913 fn sort_expr < ' b > (
38943914 column : & ' b dsl:: Column < ' b > ,
38953915 value : & ' b Option < & str > ,
3896- direction : & str ,
3916+ direction : & ' b SortDirection ,
38973917 rest_prefix : Option < & str > ,
38983918 use_sort_key_alias : bool ,
38993919 out : & mut AstPass < ' _ , ' b , Pg > ,
@@ -3939,14 +3959,14 @@ impl<'a> SortKey<'a> {
39393959 }
39403960 }
39413961 out. push_sql ( " " ) ;
3942- out. push_sql ( direction) ;
3962+ out. push_sql ( direction. as_str ( ) ) ;
39433963 out. push_sql ( ", " ) ;
39443964 if !use_sort_key_alias {
39453965 push_prefix ( rest_prefix, out) ;
39463966 }
39473967 out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
39483968 out. push_sql ( " " ) ;
3949- out. push_sql ( direction) ;
3969+ out. push_sql ( direction. as_str ( ) ) ;
39503970 Ok ( ( ) )
39513971 }
39523972
@@ -3955,7 +3975,7 @@ impl<'a> SortKey<'a> {
39553975 fn multi_sort_expr < ' b > (
39563976 parent_pk : & ' b dsl:: Column < ' b > ,
39573977 children : & ' b [ ChildKeyDetails < ' b > ] ,
3958- direction : & str ,
3978+ direction : & ' b SortDirection ,
39593979 out : & mut AstPass < ' _ , ' b , Pg > ,
39603980 ) -> QueryResult < ( ) > {
39613981 for child in children {
@@ -3990,20 +4010,20 @@ impl<'a> SortKey<'a> {
39904010
39914011 out. push_sql ( ") " ) ;
39924012
3993- out. push_sql ( direction) ;
4013+ out. push_sql ( direction. as_str ( ) ) ;
39944014 out. push_sql ( ", " ) ;
39954015
39964016 parent_pk. walk_ast ( out. reborrow ( ) ) ?;
39974017 out. push_sql ( " " ) ;
3998- out. push_sql ( direction) ;
4018+ out. push_sql ( direction. as_str ( ) ) ;
39994019 Ok ( ( ) )
40004020 }
40014021
40024022 /// Generate
40034023 /// COALESCE(id1, id2) direction, [COALESCE(br_column1, br_column2) direction]
40044024 fn multi_sort_id_expr < ' b > (
40054025 children : & ' b [ ChildIdDetails < ' b > ] ,
4006- direction : & str ,
4026+ direction : SortDirection ,
40074027 use_block_column : UseBlockColumn ,
40084028 out : & mut AstPass < ' _ , ' b , Pg > ,
40094029 ) -> QueryResult < ( ) > {
@@ -4020,7 +4040,7 @@ impl<'a> SortKey<'a> {
40204040 }
40214041 out. push_sql ( ") " ) ;
40224042
4023- out. push_sql ( direction) ;
4043+ out. push_sql ( direction. as_str ( ) ) ;
40244044
40254045 if UseBlockColumn :: Yes == use_block_column {
40264046 out. push_sql ( ", coalesce(" ) ;
@@ -4035,7 +4055,7 @@ impl<'a> SortKey<'a> {
40354055 child. child_br . walk_ast ( out. reborrow ( ) ) ?;
40364056 }
40374057 out. push_sql ( ") " ) ;
4038- out. push_sql ( direction) ;
4058+ out. push_sql ( direction. as_str ( ) ) ;
40394059 }
40404060
40414061 Ok ( ( ) )
0 commit comments