@@ -3092,7 +3092,7 @@ pub struct ChildKeyDetails<'a> {
30923092 /// Column of the child table that sorting is done on
30933093 pub sort_by_column : dsl:: Column < ' a > ,
30943094 /// Either `asc` or `desc`
3095- pub direction : & ' static str ,
3095+ pub direction : SortDirection ,
30963096}
30973097
30983098#[ derive( Debug , Clone ) ]
@@ -3110,7 +3110,7 @@ pub struct ChildKeyAndIdSharedDetails<'a> {
31103110 /// Column of the child table that sorting is done on
31113111 pub sort_by_column : dsl:: Column < ' a > ,
31123112 /// Either `asc` or `desc`
3113- pub direction : & ' static str ,
3113+ pub direction : SortDirection ,
31143114}
31153115
31163116#[ derive( Debug , Clone ) ]
@@ -3165,7 +3165,7 @@ pub enum SortKey<'a> {
31653165 Key {
31663166 column : dsl:: Column < ' a > ,
31673167 value : Option < & ' a str > ,
3168- direction : & ' static str ,
3168+ direction : SortDirection ,
31693169 } ,
31703170 /// Order by some other column; `column` will never be `id`
31713171 ChildKey ( ChildKey < ' a > ) ,
@@ -3267,8 +3267,26 @@ impl<'a> fmt::Display for SortKey<'a> {
32673267 }
32683268}
32693269
3270- const ASC : & str = "asc" ;
3271- const DESC : & str = "desc" ;
3270+ #[ derive( Debug , Clone , Copy ) ]
3271+ pub enum SortDirection {
3272+ Asc ,
3273+ Desc ,
3274+ }
3275+
3276+ impl SortDirection {
3277+ fn as_str ( & self ) -> & ' static str {
3278+ match self {
3279+ SortDirection :: Asc => "asc" ,
3280+ SortDirection :: Desc => "desc" ,
3281+ }
3282+ }
3283+ }
3284+
3285+ impl std:: fmt:: Display for SortDirection {
3286+ fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
3287+ write ! ( f, "{}" , self . as_str( ) )
3288+ }
3289+ }
32723290
32733291impl < ' a > SortKey < ' a > {
32743292 fn new (
@@ -3281,7 +3299,7 @@ impl<'a> SortKey<'a> {
32813299 fn sort_key_from_value < ' a > (
32823300 column : dsl:: Column < ' a > ,
32833301 value : & ' a Value ,
3284- direction : & ' static str ,
3302+ direction : SortDirection ,
32853303 ) -> Result < SortKey < ' a > , QueryExecutionError > {
32863304 let sort_value = value. as_str ( ) ;
32873305
@@ -3296,7 +3314,7 @@ impl<'a> SortKey<'a> {
32963314 table : dsl:: Table < ' a > ,
32973315 attribute : String ,
32983316 filter : Option < & ' a EntityFilter > ,
3299- direction : & ' static str ,
3317+ direction : SortDirection ,
33003318 use_block_column : UseBlockColumn ,
33013319 ) -> Result < SortKey < ' a > , QueryExecutionError > {
33023320 let column = table. column_for_field ( & attribute) ?;
@@ -3315,10 +3333,10 @@ impl<'a> SortKey<'a> {
33153333 }
33163334 } else if column. is_primary_key ( ) {
33173335 let block_column = use_block_column. block_column ( table) ;
3336+ use SortDirection :: * ;
33183337 match direction {
3319- ASC => Ok ( SortKey :: IdAsc ( block_column) ) ,
3320- DESC => Ok ( SortKey :: IdDesc ( block_column) ) ,
3321- _ => unreachable ! ( "direction is 'asc' or 'desc'" ) ,
3338+ Asc => Ok ( SortKey :: IdAsc ( block_column) ) ,
3339+ Desc => Ok ( SortKey :: IdDesc ( block_column) ) ,
33223340 }
33233341 } else {
33243342 Ok ( SortKey :: Key {
@@ -3337,7 +3355,7 @@ impl<'a> SortKey<'a> {
33373355 derived : bool ,
33383356 attribute : String ,
33393357 use_block_column : UseBlockColumn ,
3340- direction : & ' static str ,
3358+ direction : SortDirection ,
33413359 ) -> Result < SortKey < ' a > , QueryExecutionError > {
33423360 let child_table = child_table. child ( 1 ) ;
33433361 let sort_by_column = child_table. column_for_field ( & attribute) ?;
@@ -3376,8 +3394,9 @@ impl<'a> SortKey<'a> {
33763394 let child_pk = child_table. primary_key ( ) ;
33773395 let child_br = child_table. block_column ( ) ;
33783396 let child_at_block = child_table. at_block ( block) ;
3397+ use SortDirection :: * ;
33793398 return match direction {
3380- ASC => Ok ( SortKey :: ChildKey ( ChildKey :: IdAsc (
3399+ Asc => Ok ( SortKey :: ChildKey ( ChildKey :: IdAsc (
33813400 ChildIdDetails {
33823401 child_table,
33833402 child_from,
@@ -3389,7 +3408,7 @@ impl<'a> SortKey<'a> {
33893408 } ,
33903409 use_block_column,
33913410 ) ) ) ,
3392- DESC => Ok ( SortKey :: ChildKey ( ChildKey :: IdDesc (
3411+ Desc => Ok ( SortKey :: ChildKey ( ChildKey :: IdDesc (
33933412 ChildIdDetails {
33943413 child_table,
33953414 child_from,
@@ -3401,7 +3420,6 @@ impl<'a> SortKey<'a> {
34013420 } ,
34023421 use_block_column,
34033422 ) ) ) ,
3404- _ => unreachable ! ( "direction is 'asc' or 'desc'" ) ,
34053423 } ;
34063424 }
34073425
@@ -3427,7 +3445,7 @@ impl<'a> SortKey<'a> {
34273445 parent_table : dsl:: Table < ' a > ,
34283446 entity_types : Vec < EntityType > ,
34293447 child : EntityOrderByChildInfo ,
3430- direction : & ' static str ,
3448+ direction : SortDirection ,
34313449 ) -> Result < Vec < ChildKeyAndIdSharedDetails < ' a > > , QueryExecutionError > {
34323450 assert ! ( entity_types. len( ) < 255 ) ;
34333451 return entity_types
@@ -3498,7 +3516,7 @@ impl<'a> SortKey<'a> {
34983516 child : EntityOrderByChildInfo ,
34993517 entity_types : Vec < EntityType > ,
35003518 use_block_column : UseBlockColumn ,
3501- direction : & ' static str ,
3519+ direction : SortDirection ,
35023520 ) -> Result < SortKey < ' a > , QueryExecutionError > {
35033521 if entity_types. is_empty ( ) {
35043522 return Err ( QueryExecutionError :: ConstraintViolation (
@@ -3515,8 +3533,9 @@ impl<'a> SortKey<'a> {
35153533 "Sorting by fulltext fields" . to_string ( ) ,
35163534 ) )
35173535 } else if sort_by_column. is_primary_key ( ) {
3518- if direction == ASC {
3519- Ok ( SortKey :: ChildKey ( ChildKey :: ManyIdAsc (
3536+ use SortDirection :: * ;
3537+ match direction {
3538+ Asc => Ok ( SortKey :: ChildKey ( ChildKey :: ManyIdAsc (
35203539 build_children_vec (
35213540 layout,
35223541 block,
@@ -3537,9 +3556,8 @@ impl<'a> SortKey<'a> {
35373556 } )
35383557 . collect ( ) ,
35393558 use_block_column,
3540- ) ) )
3541- } else {
3542- Ok ( SortKey :: ChildKey ( ChildKey :: ManyIdDesc (
3559+ ) ) ) ,
3560+ Desc => Ok ( SortKey :: ChildKey ( ChildKey :: ManyIdDesc (
35433561 build_children_vec (
35443562 layout,
35453563 block,
@@ -3560,7 +3578,7 @@ impl<'a> SortKey<'a> {
35603578 } )
35613579 . collect ( ) ,
35623580 use_block_column,
3563- ) ) )
3581+ ) ) ) ,
35643582 }
35653583 } else {
35663584 Ok ( SortKey :: ChildKey ( ChildKey :: Many (
@@ -3602,10 +3620,11 @@ impl<'a> SortKey<'a> {
36023620 UseBlockColumn :: No
36033621 } ;
36043622
3623+ use SortDirection :: * ;
36053624 match order {
3606- EntityOrder :: Ascending ( attr, _) => with_key ( table, attr, filter, ASC , use_block_column) ,
3625+ EntityOrder :: Ascending ( attr, _) => with_key ( table, attr, filter, Asc , use_block_column) ,
36073626 EntityOrder :: Descending ( attr, _) => {
3608- with_key ( table, attr, filter, DESC , use_block_column)
3627+ with_key ( table, attr, filter, Desc , use_block_column)
36093628 }
36103629 EntityOrder :: Default => Ok ( SortKey :: IdAsc ( use_block_column. block_column ( table) ) ) ,
36113630 EntityOrder :: Unordered => Ok ( SortKey :: None ) ,
@@ -3618,7 +3637,7 @@ impl<'a> SortKey<'a> {
36183637 child. derived ,
36193638 child. sort_by_attribute ,
36203639 use_block_column,
3621- ASC ,
3640+ Asc ,
36223641 ) ,
36233642 EntityOrderByChild :: Interface ( child, entity_types) => with_child_interface_key (
36243643 layout,
@@ -3627,7 +3646,7 @@ impl<'a> SortKey<'a> {
36273646 child,
36283647 entity_types,
36293648 use_block_column,
3630- ASC ,
3649+ Asc ,
36313650 ) ,
36323651 } ,
36333652 EntityOrder :: ChildDescending ( kind) => match kind {
@@ -3639,7 +3658,7 @@ impl<'a> SortKey<'a> {
36393658 child. derived ,
36403659 child. sort_by_attribute ,
36413660 use_block_column,
3642- DESC ,
3661+ Desc ,
36433662 ) ,
36443663 EntityOrderByChild :: Interface ( child, entity_types) => with_child_interface_key (
36453664 layout,
@@ -3648,7 +3667,7 @@ impl<'a> SortKey<'a> {
36483667 child,
36493668 entity_types,
36503669 use_block_column,
3651- DESC ,
3670+ Desc ,
36523671 ) ,
36533672 } ,
36543673 }
@@ -3759,6 +3778,7 @@ impl<'a> SortKey<'a> {
37593778 out : & mut AstPass < ' _ , ' b , Pg > ,
37603779 use_sort_key_alias : bool ,
37613780 ) -> QueryResult < ( ) > {
3781+ use SortDirection :: * ;
37623782 match self {
37633783 SortKey :: None => Ok ( ( ) ) ,
37643784 SortKey :: IdAsc ( br_column) => {
@@ -3805,23 +3825,23 @@ impl<'a> SortKey<'a> {
38053825 ChildKey :: Single ( child) => SortKey :: sort_expr (
38063826 & child. sort_by_column ,
38073827 & None ,
3808- child. direction ,
3828+ & child. direction ,
38093829 Some ( "c" ) ,
38103830 use_sort_key_alias,
38113831 out,
38123832 ) ,
38133833 ChildKey :: Many ( parent_pk, children) => SortKey :: multi_sort_expr (
38143834 parent_pk,
38153835 children,
3816- children. first ( ) . unwrap ( ) . direction ,
3836+ & children. first ( ) . unwrap ( ) . direction ,
38173837 out,
38183838 ) ,
38193839
38203840 ChildKey :: ManyIdAsc ( children, use_block_column) => {
3821- SortKey :: multi_sort_id_expr ( children, ASC , * use_block_column, out)
3841+ SortKey :: multi_sort_id_expr ( children, Asc , * use_block_column, out)
38223842 }
38233843 ChildKey :: ManyIdDesc ( children, use_block_column) => {
3824- SortKey :: multi_sort_id_expr ( children, DESC , * use_block_column, out)
3844+ SortKey :: multi_sort_id_expr ( children, Desc , * use_block_column, out)
38253845 }
38263846
38273847 ChildKey :: IdAsc ( child, use_block_column) => {
@@ -3894,7 +3914,7 @@ impl<'a> SortKey<'a> {
38943914 fn sort_expr < ' b > (
38953915 column : & ' b dsl:: Column < ' b > ,
38963916 value : & ' b Option < & str > ,
3897- direction : & str ,
3917+ direction : & ' b SortDirection ,
38983918 rest_prefix : Option < & str > ,
38993919 use_sort_key_alias : bool ,
39003920 out : & mut AstPass < ' _ , ' b , Pg > ,
@@ -3940,14 +3960,14 @@ impl<'a> SortKey<'a> {
39403960 }
39413961 }
39423962 out. push_sql ( " " ) ;
3943- out. push_sql ( direction) ;
3963+ out. push_sql ( direction. as_str ( ) ) ;
39443964 out. push_sql ( ", " ) ;
39453965 if !use_sort_key_alias {
39463966 push_prefix ( rest_prefix, out) ;
39473967 }
39483968 out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
39493969 out. push_sql ( " " ) ;
3950- out. push_sql ( direction) ;
3970+ out. push_sql ( direction. as_str ( ) ) ;
39513971 Ok ( ( ) )
39523972 }
39533973
@@ -3956,7 +3976,7 @@ impl<'a> SortKey<'a> {
39563976 fn multi_sort_expr < ' b > (
39573977 parent_pk : & ' b dsl:: Column < ' b > ,
39583978 children : & ' b [ ChildKeyDetails < ' b > ] ,
3959- direction : & str ,
3979+ direction : & ' b SortDirection ,
39603980 out : & mut AstPass < ' _ , ' b , Pg > ,
39613981 ) -> QueryResult < ( ) > {
39623982 for child in children {
@@ -3991,20 +4011,20 @@ impl<'a> SortKey<'a> {
39914011
39924012 out. push_sql ( ") " ) ;
39934013
3994- out. push_sql ( direction) ;
4014+ out. push_sql ( direction. as_str ( ) ) ;
39954015 out. push_sql ( ", " ) ;
39964016
39974017 parent_pk. walk_ast ( out. reborrow ( ) ) ?;
39984018 out. push_sql ( " " ) ;
3999- out. push_sql ( direction) ;
4019+ out. push_sql ( direction. as_str ( ) ) ;
40004020 Ok ( ( ) )
40014021 }
40024022
40034023 /// Generate
40044024 /// COALESCE(id1, id2) direction, [COALESCE(br_column1, br_column2) direction]
40054025 fn multi_sort_id_expr < ' b > (
40064026 children : & ' b [ ChildIdDetails < ' b > ] ,
4007- direction : & str ,
4027+ direction : SortDirection ,
40084028 use_block_column : UseBlockColumn ,
40094029 out : & mut AstPass < ' _ , ' b , Pg > ,
40104030 ) -> QueryResult < ( ) > {
@@ -4021,7 +4041,7 @@ impl<'a> SortKey<'a> {
40214041 }
40224042 out. push_sql ( ") " ) ;
40234043
4024- out. push_sql ( direction) ;
4044+ out. push_sql ( direction. as_str ( ) ) ;
40254045
40264046 if UseBlockColumn :: Yes == use_block_column {
40274047 out. push_sql ( ", coalesce(" ) ;
@@ -4036,7 +4056,7 @@ impl<'a> SortKey<'a> {
40364056 child. child_br . walk_ast ( out. reborrow ( ) ) ?;
40374057 }
40384058 out. push_sql ( ") " ) ;
4039- out. push_sql ( direction) ;
4059+ out. push_sql ( direction. as_str ( ) ) ;
40404060 }
40414061
40424062 Ok ( ( ) )
0 commit comments