Skip to content

Commit 28a4885

Browse files
committed
modify rewrite_explicit_self, rewrite_struct_field to return RewriteResult
1 parent a16bcca commit 28a4885

File tree

3 files changed

+53
-36
lines changed

3 files changed

+53
-36
lines changed

src/items.rs

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,10 +1859,10 @@ fn type_annotation_spacing(config: &Config) -> (&str, &str) {
18591859
pub(crate) fn rewrite_struct_field_prefix(
18601860
context: &RewriteContext<'_>,
18611861
field: &ast::FieldDef,
1862-
) -> Option<String> {
1862+
) -> RewriteResult {
18631863
let vis = format_visibility(context, &field.vis);
18641864
let type_annotation_spacing = type_annotation_spacing(context.config);
1865-
Some(match field.ident {
1865+
Ok(match field.ident {
18661866
Some(name) => format!(
18671867
"{}{}{}:",
18681868
vis,
@@ -1875,6 +1875,9 @@ pub(crate) fn rewrite_struct_field_prefix(
18751875

18761876
impl Rewrite for ast::FieldDef {
18771877
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1878+
self.rewrite_result(context, shape).ok()
1879+
}
1880+
fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
18781881
rewrite_struct_field(context, self, shape, 0)
18791882
}
18801883
}
@@ -1884,15 +1887,15 @@ pub(crate) fn rewrite_struct_field(
18841887
field: &ast::FieldDef,
18851888
shape: Shape,
18861889
lhs_max_width: usize,
1887-
) -> Option<String> {
1890+
) -> RewriteResult {
18881891
if contains_skip(&field.attrs) {
1889-
return Some(context.snippet(field.span()).to_owned());
1892+
return Ok(context.snippet(field.span()).to_owned());
18901893
}
18911894

18921895
let type_annotation_spacing = type_annotation_spacing(context.config);
18931896
let prefix = rewrite_struct_field_prefix(context, field)?;
18941897

1895-
let attrs_str = field.attrs.rewrite(context, shape)?;
1898+
let attrs_str = field.attrs.rewrite_result(context, shape)?;
18961899
let attrs_extendable = field.ident.is_none() && is_attributes_extendable(&attrs_str);
18971900
let missing_span = if field.attrs.is_empty() {
18981901
mk_sp(field.span.lo(), field.span.lo())
@@ -1912,7 +1915,8 @@ pub(crate) fn rewrite_struct_field(
19121915
missing_span,
19131916
shape,
19141917
attrs_extendable,
1915-
)?;
1918+
)
1919+
.ok_or_else(|| RewriteError::Unknown)?;
19161920
let overhead = trimmed_last_line_width(&attr_prefix);
19171921
let lhs_offset = lhs_max_width.saturating_sub(overhead);
19181922
for _ in 0..lhs_offset {
@@ -1922,25 +1926,36 @@ pub(crate) fn rewrite_struct_field(
19221926
if prefix.is_empty() && !attrs_str.is_empty() && attrs_extendable && spacing.is_empty() {
19231927
spacing.push(' ');
19241928
}
1925-
let orig_ty = shape
1926-
.offset_left(overhead + spacing.len())
1927-
.and_then(|ty_shape| field.ty.rewrite(context, ty_shape));
1928-
if let Some(ref ty) = orig_ty {
1929+
1930+
// Question. Why don't we immediately return None/Err with ?
1931+
// let orig_ty = shape
1932+
// .offset_left(overhead + spacing.len())
1933+
// .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+
})?;
1940+
let orig_ty = field.ty.rewrite_result(context, ty_shape);
1941+
if let Ok(ref ty) = orig_ty {
19291942
if !ty.contains('\n') && !contains_comment(context.snippet(missing_span)) {
1930-
return Some(attr_prefix + &spacing + ty);
1943+
return Ok(attr_prefix + &spacing + ty);
19311944
}
19321945
}
19331946

19341947
let is_prefix_empty = prefix.is_empty();
19351948
// We must use multiline. We are going to put attributes and a field on different lines.
1936-
let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, &RhsAssignKind::Ty, shape)?;
1949+
let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, &RhsAssignKind::Ty, shape)
1950+
.ok_or_else(|| RewriteError::Unknown)?;
19371951
// Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct.
19381952
let field_str = if is_prefix_empty {
19391953
field_str.trim_start()
19401954
} else {
19411955
&field_str
19421956
};
19431957
combine_strs_with_missing_comments(context, &attrs_str, field_str, missing_span, shape, false)
1958+
.ok_or_else(|| RewriteError::Unknown)
19441959
}
19451960

19461961
pub(crate) struct StaticParts<'a> {
@@ -2211,7 +2226,6 @@ impl Rewrite for ast::Param {
22112226
shape,
22122227
has_multiple_attr_lines,
22132228
)
2214-
.ok_or_else(|| RewriteError::Unknown)
22152229
} else if is_named_param(self) {
22162230
let param_name = &self
22172231
.pat
@@ -2291,58 +2305,62 @@ fn rewrite_explicit_self(
22912305
span: Span,
22922306
shape: Shape,
22932307
has_multiple_attr_lines: bool,
2294-
) -> Option<String> {
2308+
) -> RewriteResult {
22952309
match explicit_self.node {
22962310
ast::SelfKind::Region(lt, m) => {
22972311
let mut_str = format_mutability(m);
22982312
match lt {
22992313
Some(ref l) => {
2300-
let lifetime_str = l.rewrite(
2314+
let lifetime_str = l.rewrite_result(
23012315
context,
23022316
Shape::legacy(context.config.max_width(), Indent::empty()),
23032317
)?;
2304-
Some(combine_strs_with_missing_comments(
2318+
Ok(combine_strs_with_missing_comments(
23052319
context,
23062320
param_attrs,
23072321
&format!("&{lifetime_str} {mut_str}self"),
23082322
span,
23092323
shape,
23102324
!has_multiple_attr_lines,
2311-
)?)
2325+
)
2326+
.ok_or_else(|| RewriteError::Unknown)?)
23122327
}
2313-
None => Some(combine_strs_with_missing_comments(
2328+
None => Ok(combine_strs_with_missing_comments(
23142329
context,
23152330
param_attrs,
23162331
&format!("&{mut_str}self"),
23172332
span,
23182333
shape,
23192334
!has_multiple_attr_lines,
2320-
)?),
2335+
)
2336+
.ok_or_else(|| RewriteError::Unknown)?),
23212337
}
23222338
}
23232339
ast::SelfKind::Explicit(ref ty, mutability) => {
2324-
let type_str = ty.rewrite(
2340+
let type_str = ty.rewrite_result(
23252341
context,
23262342
Shape::legacy(context.config.max_width(), Indent::empty()),
23272343
)?;
23282344

2329-
Some(combine_strs_with_missing_comments(
2345+
Ok(combine_strs_with_missing_comments(
23302346
context,
23312347
param_attrs,
23322348
&format!("{}self: {}", format_mutability(mutability), type_str),
23332349
span,
23342350
shape,
23352351
!has_multiple_attr_lines,
2336-
)?)
2352+
)
2353+
.ok_or_else(|| RewriteError::Unknown)?)
23372354
}
2338-
ast::SelfKind::Value(mutability) => Some(combine_strs_with_missing_comments(
2355+
ast::SelfKind::Value(mutability) => Ok(combine_strs_with_missing_comments(
23392356
context,
23402357
param_attrs,
23412358
&format!("{}self", format_mutability(mutability)),
23422359
span,
23432360
shape,
23442361
!has_multiple_attr_lines,
2345-
)?),
2362+
)
2363+
.ok_or_else(|| RewriteError::Unknown)?),
23462364
}
23472365
}
23482366

src/rewrite.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::skip::SkipContext;
1414
use crate::visitor::SnippetProvider;
1515
use crate::FormatReport;
1616

17-
pub type RewriteResult = Result<String, RewriteError>;
17+
pub(crate) type RewriteResult = Result<String, RewriteError>;
1818
pub(crate) trait Rewrite {
1919
/// Rewrite self into shape.
2020
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>;

src/vertical.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,15 @@ impl AlignedItem for ast::FieldDef {
5050
mk_sp(self.attrs.last().unwrap().span.hi(), self.span.lo())
5151
};
5252
let attrs_extendable = self.ident.is_none() && is_attributes_extendable(&attrs_str);
53-
rewrite_struct_field_prefix(context, self).and_then(|field_str| {
54-
combine_strs_with_missing_comments(
55-
context,
56-
&attrs_str,
57-
&field_str,
58-
missing_span,
59-
shape,
60-
attrs_extendable,
61-
)
62-
})
53+
let field_str = rewrite_struct_field_prefix(context, self).ok()?;
54+
combine_strs_with_missing_comments(
55+
context,
56+
&attrs_str,
57+
&field_str,
58+
missing_span,
59+
shape,
60+
attrs_extendable,
61+
)
6362
}
6463

6564
fn rewrite_aligned_item(
@@ -68,7 +67,7 @@ impl AlignedItem for ast::FieldDef {
6867
shape: Shape,
6968
prefix_max_width: usize,
7069
) -> Option<String> {
71-
rewrite_struct_field(context, self, shape, prefix_max_width)
70+
rewrite_struct_field(context, self, shape, prefix_max_width).ok()
7271
}
7372
}
7473

0 commit comments

Comments
 (0)