Skip to content

Commit

Permalink
uucore: Remove crash! macro
Browse files Browse the repository at this point in the history
  • Loading branch information
saulvaldelvira committed Jan 7, 2025
1 parent f94fd11 commit aa37206
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 96 deletions.
23 changes: 10 additions & 13 deletions src/uu/csplit/src/csplit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use clap::{crate_version, Arg, ArgAction, ArgMatches, Command};
use regex::Regex;
use uucore::display::Quotable;
use uucore::error::{FromIo, UResult};
use uucore::{crash_if_err, format_usage, help_about, help_section, help_usage};
use uucore::{format_usage, help_about, help_section, help_usage};

mod csplit_error;
mod patterns;
Expand Down Expand Up @@ -51,26 +51,23 @@ pub struct CsplitOptions {
}

impl CsplitOptions {
fn new(matches: &ArgMatches) -> Self {
fn new(matches: &ArgMatches) -> Result<Self, CsplitError> {
let keep_files = matches.get_flag(options::KEEP_FILES);
let quiet = matches.get_flag(options::QUIET);
let elide_empty_files = matches.get_flag(options::ELIDE_EMPTY_FILES);
let suppress_matched = matches.get_flag(options::SUPPRESS_MATCHED);

Self {
split_name: crash_if_err!(
1,
SplitName::new(
matches.get_one::<String>(options::PREFIX).cloned(),
matches.get_one::<String>(options::SUFFIX_FORMAT).cloned(),
matches.get_one::<String>(options::DIGITS).cloned()
)
),
Ok(Self {
split_name: SplitName::new(
matches.get_one::<String>(options::PREFIX).cloned(),
matches.get_one::<String>(options::SUFFIX_FORMAT).cloned(),
matches.get_one::<String>(options::DIGITS).cloned(),
)?,
keep_files,
quiet,
elide_empty_files,
suppress_matched,
}
})
}
}

Expand Down Expand Up @@ -561,7 +558,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
.unwrap()
.map(|s| s.to_string())
.collect();
let options = CsplitOptions::new(&matches);
let options = CsplitOptions::new(&matches)?;
if file_name == "-" {
let stdin = io::stdin();
Ok(csplit(&options, &patterns, stdin.lock())?)
Expand Down
27 changes: 16 additions & 11 deletions src/uu/join/src/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::os::unix::ffi::OsStrExt;
use uucore::display::Quotable;
use uucore::error::{set_exit_code, FromIo, UError, UResult, USimpleError};
use uucore::line_ending::LineEnding;
use uucore::{crash_if_err, format_usage, help_about, help_usage};
use uucore::{format_usage, help_about, help_usage};

const ABOUT: &str = help_about!("join.md");
const USAGE: &str = help_usage!("join.md");
Expand Down Expand Up @@ -587,15 +587,19 @@ impl<'a> State<'a> {
!self.seq.is_empty()
}

fn initialize<Sep: Separator>(&mut self, read_sep: &Sep, autoformat: bool) -> usize {
if let Some(line) = crash_if_err!(1, self.read_line(read_sep)) {
fn initialize<Sep: Separator>(
&mut self,
read_sep: &Sep,
autoformat: bool,
) -> std::io::Result<usize> {
if let Some(line) = self.read_line(read_sep)? {
self.seq.push(line);

if autoformat {
return self.seq[0].field_ranges.len();
return Ok(self.seq[0].field_ranges.len());
}
}
0
Ok(0)
}

fn finalize<Sep: Separator>(
Expand Down Expand Up @@ -1008,20 +1012,21 @@ fn exec<Sep: Separator>(file1: &str, file2: &str, settings: Settings, sep: Sep)

let format = if settings.autoformat {
let mut format = vec![Spec::Key];
let mut initialize = |state: &mut State| {
let max_fields = state.initialize(&sep, settings.autoformat);
let mut initialize = |state: &mut State| -> UResult<()> {
let max_fields = state.initialize(&sep, settings.autoformat)?;
for i in 0..max_fields {
if i != state.key {
format.push(Spec::Field(state.file_num, i));
}
}
Ok(())
};
initialize(&mut state1);
initialize(&mut state2);
initialize(&mut state1)?;
initialize(&mut state2)?;
format
} else {
state1.initialize(&sep, settings.autoformat);
state2.initialize(&sep, settings.autoformat);
state1.initialize(&sep, settings.autoformat)?;
state2.initialize(&sep, settings.autoformat)?;
settings.format
};

Expand Down
17 changes: 9 additions & 8 deletions src/uu/unexpand/src/unexpand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::str::from_utf8;
use unicode_width::UnicodeWidthChar;
use uucore::display::Quotable;
use uucore::error::{FromIo, UError, UResult, USimpleError};
use uucore::{crash_if_err, format_usage, help_about, help_usage, show};
use uucore::{format_usage, help_about, help_usage, show};

const USAGE: &str = help_usage!("unexpand.md");
const ABOUT: &str = help_about!("unexpand.md");
Expand Down Expand Up @@ -244,7 +244,7 @@ fn write_tabs(
prevtab: bool,
init: bool,
amode: bool,
) {
) -> UResult<()> {
// This conditional establishes the following:
// We never turn a single space before a non-blank into
// a tab, unless it's at the start of the line.
Expand All @@ -255,15 +255,16 @@ fn write_tabs(
break;
}

crash_if_err!(1, output.write_all(b"\t"));
output.write_all(b"\t")?;
scol += nts;
}
}

while col > scol {
crash_if_err!(1, output.write_all(b" "));
output.write_all(b" ")?;
scol += 1;
}
Ok(())
}

#[derive(PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -325,7 +326,7 @@ fn unexpand_line(
options: &Options,
lastcol: usize,
ts: &[usize],
) -> std::io::Result<()> {
) -> UResult<()> {
let mut byte = 0; // offset into the buffer
let mut col = 0; // the current column
let mut scol = 0; // the start col for the current span, i.e., the already-printed width
Expand All @@ -335,7 +336,7 @@ fn unexpand_line(
while byte < buf.len() {
// when we have a finite number of columns, never convert past the last column
if lastcol > 0 && col >= lastcol {
write_tabs(output, ts, scol, col, pctype == CharType::Tab, init, true);
write_tabs(output, ts, scol, col, pctype == CharType::Tab, init, true)?;
output.write_all(&buf[byte..])?;
scol = col;
break;
Expand Down Expand Up @@ -370,7 +371,7 @@ fn unexpand_line(
pctype == CharType::Tab,
init,
options.aflag,
);
)?;
init = false; // no longer at the start of a line
col = if ctype == CharType::Other {
// use computed width
Expand All @@ -391,7 +392,7 @@ fn unexpand_line(
}

// write out anything remaining
write_tabs(output, ts, scol, col, pctype == CharType::Tab, init, true);
write_tabs(output, ts, scol, col, pctype == CharType::Tab, init, true)?;
output.flush()?;
buf.truncate(0); // clear out the buffer

Expand Down
8 changes: 0 additions & 8 deletions src/uucore/src/lib/features/fsext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,14 +597,6 @@ impl FsUsage {
&mut total_number_of_clusters,
)
};
if 0 == success {
// Fails in case of CD for example
// crash!(
// EXIT_ERR,
// "GetDiskFreeSpaceW failed: {}",
// IOError::last_os_error()
// );
}

let bytes_per_cluster = sectors_per_cluster as u64 * bytes_per_sector as u64;
Ok(Self {
Expand Down
5 changes: 4 additions & 1 deletion src/uucore/src/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,10 @@ macro_rules! prompt_yes(
eprint!("{}: ", uucore::util_name());
eprint!($($args)+);
eprint!(" ");
uucore::crash_if_err!(1, std::io::stderr().flush());
let res = std::io::stderr().flush().map_err(|err| {
$crate::error::USimpleError::new(1, err.to_string())
});
uucore::show_if_err!(res);
uucore::read_yes()
})
);
Expand Down
55 changes: 0 additions & 55 deletions src/uucore/src/lib/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
//! - From custom messages: [`crate::show_error!`]
//! - Print warnings: [`crate::show_warning!`]
//! - Terminate util execution
//! - Crash program: [`crate::crash!`], [`crate::crash_if_err!`]
// spell-checker:ignore sourcepath targetpath rustdoc

Expand Down Expand Up @@ -189,57 +188,3 @@ macro_rules! show_warning_caps(
eprintln!($($args)+);
})
);

/// Display an error and [`std::process::exit`]
///
/// Displays the provided error message using [`show_error!`], then invokes
/// [`std::process::exit`] with the provided exit code.
///
/// # Examples
///
/// ```should_panic
/// # #[macro_use]
/// # extern crate uucore;
/// # fn main() {
/// // outputs <name>: Couldn't apply foo to bar
/// // and terminates execution
/// crash!(1, "Couldn't apply {} to {}", "foo", "bar");
/// # }
/// ```
#[macro_export]
macro_rules! crash(
($exit_code:expr, $($args:tt)+) => ({
$crate::show_error!($($args)+);
std::process::exit($exit_code);
})
);

/// Unwrap a [`std::result::Result`], crashing instead of panicking.
///
/// If the result is an `Ok`-variant, returns the value contained inside. If it
/// is an `Err`-variant, invokes [`crash!`] with the formatted error instead.
///
/// # Examples
///
/// ```should_panic
/// # #[macro_use]
/// # extern crate uucore;
/// # fn main() {
/// let is_ok: Result<u32, &str> = Ok(1);
/// // Does nothing
/// crash_if_err!(1, is_ok);
///
/// let is_err: Result<u32, &str> = Err("This didn't work...");
/// // Calls `crash!`
/// crash_if_err!(1, is_err);
/// # }
/// ```
#[macro_export]
macro_rules! crash_if_err(
($exit_code:expr, $exp:expr) => (
match $exp {
Ok(m) => m,
Err(f) => $crate::crash!($exit_code, "{}", f),
}
)
);

0 comments on commit aa37206

Please sign in to comment.