@@ -8,7 +8,7 @@ use rustc_span::BytePos;
8
8
use crate :: comment:: { find_comment_end, rewrite_comment, FindUncommented } ;
9
9
use crate :: config:: lists:: * ;
10
10
use crate :: config:: { Config , IndentStyle } ;
11
- use crate :: rewrite:: { RewriteContext , RewriteResult } ;
11
+ use crate :: rewrite:: { RewriteContext , RewriteError , RewriteErrorExt , RewriteResult } ;
12
12
use crate :: shape:: { Indent , Shape } ;
13
13
use crate :: utils:: {
14
14
count_newlines, first_line_width, last_line_width, mk_sp, starts_with_newline,
@@ -125,18 +125,18 @@ pub(crate) struct ListItem {
125
125
pub ( crate ) pre_comment_style : ListItemCommentStyle ,
126
126
// Item should include attributes and doc comments. None indicates a failed
127
127
// rewrite.
128
- pub ( crate ) item : Option < String > ,
128
+ pub ( crate ) item : RewriteResult ,
129
129
pub ( crate ) post_comment : Option < String > ,
130
130
// Whether there is extra whitespace before this item.
131
131
pub ( crate ) new_lines : bool ,
132
132
}
133
133
134
134
impl ListItem {
135
- pub ( crate ) fn empty ( ) -> ListItem {
135
+ pub ( crate ) fn from_item ( item : RewriteResult ) -> ListItem {
136
136
ListItem {
137
137
pre_comment : None ,
138
138
pre_comment_style : ListItemCommentStyle :: None ,
139
- item : None ,
139
+ item : item ,
140
140
post_comment : None ,
141
141
new_lines : false ,
142
142
}
@@ -185,7 +185,7 @@ impl ListItem {
185
185
ListItem {
186
186
pre_comment : None ,
187
187
pre_comment_style : ListItemCommentStyle :: None ,
188
- item : Some ( s. into ( ) ) ,
188
+ item : Ok ( s. into ( ) ) ,
189
189
post_comment : None ,
190
190
new_lines : false ,
191
191
}
@@ -197,7 +197,11 @@ impl ListItem {
197
197
!matches ! ( * s, Some ( ref s) if !s. is_empty( ) )
198
198
}
199
199
200
- !( empty ( & self . pre_comment ) && empty ( & self . item ) && empty ( & self . post_comment ) )
200
+ fn empty_result ( s : & RewriteResult ) -> bool {
201
+ !matches ! ( * s, Ok ( ref s) if !s. is_empty( ) )
202
+ }
203
+
204
+ !( empty ( & self . pre_comment ) && empty_result ( & self . item ) && empty ( & self . post_comment ) )
201
205
}
202
206
}
203
207
@@ -257,7 +261,7 @@ where
257
261
}
258
262
259
263
// Format a list of commented items into a string.
260
- pub ( crate ) fn write_list < I , T > ( items : I , formatting : & ListFormatting < ' _ > ) -> Option < String >
264
+ pub ( crate ) fn write_list < I , T > ( items : I , formatting : & ListFormatting < ' _ > ) -> RewriteResult
261
265
where
262
266
I : IntoIterator < Item = T > + Clone ,
263
267
T : AsRef < ListItem > ,
@@ -281,8 +285,7 @@ where
281
285
let indent_str = & formatting. shape . indent . to_string ( formatting. config ) ;
282
286
while let Some ( ( i, item) ) = iter. next ( ) {
283
287
let item = item. as_ref ( ) ;
284
- // TODO here Is it possible to 실제로 list item이 없으면..
285
- let inner_item = item. item . as_ref ( ) ?;
288
+ let inner_item = item. item . as_ref ( ) . or_else ( |err| Err ( err. clone ( ) ) ) ?;
286
289
let first = i == 0 ;
287
290
let last = iter. peek ( ) . is_none ( ) ;
288
291
let mut separate = match sep_place {
@@ -363,8 +366,8 @@ where
363
366
// Block style in non-vertical mode.
364
367
let block_mode = tactic == DefinitiveListTactic :: Horizontal ;
365
368
// Width restriction is only relevant in vertical mode.
366
- let comment =
367
- rewrite_comment ( comment , block_mode , formatting . shape , formatting . config ) ?;
369
+ let comment = rewrite_comment ( comment , block_mode , formatting . shape , formatting . config )
370
+ . unknown_error ( ) ?;
368
371
result. push_str ( & comment) ;
369
372
370
373
if !inner_item. is_empty ( ) {
@@ -410,7 +413,8 @@ where
410
413
true ,
411
414
Shape :: legacy ( formatting. shape . width , Indent :: empty ( ) ) ,
412
415
formatting. config ,
413
- ) ?;
416
+ )
417
+ . unknown_error ( ) ?;
414
418
415
419
result. push ( ' ' ) ;
416
420
result. push_str ( & formatted_comment) ;
@@ -461,7 +465,8 @@ where
461
465
)
462
466
} ;
463
467
464
- let mut formatted_comment = rewrite_post_comment ( & mut item_max_width) ?;
468
+ let mut formatted_comment =
469
+ rewrite_post_comment ( & mut item_max_width) . unknown_error ( ) ?;
465
470
466
471
if !starts_with_newline ( comment) {
467
472
if formatting. align_comments {
@@ -474,7 +479,8 @@ where
474
479
> formatting. config . max_width ( )
475
480
{
476
481
item_max_width = None ;
477
- formatted_comment = rewrite_post_comment ( & mut item_max_width) ?;
482
+ formatted_comment =
483
+ rewrite_post_comment ( & mut item_max_width) . unknown_error ( ) ?;
478
484
comment_alignment =
479
485
post_comment_alignment ( item_max_width, unicode_str_width ( inner_item) ) ;
480
486
}
@@ -517,7 +523,7 @@ where
517
523
prev_item_is_nested_import = inner_item. contains ( "::" ) ;
518
524
}
519
525
520
- Some ( result)
526
+ Ok ( result)
521
527
}
522
528
523
529
fn max_width_of_item_with_post_comment < I , T > (
@@ -776,10 +782,11 @@ where
776
782
ListItem {
777
783
pre_comment,
778
784
pre_comment_style,
785
+ // leave_last is set to true only for rewrite_items
779
786
item : if self . inner . peek ( ) . is_none ( ) && self . leave_last {
780
- None
787
+ Err ( RewriteError :: SkipFormatting )
781
788
} else {
782
- ( self . get_item_string ) ( & item) . ok ( )
789
+ ( self . get_item_string ) ( & item)
783
790
} ,
784
791
post_comment,
785
792
new_lines,
0 commit comments