@@ -24,7 +24,7 @@ use crate::expr::{
24
24
use crate :: lists:: { definitive_tactic, itemize_list, write_list, ListFormatting , Separator } ;
25
25
use crate :: macros:: { rewrite_macro, MacroPosition } ;
26
26
use crate :: overflow;
27
- use crate :: rewrite:: { Rewrite , RewriteContext , RewriteError , RewriteResult } ;
27
+ use crate :: rewrite:: { Rewrite , RewriteContext , RewriteError , RewriteErrorExt , RewriteResult } ;
28
28
use crate :: shape:: { Indent , Shape } ;
29
29
use crate :: source_map:: { LineRangeUtils , SpanUtils } ;
30
30
use crate :: spanned:: Spanned ;
@@ -78,24 +78,18 @@ impl Rewrite for ast::Local {
78
78
shape,
79
79
false ,
80
80
)
81
- . ok_or_else ( || RewriteError :: Unknown ) ?
81
+ . unknown_error ( ) ?
82
82
} ;
83
83
let let_kw_offset = result. len ( ) - "let " . len ( ) ;
84
84
85
85
// 4 = "let ".len()
86
86
let pat_shape = shape
87
87
. offset_left ( 4 )
88
- . ok_or_else ( || RewriteError :: ExceedsMaxWidth {
89
- configured_width : shape. width ,
90
- span : self . span ( ) ,
91
- } ) ?;
88
+ . max_width_error ( shape. width , self . span ( ) ) ?;
92
89
// 1 = ;
93
90
let pat_shape = pat_shape
94
91
. sub_width ( 1 )
95
- . ok_or_else ( || RewriteError :: ExceedsMaxWidth {
96
- configured_width : shape. width ,
97
- span : self . span ( ) ,
98
- } ) ?;
92
+ . max_width_error ( shape. width , self . span ( ) ) ?;
99
93
let pat_str = self . pat . rewrite_result ( context, pat_shape) ?;
100
94
101
95
result. push_str ( & pat_str) ;
@@ -112,16 +106,10 @@ impl Rewrite for ast::Local {
112
106
shape
113
107
}
114
108
. offset_left ( last_line_width ( & result) + separator. len ( ) )
115
- . ok_or_else ( || RewriteError :: ExceedsMaxWidth {
116
- configured_width : shape. width ,
117
- span : self . span ( ) ,
118
- } ) ?
109
+ . max_width_error ( shape. width , self . span ( ) ) ?
119
110
// 2 = ` =`
120
111
. sub_width ( 2 )
121
- . ok_or_else ( || RewriteError :: ExceedsMaxWidth {
122
- configured_width : shape. width ,
123
- span : self . span ( ) ,
124
- } ) ?;
112
+ . max_width_error ( shape. width , self . span ( ) ) ?;
125
113
126
114
let rewrite = ty. rewrite_result ( context, ty_shape) ?;
127
115
@@ -140,7 +128,7 @@ impl Rewrite for ast::Local {
140
128
141
129
if let Some ( ( init, else_block) ) = self . kind . init_else_opt ( ) {
142
130
// 1 = trailing semicolon;
143
- let nested_shape = shape. sub_width ( 1 ) . ok_or_else ( || RewriteError :: Unknown ) ?;
131
+ let nested_shape = shape. sub_width ( 1 ) . unknown_error ( ) ?;
144
132
145
133
result = rewrite_assign_rhs (
146
134
context,
@@ -149,10 +137,7 @@ impl Rewrite for ast::Local {
149
137
& RhsAssignKind :: Expr ( & init. kind , init. span ) ,
150
138
nested_shape,
151
139
)
152
- . ok_or_else ( || RewriteError :: ExceedsMaxWidth {
153
- configured_width : shape. width ,
154
- span : self . span ( ) ,
155
- } ) ?;
140
+ . max_width_error ( shape. width , self . span ( ) ) ?;
156
141
157
142
if let Some ( block) = else_block {
158
143
let else_kw_span = init. span . between ( block. span ) ;
@@ -195,7 +180,7 @@ impl Rewrite for ast::Local {
195
180
196
181
let mut rw_else_block =
197
182
rewrite_let_else_block ( block, allow_single_line, context, shape)
198
- . ok_or_else ( || RewriteError :: Unknown ) ?;
183
+ . unknown_error ( ) ?;
199
184
200
185
let single_line_else = !rw_else_block. contains ( '\n' ) ;
201
186
// +1 for the trailing `;`
@@ -204,8 +189,8 @@ impl Rewrite for ast::Local {
204
189
if allow_single_line && single_line_else && else_block_exceeds_width {
205
190
// writing this on one line would exceed the available width
206
191
// so rewrite the else block over multiple lines.
207
- rw_else_block = rewrite_let_else_block ( block , false , context , shape )
208
- . ok_or_else ( || RewriteError :: Unknown ) ?;
192
+ rw_else_block =
193
+ rewrite_let_else_block ( block , false , context , shape ) . unknown_error ( ) ?;
209
194
}
210
195
211
196
result. push_str ( & rw_else_block) ;
@@ -1916,7 +1901,7 @@ pub(crate) fn rewrite_struct_field(
1916
1901
shape,
1917
1902
attrs_extendable,
1918
1903
)
1919
- . ok_or_else ( || RewriteError :: Unknown ) ?;
1904
+ . unknown_error ( ) ?;
1920
1905
let overhead = trimmed_last_line_width ( & attr_prefix) ;
1921
1906
let lhs_offset = lhs_max_width. saturating_sub ( overhead) ;
1922
1907
for _ in 0 ..lhs_offset {
@@ -1931,12 +1916,9 @@ pub(crate) fn rewrite_struct_field(
1931
1916
// let orig_ty = shape
1932
1917
// .offset_left(overhead + spacing.len())
1933
1918
// .and_then(|ty_shape| field.ty.rewrite(context, ty_shape));
1934
- let ty_shape = shape. offset_left ( overhead + spacing. len ( ) ) . ok_or_else ( || {
1935
- RewriteError :: ExceedsMaxWidth {
1936
- configured_width : shape. width ,
1937
- span : field. span ( ) ,
1938
- }
1939
- } ) ?;
1919
+ let ty_shape = shape
1920
+ . offset_left ( overhead + spacing. len ( ) )
1921
+ . max_width_error ( shape. width , field. ty . span ( ) ) ?;
1940
1922
let orig_ty = field. ty . rewrite_result ( context, ty_shape) ;
1941
1923
if let Ok ( ref ty) = orig_ty {
1942
1924
if !ty. contains ( '\n' ) && !contains_comment ( context. snippet ( missing_span) ) {
@@ -1947,15 +1929,15 @@ pub(crate) fn rewrite_struct_field(
1947
1929
let is_prefix_empty = prefix. is_empty ( ) ;
1948
1930
// We must use multiline. We are going to put attributes and a field on different lines.
1949
1931
let field_str = rewrite_assign_rhs ( context, prefix, & * field. ty , & RhsAssignKind :: Ty , shape)
1950
- . ok_or_else ( || RewriteError :: Unknown ) ?;
1932
+ . unknown_error ( ) ?;
1951
1933
// Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct.
1952
1934
let field_str = if is_prefix_empty {
1953
1935
field_str. trim_start ( )
1954
1936
} else {
1955
1937
& field_str
1956
1938
} ;
1957
1939
combine_strs_with_missing_comments ( context, & attrs_str, field_str, missing_span, shape, false )
1958
- . ok_or_else ( || RewriteError :: Unknown )
1940
+ . unknown_error ( )
1959
1941
}
1960
1942
1961
1943
pub ( crate ) struct StaticParts < ' a > {
@@ -2126,12 +2108,10 @@ impl Rewrite for ast::FnRetTy {
2126
2108
if context. config . version ( ) == Version :: One
2127
2109
|| context. config . indent_style ( ) == IndentStyle :: Visual
2128
2110
{
2129
- let inner_width = shape. width . checked_sub ( 3 ) . ok_or_else ( || {
2130
- RewriteError :: ExceedsMaxWidth {
2131
- configured_width : shape. width ,
2132
- span : self . span ( ) ,
2133
- }
2134
- } ) ?;
2111
+ let inner_width = shape
2112
+ . width
2113
+ . checked_sub ( 3 )
2114
+ . max_width_error ( shape. width , self . span ( ) ) ?;
2135
2115
return ty
2136
2116
. rewrite_result ( context, Shape :: legacy ( inner_width, shape. indent + 3 ) )
2137
2117
. map ( |r| format ! ( "-> {}" , r) ) ;
@@ -2140,10 +2120,7 @@ impl Rewrite for ast::FnRetTy {
2140
2120
// 3 = "-> "
2141
2121
let shape = shape
2142
2122
. offset_left ( 3 )
2143
- . ok_or_else ( || RewriteError :: ExceedsMaxWidth {
2144
- configured_width : shape. width ,
2145
- span : self . span ( ) ,
2146
- } ) ?;
2123
+ . max_width_error ( shape. width , self . span ( ) ) ?;
2147
2124
2148
2125
ty. rewrite_result ( context, shape)
2149
2126
. map ( |s| format ! ( "-> {}" , s) )
@@ -2238,7 +2215,7 @@ impl Rewrite for ast::Param {
2238
2215
shape,
2239
2216
!has_multiple_attr_lines && !has_doc_comments,
2240
2217
)
2241
- . ok_or_else ( || RewriteError :: Unknown ) ?;
2218
+ . unknown_error ( ) ?;
2242
2219
2243
2220
if !is_empty_infer ( & * self . ty , self . pat . span ) {
2244
2221
let ( before_comment, after_comment) =
@@ -2247,12 +2224,10 @@ impl Rewrite for ast::Param {
2247
2224
result. push_str ( colon_spaces ( context. config ) ) ;
2248
2225
result. push_str ( & after_comment) ;
2249
2226
let overhead = last_line_width ( & result) ;
2250
- let max_width = shape. width . checked_sub ( overhead) . ok_or_else ( || {
2251
- RewriteError :: ExceedsMaxWidth {
2252
- configured_width : shape. width ,
2253
- span : self . span ( ) ,
2254
- }
2255
- } ) ?;
2227
+ let max_width = shape
2228
+ . width
2229
+ . checked_sub ( overhead)
2230
+ . max_width_error ( shape. width , self . span ( ) ) ?;
2256
2231
if let Ok ( ty_str) = self
2257
2232
. ty
2258
2233
. rewrite_result ( context, Shape :: legacy ( max_width, shape. indent ) )
@@ -2273,17 +2248,15 @@ impl Rewrite for ast::Param {
2273
2248
shape,
2274
2249
!has_multiple_attr_lines,
2275
2250
)
2276
- . ok_or_else ( || RewriteError :: Unknown ) ?;
2251
+ . unknown_error ( ) ?;
2277
2252
result. push_str ( & before_comment) ;
2278
2253
result. push_str ( colon_spaces ( context. config ) ) ;
2279
2254
result. push_str ( & after_comment) ;
2280
2255
let overhead = last_line_width ( & result) ;
2281
- let max_width = shape. width . checked_sub ( overhead) . ok_or_else ( || {
2282
- RewriteError :: ExceedsMaxWidth {
2283
- configured_width : shape. width ,
2284
- span : self . span ( ) ,
2285
- }
2286
- } ) ?;
2256
+ let max_width = shape
2257
+ . width
2258
+ . checked_sub ( overhead)
2259
+ . max_width_error ( shape. width , self . span ( ) ) ?;
2287
2260
let ty_str = self
2288
2261
. ty
2289
2262
. rewrite_result ( context, Shape :: legacy ( max_width, shape. indent ) ) ?;
@@ -2323,7 +2296,7 @@ fn rewrite_explicit_self(
2323
2296
shape,
2324
2297
!has_multiple_attr_lines,
2325
2298
)
2326
- . ok_or_else ( || RewriteError :: Unknown ) ?)
2299
+ . unknown_error ( ) ?)
2327
2300
}
2328
2301
None => Ok ( combine_strs_with_missing_comments (
2329
2302
context,
@@ -2333,7 +2306,7 @@ fn rewrite_explicit_self(
2333
2306
shape,
2334
2307
!has_multiple_attr_lines,
2335
2308
)
2336
- . ok_or_else ( || RewriteError :: Unknown ) ?) ,
2309
+ . unknown_error ( ) ?) ,
2337
2310
}
2338
2311
}
2339
2312
ast:: SelfKind :: Explicit ( ref ty, mutability) => {
@@ -2350,7 +2323,7 @@ fn rewrite_explicit_self(
2350
2323
shape,
2351
2324
!has_multiple_attr_lines,
2352
2325
)
2353
- . ok_or_else ( || RewriteError :: Unknown ) ?)
2326
+ . unknown_error ( ) ?)
2354
2327
}
2355
2328
ast:: SelfKind :: Value ( mutability) => Ok ( combine_strs_with_missing_comments (
2356
2329
context,
@@ -2360,7 +2333,7 @@ fn rewrite_explicit_self(
2360
2333
shape,
2361
2334
!has_multiple_attr_lines,
2362
2335
)
2363
- . ok_or_else ( || RewriteError :: Unknown ) ?) ,
2336
+ . unknown_error ( ) ?) ,
2364
2337
}
2365
2338
}
2366
2339
0 commit comments