@@ -97,6 +97,7 @@ pub enum UseSegment {
97
97
Ident ( String , Option < String > ) ,
98
98
Slf ( Option < String > ) ,
99
99
Super ( Option < String > ) ,
100
+ Crate ( Option < String > ) ,
100
101
Glob ,
101
102
List ( Vec < UseTree > ) ,
102
103
}
@@ -138,6 +139,7 @@ impl UseSegment {
138
139
UseSegment :: Ident ( ref s, _) => UseSegment :: Ident ( s. clone ( ) , None ) ,
139
140
UseSegment :: Slf ( _) => UseSegment :: Slf ( None ) ,
140
141
UseSegment :: Super ( _) => UseSegment :: Super ( None ) ,
142
+ UseSegment :: Crate ( _) => UseSegment :: Crate ( None ) ,
141
143
_ => self . clone ( ) ,
142
144
}
143
145
}
@@ -154,6 +156,7 @@ impl UseSegment {
154
156
Some ( match name {
155
157
"self" => UseSegment :: Slf ( None ) ,
156
158
"super" => UseSegment :: Super ( None ) ,
159
+ "crate" => UseSegment :: Crate ( None ) ,
157
160
_ => {
158
161
let mod_sep = if modsep { "::" } else { "" } ;
159
162
UseSegment :: Ident ( format ! ( "{}{}" , mod_sep, name) , None )
@@ -207,6 +210,7 @@ impl fmt::Display for UseSegment {
207
210
UseSegment :: Ident ( ref s, _) => write ! ( f, "{}" , s) ,
208
211
UseSegment :: Slf ( ..) => write ! ( f, "self" ) ,
209
212
UseSegment :: Super ( ..) => write ! ( f, "super" ) ,
213
+ UseSegment :: Crate ( ..) => write ! ( f, "crate" ) ,
210
214
UseSegment :: List ( ref list) => {
211
215
write ! ( f, "{{" ) ?;
212
216
for ( i, item) in list. iter ( ) . enumerate ( ) {
@@ -377,6 +381,7 @@ impl UseTree {
377
381
let segment = match name. as_ref ( ) {
378
382
"self" => UseSegment :: Slf ( alias) ,
379
383
"super" => UseSegment :: Super ( alias) ,
384
+ "crate" => UseSegment :: Crate ( alias) ,
380
385
_ => UseSegment :: Ident ( name, alias) ,
381
386
} ;
382
387
@@ -617,7 +622,9 @@ impl Ord for UseSegment {
617
622
}
618
623
619
624
match ( self , other) {
620
- ( & Slf ( ref a) , & Slf ( ref b) ) | ( & Super ( ref a) , & Super ( ref b) ) => a. cmp ( b) ,
625
+ ( & Slf ( ref a) , & Slf ( ref b) )
626
+ | ( & Super ( ref a) , & Super ( ref b) )
627
+ | ( & Crate ( ref a) , & Crate ( ref b) ) => a. cmp ( b) ,
621
628
( & Glob , & Glob ) => Ordering :: Equal ,
622
629
( & Ident ( ref ia, ref aa) , & Ident ( ref ib, ref ab) ) => {
623
630
// snake_case < CamelCase < UPPER_SNAKE_CASE
@@ -659,6 +666,8 @@ impl Ord for UseSegment {
659
666
( _, & Slf ( _) ) => Ordering :: Greater ,
660
667
( & Super ( _) , _) => Ordering :: Less ,
661
668
( _, & Super ( _) ) => Ordering :: Greater ,
669
+ ( & Crate ( _) , _) => Ordering :: Less ,
670
+ ( _, & Crate ( _) ) => Ordering :: Greater ,
662
671
( & Ident ( ..) , _) => Ordering :: Less ,
663
672
( _, & Ident ( ..) ) => Ordering :: Greater ,
664
673
( & Glob , _) => Ordering :: Less ,
@@ -768,6 +777,8 @@ impl Rewrite for UseSegment {
768
777
UseSegment :: Slf ( None ) => "self" . to_owned ( ) ,
769
778
UseSegment :: Super ( Some ( ref rename) ) => format ! ( "super as {}" , rename) ,
770
779
UseSegment :: Super ( None ) => "super" . to_owned ( ) ,
780
+ UseSegment :: Crate ( Some ( ref rename) ) => format ! ( "crate as {}" , rename) ,
781
+ UseSegment :: Crate ( None ) => "crate" . to_owned ( ) ,
771
782
UseSegment :: Glob => "*" . to_owned ( ) ,
772
783
UseSegment :: List ( ref use_tree_list) => rewrite_nested_use_tree (
773
784
context,
@@ -830,18 +841,28 @@ mod test {
830
841
if !buf. is_empty ( ) {
831
842
let mut alias = None ;
832
843
swap ( alias_buf, & mut alias) ;
833
- if buf == "self" {
834
- result. push ( UseSegment :: Slf ( alias) ) ;
835
- * buf = String :: new ( ) ;
836
- * alias_buf = None ;
837
- } else if buf == "super" {
838
- result. push ( UseSegment :: Super ( alias) ) ;
839
- * buf = String :: new ( ) ;
840
- * alias_buf = None ;
841
- } else {
842
- let mut name = String :: new ( ) ;
843
- swap ( buf, & mut name) ;
844
- result. push ( UseSegment :: Ident ( name, alias) ) ;
844
+
845
+ match buf. as_ref ( ) {
846
+ "self" => {
847
+ result. push ( UseSegment :: Slf ( alias) ) ;
848
+ * buf = String :: new ( ) ;
849
+ * alias_buf = None ;
850
+ }
851
+ "super" => {
852
+ result. push ( UseSegment :: Super ( alias) ) ;
853
+ * buf = String :: new ( ) ;
854
+ * alias_buf = None ;
855
+ }
856
+ "crate" => {
857
+ result. push ( UseSegment :: Crate ( alias) ) ;
858
+ * buf = String :: new ( ) ;
859
+ * alias_buf = None ;
860
+ }
861
+ _ => {
862
+ let mut name = String :: new ( ) ;
863
+ swap ( buf, & mut name) ;
864
+ result. push ( UseSegment :: Ident ( name, alias) ) ;
865
+ }
845
866
}
846
867
}
847
868
}
0 commit comments