Skip to content

Commit

Permalink
formatter: make error type of with_label() callback generic
Browse files Browse the repository at this point in the history
This will help eliminate push/pop_label() calls from show_diff_*().
  • Loading branch information
yuja committed Aug 1, 2024
1 parent f57a7e5 commit 052f022
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1971,7 +1971,7 @@ pub fn print_conflicted_paths(
}
};
}
Ok(())
io::Result::Ok(())
})?;
writeln!(formatter)?;
}
Expand Down
3 changes: 1 addition & 2 deletions cli/src/command_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,6 @@ fn print_error_hints(ui: &Ui, hints: &[ErrorHint]) -> io::Result<()> {
match hint {
ErrorHint::PlainText(message) => {
writeln!(formatter, "{message}")?;
Ok(())
}
ErrorHint::Formatted(recorded) => {
recorded.replay(formatter)?;
Expand All @@ -747,9 +746,9 @@ fn print_error_hints(ui: &Ui, hints: &[ErrorHint]) -> io::Result<()> {
if !recorded.data().ends_with(b"\n") {
writeln!(formatter)?;
}
Ok(())
}
}
io::Result::Ok(())
})?;
}
Ok(())
Expand Down
3 changes: 1 addition & 2 deletions cli/src/commands/operation/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ pub fn cmd_op_diff(
&to_op.metadata().description
}
)?;
writeln!(formatter)?;
Ok(())
writeln!(formatter)
})?;

show_op_diff(
Expand Down
8 changes: 4 additions & 4 deletions cli/src/diff_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ fn show_unified_diff_hunks(
.with_label("token", |formatter| formatter.write_all(content))?,
}
}
Ok(())
io::Result::Ok(())
})?;
let (_, content) = tokens.last().expect("hunk line must not be empty");
if !content.ends_with(b"\n") {
Expand Down Expand Up @@ -1036,7 +1036,7 @@ pub fn show_git_diff(
}
(None, None) => panic!("either left or right part should be present"),
}
Ok(())
io::Result::Ok(())
})?;

if left_part.content.contents == right_part.content.contents {
Expand All @@ -1061,7 +1061,7 @@ pub fn show_git_diff(
formatter.with_label("file_header", |formatter| {
writeln!(formatter, "--- {left_path}")?;
writeln!(formatter, "+++ {right_path}")?;
Ok(())
io::Result::Ok(())
})?;
show_unified_diff_hunks(
formatter,
Expand Down Expand Up @@ -1210,7 +1210,7 @@ pub fn show_diff_stat(
total_removed,
if total_removed == 1 { "" } else { "s" },
)?;
Ok(())
io::Result::Ok(())
})?;
Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions cli/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ impl dyn Formatter + '_ {
}
}

pub fn with_label(
pub fn with_label<E: From<io::Error>>(
&mut self,
label: &str,
write_inner: impl FnOnce(&mut dyn Formatter) -> io::Result<()>,
) -> io::Result<()> {
write_inner: impl FnOnce(&mut dyn Formatter) -> Result<(), E>,
) -> Result<(), E> {
self.push_label(label)?;
// Call `pop_label()` whether or not `write_inner()` fails, but don't let
// its error replace the one from `write_inner()`.
write_inner(self).and(self.pop_label())
write_inner(self).and(self.pop_label().map_err(Into::into))
}
}

Expand Down

0 comments on commit 052f022

Please sign in to comment.