Skip to content

Commit

Permalink
refactor(snap): Reduce nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Apr 19, 2024
1 parent c3a43ce commit 0858005
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions crates/snapbox/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,39 +170,40 @@ impl Assert {
actual_name: Option<&dyn std::fmt::Display>,
) {
let result = self.try_verify(&expected, &actual, actual_name);
if let Err(err) = result {
match self.action {
Action::Skip => unreachable!("Bailed out earlier"),
Action::Ignore => {
use std::io::Write;

let _ = writeln!(
stderr(),
"{}: {}",
self.palette.warn("Ignoring failure"),
err
);
}
Action::Verify => {
let message = if expected.source().is_none() {
crate::report::Styled::new(String::new(), Default::default())
} else if let Some(action_var) = self.action_var.as_deref() {
self.palette
.hint(format!("Update with {}=overwrite", action_var))
} else {
crate::report::Styled::new(String::new(), Default::default())
};
panic!("{err}{message}");
}
Action::Overwrite => {
use std::io::Write;

if let Some(source) = expected.source() {
let _ = writeln!(stderr(), "{}: {}", self.palette.warn("Fixing"), err);
actual.write_to(source).unwrap();
} else {
panic!("{err}");
}
let Err(err) = result else {
return;
};
match self.action {
Action::Skip => unreachable!("Bailed out earlier"),
Action::Ignore => {
use std::io::Write;

let _ = writeln!(
stderr(),
"{}: {}",
self.palette.warn("Ignoring failure"),
err
);
}
Action::Verify => {
let message = if expected.source().is_none() {
crate::report::Styled::new(String::new(), Default::default())
} else if let Some(action_var) = self.action_var.as_deref() {
self.palette
.hint(format!("Update with {}=overwrite", action_var))
} else {
crate::report::Styled::new(String::new(), Default::default())
};
panic!("{err}{message}");
}
Action::Overwrite => {
use std::io::Write;

if let Some(source) = expected.source() {
let _ = writeln!(stderr(), "{}: {}", self.palette.warn("Fixing"), err);
actual.write_to(source).unwrap();
} else {
panic!("{err}");
}
}
}
Expand Down

0 comments on commit 0858005

Please sign in to comment.