@@ -3056,7 +3056,7 @@ pub struct ChildKeyDetails<'a> {
30563056 /// Column of the child table that sorting is done on
30573057 pub sort_by_column : dsl:: Column < ' a > ,
30583058 /// Either `asc` or `desc`
3059- pub direction : & ' static str ,
3059+ pub direction : SortDirection ,
30603060}
30613061
30623062#[ derive( Debug , Clone ) ]
@@ -3074,7 +3074,7 @@ pub struct ChildKeyAndIdSharedDetails<'a> {
30743074 /// Column of the child table that sorting is done on
30753075 pub sort_by_column : dsl:: Column < ' a > ,
30763076 /// Either `asc` or `desc`
3077- pub direction : & ' static str ,
3077+ pub direction : SortDirection ,
30783078}
30793079
30803080#[ allow( unused) ]
@@ -3130,7 +3130,7 @@ pub enum SortKey<'a> {
31303130 Key {
31313131 column : dsl:: Column < ' a > ,
31323132 value : Option < & ' a str > ,
3133- direction : & ' static str ,
3133+ direction : SortDirection ,
31343134 } ,
31353135 /// Order by some other column; `column` will never be `id`
31363136 ChildKey ( ChildKey < ' a > ) ,
@@ -3232,8 +3232,26 @@ impl<'a> fmt::Display for SortKey<'a> {
32323232 }
32333233}
32343234
3235- const ASC : & str = "asc" ;
3236- const DESC : & str = "desc" ;
3235+ #[ derive( Debug , Clone , Copy ) ]
3236+ pub enum SortDirection {
3237+ Asc ,
3238+ Desc ,
3239+ }
3240+
3241+ impl SortDirection {
3242+ fn as_str ( & self ) -> & ' static str {
3243+ match self {
3244+ SortDirection :: Asc => "asc" ,
3245+ SortDirection :: Desc => "desc" ,
3246+ }
3247+ }
3248+ }
3249+
3250+ impl std:: fmt:: Display for SortDirection {
3251+ fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
3252+ write ! ( f, "{}" , self . as_str( ) )
3253+ }
3254+ }
32373255
32383256impl < ' a > SortKey < ' a > {
32393257 fn new (
@@ -3246,7 +3264,7 @@ impl<'a> SortKey<'a> {
32463264 fn sort_key_from_value < ' a > (
32473265 column : dsl:: Column < ' a > ,
32483266 value : & ' a Value ,
3249- direction : & ' static str ,
3267+ direction : SortDirection ,
32503268 ) -> Result < SortKey < ' a > , QueryExecutionError > {
32513269 let sort_value = value. as_str ( ) ;
32523270
@@ -3261,7 +3279,7 @@ impl<'a> SortKey<'a> {
32613279 table : dsl:: Table < ' a > ,
32623280 attribute : String ,
32633281 filter : Option < & ' a EntityFilter > ,
3264- direction : & ' static str ,
3282+ direction : SortDirection ,
32653283 use_block_column : UseBlockColumn ,
32663284 ) -> Result < SortKey < ' a > , QueryExecutionError > {
32673285 let column = table. column_for_field ( & attribute) ?;
@@ -3280,10 +3298,10 @@ impl<'a> SortKey<'a> {
32803298 }
32813299 } else if column. is_primary_key ( ) {
32823300 let block_column = use_block_column. block_column ( table) ;
3301+ use SortDirection :: * ;
32833302 match direction {
3284- ASC => Ok ( SortKey :: IdAsc ( block_column) ) ,
3285- DESC => Ok ( SortKey :: IdDesc ( block_column) ) ,
3286- _ => unreachable ! ( "direction is 'asc' or 'desc'" ) ,
3303+ Asc => Ok ( SortKey :: IdAsc ( block_column) ) ,
3304+ Desc => Ok ( SortKey :: IdDesc ( block_column) ) ,
32873305 }
32883306 } else {
32893307 Ok ( SortKey :: Key {
@@ -3302,7 +3320,7 @@ impl<'a> SortKey<'a> {
33023320 derived : bool ,
33033321 attribute : String ,
33043322 use_block_column : UseBlockColumn ,
3305- direction : & ' static str ,
3323+ direction : SortDirection ,
33063324 ) -> Result < SortKey < ' a > , QueryExecutionError > {
33073325 let child_table = child_table. child ( 1 ) ;
33083326 let sort_by_column = child_table. column_for_field ( & attribute) ?;
@@ -3341,8 +3359,9 @@ impl<'a> SortKey<'a> {
33413359 let child_pk = child_table. primary_key ( ) ;
33423360 let child_br = child_table. block_column ( ) ;
33433361 let child_at_block = child_table. at_block ( block) ;
3362+ use SortDirection :: * ;
33443363 return match direction {
3345- ASC => Ok ( SortKey :: ChildKey ( ChildKey :: IdAsc (
3364+ Asc => Ok ( SortKey :: ChildKey ( ChildKey :: IdAsc (
33463365 ChildIdDetails {
33473366 child_table,
33483367 child_from,
@@ -3354,7 +3373,7 @@ impl<'a> SortKey<'a> {
33543373 } ,
33553374 use_block_column,
33563375 ) ) ) ,
3357- DESC => Ok ( SortKey :: ChildKey ( ChildKey :: IdDesc (
3376+ Desc => Ok ( SortKey :: ChildKey ( ChildKey :: IdDesc (
33583377 ChildIdDetails {
33593378 child_table,
33603379 child_from,
@@ -3366,7 +3385,6 @@ impl<'a> SortKey<'a> {
33663385 } ,
33673386 use_block_column,
33683387 ) ) ) ,
3369- _ => unreachable ! ( "direction is 'asc' or 'desc'" ) ,
33703388 } ;
33713389 }
33723390
@@ -3392,7 +3410,7 @@ impl<'a> SortKey<'a> {
33923410 parent_table : dsl:: Table < ' a > ,
33933411 entity_types : Vec < EntityType > ,
33943412 child : EntityOrderByChildInfo ,
3395- direction : & ' static str ,
3413+ direction : SortDirection ,
33963414 ) -> Result < Vec < ChildKeyAndIdSharedDetails < ' a > > , QueryExecutionError > {
33973415 assert ! ( entity_types. len( ) < 255 ) ;
33983416 return entity_types
@@ -3463,7 +3481,7 @@ impl<'a> SortKey<'a> {
34633481 child : EntityOrderByChildInfo ,
34643482 entity_types : Vec < EntityType > ,
34653483 use_block_column : UseBlockColumn ,
3466- direction : & ' static str ,
3484+ direction : SortDirection ,
34673485 ) -> Result < SortKey < ' a > , QueryExecutionError > {
34683486 if entity_types. is_empty ( ) {
34693487 return Err ( QueryExecutionError :: ConstraintViolation (
@@ -3480,8 +3498,9 @@ impl<'a> SortKey<'a> {
34803498 "Sorting by fulltext fields" . to_string ( ) ,
34813499 ) )
34823500 } else if sort_by_column. is_primary_key ( ) {
3483- if direction == ASC {
3484- Ok ( SortKey :: ChildKey ( ChildKey :: ManyIdAsc (
3501+ use SortDirection :: * ;
3502+ match direction {
3503+ Asc => Ok ( SortKey :: ChildKey ( ChildKey :: ManyIdAsc (
34853504 build_children_vec (
34863505 layout,
34873506 block,
@@ -3502,9 +3521,8 @@ impl<'a> SortKey<'a> {
35023521 } )
35033522 . collect ( ) ,
35043523 use_block_column,
3505- ) ) )
3506- } else {
3507- Ok ( SortKey :: ChildKey ( ChildKey :: ManyIdDesc (
3524+ ) ) ) ,
3525+ Desc => Ok ( SortKey :: ChildKey ( ChildKey :: ManyIdDesc (
35083526 build_children_vec (
35093527 layout,
35103528 block,
@@ -3525,7 +3543,7 @@ impl<'a> SortKey<'a> {
35253543 } )
35263544 . collect ( ) ,
35273545 use_block_column,
3528- ) ) )
3546+ ) ) ) ,
35293547 }
35303548 } else {
35313549 Ok ( SortKey :: ChildKey ( ChildKey :: Many (
@@ -3567,10 +3585,11 @@ impl<'a> SortKey<'a> {
35673585 UseBlockColumn :: No
35683586 } ;
35693587
3588+ use SortDirection :: * ;
35703589 match order {
3571- EntityOrder :: Ascending ( attr, _) => with_key ( table, attr, filter, ASC , use_block_column) ,
3590+ EntityOrder :: Ascending ( attr, _) => with_key ( table, attr, filter, Asc , use_block_column) ,
35723591 EntityOrder :: Descending ( attr, _) => {
3573- with_key ( table, attr, filter, DESC , use_block_column)
3592+ with_key ( table, attr, filter, Desc , use_block_column)
35743593 }
35753594 EntityOrder :: Default => Ok ( SortKey :: IdAsc ( use_block_column. block_column ( table) ) ) ,
35763595 EntityOrder :: Unordered => Ok ( SortKey :: None ) ,
@@ -3583,7 +3602,7 @@ impl<'a> SortKey<'a> {
35833602 child. derived ,
35843603 child. sort_by_attribute ,
35853604 use_block_column,
3586- ASC ,
3605+ Asc ,
35873606 ) ,
35883607 EntityOrderByChild :: Interface ( child, entity_types) => with_child_interface_key (
35893608 layout,
@@ -3592,7 +3611,7 @@ impl<'a> SortKey<'a> {
35923611 child,
35933612 entity_types,
35943613 use_block_column,
3595- ASC ,
3614+ Asc ,
35963615 ) ,
35973616 } ,
35983617 EntityOrder :: ChildDescending ( kind) => match kind {
@@ -3604,7 +3623,7 @@ impl<'a> SortKey<'a> {
36043623 child. derived ,
36053624 child. sort_by_attribute ,
36063625 use_block_column,
3607- DESC ,
3626+ Desc ,
36083627 ) ,
36093628 EntityOrderByChild :: Interface ( child, entity_types) => with_child_interface_key (
36103629 layout,
@@ -3613,7 +3632,7 @@ impl<'a> SortKey<'a> {
36133632 child,
36143633 entity_types,
36153634 use_block_column,
3616- DESC ,
3635+ Desc ,
36173636 ) ,
36183637 } ,
36193638 }
@@ -3724,6 +3743,7 @@ impl<'a> SortKey<'a> {
37243743 out : & mut AstPass < ' _ , ' b , Pg > ,
37253744 use_sort_key_alias : bool ,
37263745 ) -> QueryResult < ( ) > {
3746+ use SortDirection :: * ;
37273747 match self {
37283748 SortKey :: None => Ok ( ( ) ) ,
37293749 SortKey :: IdAsc ( br_column) => {
@@ -3770,23 +3790,23 @@ impl<'a> SortKey<'a> {
37703790 ChildKey :: Single ( child) => SortKey :: sort_expr (
37713791 & child. sort_by_column ,
37723792 & None ,
3773- child. direction ,
3793+ & child. direction ,
37743794 Some ( "c" ) ,
37753795 use_sort_key_alias,
37763796 out,
37773797 ) ,
37783798 ChildKey :: Many ( parent_pk, children) => SortKey :: multi_sort_expr (
37793799 parent_pk,
37803800 children,
3781- children. first ( ) . unwrap ( ) . direction ,
3801+ & children. first ( ) . unwrap ( ) . direction ,
37823802 out,
37833803 ) ,
37843804
37853805 ChildKey :: ManyIdAsc ( children, use_block_column) => {
3786- SortKey :: multi_sort_id_expr ( children, ASC , * use_block_column, out)
3806+ SortKey :: multi_sort_id_expr ( children, Asc , * use_block_column, out)
37873807 }
37883808 ChildKey :: ManyIdDesc ( children, use_block_column) => {
3789- SortKey :: multi_sort_id_expr ( children, DESC , * use_block_column, out)
3809+ SortKey :: multi_sort_id_expr ( children, Desc , * use_block_column, out)
37903810 }
37913811
37923812 ChildKey :: IdAsc ( child, use_block_column) => {
@@ -3859,7 +3879,7 @@ impl<'a> SortKey<'a> {
38593879 fn sort_expr < ' b > (
38603880 column : & ' b dsl:: Column < ' b > ,
38613881 value : & ' b Option < & str > ,
3862- direction : & str ,
3882+ direction : & ' b SortDirection ,
38633883 rest_prefix : Option < & str > ,
38643884 use_sort_key_alias : bool ,
38653885 out : & mut AstPass < ' _ , ' b , Pg > ,
@@ -3905,14 +3925,14 @@ impl<'a> SortKey<'a> {
39053925 }
39063926 }
39073927 out. push_sql ( " " ) ;
3908- out. push_sql ( direction) ;
3928+ out. push_sql ( direction. as_str ( ) ) ;
39093929 out. push_sql ( ", " ) ;
39103930 if !use_sort_key_alias {
39113931 push_prefix ( rest_prefix, out) ;
39123932 }
39133933 out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
39143934 out. push_sql ( " " ) ;
3915- out. push_sql ( direction) ;
3935+ out. push_sql ( direction. as_str ( ) ) ;
39163936 Ok ( ( ) )
39173937 }
39183938
@@ -3921,7 +3941,7 @@ impl<'a> SortKey<'a> {
39213941 fn multi_sort_expr < ' b > (
39223942 parent_pk : & ' b dsl:: Column < ' b > ,
39233943 children : & ' b [ ChildKeyDetails < ' b > ] ,
3924- direction : & str ,
3944+ direction : & ' b SortDirection ,
39253945 out : & mut AstPass < ' _ , ' b , Pg > ,
39263946 ) -> QueryResult < ( ) > {
39273947 for child in children {
@@ -3956,20 +3976,20 @@ impl<'a> SortKey<'a> {
39563976
39573977 out. push_sql ( ") " ) ;
39583978
3959- out. push_sql ( direction) ;
3979+ out. push_sql ( direction. as_str ( ) ) ;
39603980 out. push_sql ( ", " ) ;
39613981
39623982 parent_pk. walk_ast ( out. reborrow ( ) ) ?;
39633983 out. push_sql ( " " ) ;
3964- out. push_sql ( direction) ;
3984+ out. push_sql ( direction. as_str ( ) ) ;
39653985 Ok ( ( ) )
39663986 }
39673987
39683988 /// Generate
39693989 /// COALESCE(id1, id2) direction, [COALESCE(br_column1, br_column2) direction]
39703990 fn multi_sort_id_expr < ' b > (
39713991 children : & ' b [ ChildIdDetails < ' b > ] ,
3972- direction : & str ,
3992+ direction : SortDirection ,
39733993 use_block_column : UseBlockColumn ,
39743994 out : & mut AstPass < ' _ , ' b , Pg > ,
39753995 ) -> QueryResult < ( ) > {
@@ -3986,7 +4006,7 @@ impl<'a> SortKey<'a> {
39864006 }
39874007 out. push_sql ( ") " ) ;
39884008
3989- out. push_sql ( direction) ;
4009+ out. push_sql ( direction. as_str ( ) ) ;
39904010
39914011 if UseBlockColumn :: Yes == use_block_column {
39924012 out. push_sql ( ", coalesce(" ) ;
@@ -4001,7 +4021,7 @@ impl<'a> SortKey<'a> {
40014021 child. child_br . walk_ast ( out. reborrow ( ) ) ?;
40024022 }
40034023 out. push_sql ( ") " ) ;
4004- out. push_sql ( direction) ;
4024+ out. push_sql ( direction. as_str ( ) ) ;
40054025 }
40064026
40074027 Ok ( ( ) )
0 commit comments