Skip to content

Commit 614c26e

Browse files
committed
Rename the macros for exiting the binary
"die" is inspired by the PHP function of the same name, and limits any potential for confusion with the bail! macro from the popular anyhow crate. I've used this new naming convention when implementing this pattern in subsequent Rust projects.
1 parent ec6260c commit 614c26e

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

src/bail.rs renamed to src/die.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//! Top-level error handling for the xt binary.
1+
//! Handle errors in the xt binary by logging and exiting.
22
33
/// Formats a message to standard error, then terminates the current process with exit code 1.
4-
macro_rules! xt_bail {
4+
macro_rules! die {
55
($fmt:literal $(, $($args:tt)* )?) => {{
66
use ::std::io::Write;
77
let _ = writeln!(
@@ -15,7 +15,7 @@ macro_rules! xt_bail {
1515

1616
/// Formats a message to standard error, including the provided file path, then terminates the
1717
/// current process with exit code 1.
18-
macro_rules! xt_bail_path {
18+
macro_rules! die_in {
1919
($path:expr, $fmt:literal $(, $($args:tt)* )?) => {{
2020
use ::std::io::Write;
2121
let _ = writeln!(

src/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use std::process;
4141
use xt::Format;
4242

4343
#[macro_use]
44-
mod bail;
44+
mod die;
4545

4646
fn main() {
4747
let Ok(args) = Cli::parse_args().map_err(|err| {
@@ -53,9 +53,9 @@ fn main() {
5353

5454
let stdout = io::stdout();
5555
if stdout.is_terminal() && format_is_unsafe_for_terminal(args.to) {
56-
xt_bail!(
56+
die!(
5757
"refusing to output {format} to a terminal",
58-
format = args.to,
58+
format = args.to
5959
);
6060
}
6161

@@ -70,13 +70,13 @@ fn main() {
7070
};
7171

7272
for path in input_paths {
73-
let Ok(input) = path.open().map_err(|err| xt_bail_path!(path, "{err}"));
73+
let Ok(input) = path.open().map_err(|err| die_in!(path, "{err}"));
7474

7575
if matches!(input, Input::Stdin) {
7676
// TODO: Is this check worth it? You can pass /dev/stdin more than once, though the
7777
// behavior might be weird.
7878
if stdin_used {
79-
xt_bail!("cannot read from standard input more than once");
79+
die!("cannot read from standard input more than once");
8080
}
8181
stdin_used = true;
8282
}
@@ -88,10 +88,10 @@ fn main() {
8888
Input::Mmap(map) => translator.translate_slice(&map, from),
8989
};
9090
if let Err(err) = result {
91-
xt_bail_path!(path, "{err}");
91+
die_in!(path, "{err}");
9292
}
9393
if let Err(err) = translator.flush() {
94-
xt_bail!("{err}");
94+
die!("{err}");
9595
}
9696
}
9797
}

0 commit comments

Comments
 (0)