Skip to content

Commit a7e5af4

Browse files
authored
Merge pull request #5540 from cswn/join-remove-crash-macro
join: remove crash! macro
2 parents 0a385ee + 65dc70b commit a7e5af4

File tree

1 file changed

+16
-31
lines changed

1 file changed

+16
-31
lines changed

src/uu/join/src/join.rs

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ use std::num::IntErrorKind;
1919
#[cfg(unix)]
2020
use std::os::unix::ffi::OsStrExt;
2121
use uucore::display::Quotable;
22-
use uucore::error::{set_exit_code, UError, UResult, USimpleError};
22+
use uucore::error::{set_exit_code, FromIo, UError, UResult, USimpleError};
2323
use uucore::line_ending::LineEnding;
24-
use uucore::{crash, crash_if_err, format_usage, help_about, help_usage};
24+
use uucore::{crash_if_err, format_usage, help_about, help_usage};
2525

2626
const ABOUT: &str = help_about!("join.md");
2727
const USAGE: &str = help_usage!("join.md");
@@ -334,37 +334,30 @@ impl<'a> State<'a> {
334334
key: usize,
335335
line_ending: LineEnding,
336336
print_unpaired: bool,
337-
) -> State<'a> {
338-
let f = if name == "-" {
337+
) -> UResult<State<'a>> {
338+
let file_buf = if name == "-" {
339339
Box::new(stdin.lock()) as Box<dyn BufRead>
340340
} else {
341-
match File::open(name) {
342-
Ok(file) => Box::new(BufReader::new(file)) as Box<dyn BufRead>,
343-
Err(err) => crash!(1, "{}: {}", name.maybe_quote(), err),
344-
}
341+
let file = File::open(name).map_err_context(|| format!("{}", name.maybe_quote()))?;
342+
Box::new(BufReader::new(file)) as Box<dyn BufRead>
345343
};
346344

347-
State {
345+
Ok(State {
348346
key,
349347
file_name: name,
350348
file_num,
351349
print_unpaired,
352-
lines: f.split(line_ending as u8),
350+
lines: file_buf.split(line_ending as u8),
353351
max_len: 1,
354352
seq: Vec::new(),
355353
line_num: 0,
356354
has_failed: false,
357355
has_unpaired: false,
358-
}
356+
})
359357
}
360358

361359
/// Skip the current unpaired line.
362-
fn skip_line(
363-
&mut self,
364-
writer: &mut impl Write,
365-
input: &Input,
366-
repr: &Repr,
367-
) -> Result<(), JoinError> {
360+
fn skip_line(&mut self, writer: &mut impl Write, input: &Input, repr: &Repr) -> UResult<()> {
368361
if self.print_unpaired {
369362
self.print_first_line(writer, repr)?;
370363
}
@@ -375,7 +368,7 @@ impl<'a> State<'a> {
375368

376369
/// Keep reading line sequence until the key does not change, return
377370
/// the first line whose key differs.
378-
fn extend(&mut self, input: &Input) -> Result<Option<Line>, JoinError> {
371+
fn extend(&mut self, input: &Input) -> UResult<Option<Line>> {
379372
while let Some(line) = self.next_line(input)? {
380373
let diff = input.compare(self.get_current_key(), line.get_field(self.key));
381374

@@ -484,12 +477,7 @@ impl<'a> State<'a> {
484477
0
485478
}
486479

487-
fn finalize(
488-
&mut self,
489-
writer: &mut impl Write,
490-
input: &Input,
491-
repr: &Repr,
492-
) -> Result<(), JoinError> {
480+
fn finalize(&mut self, writer: &mut impl Write, input: &Input, repr: &Repr) -> UResult<()> {
493481
if self.has_line() {
494482
if self.print_unpaired {
495483
self.print_first_line(writer, repr)?;
@@ -713,10 +701,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
713701
return Err(USimpleError::new(1, "both files cannot be standard input"));
714702
}
715703

716-
match exec(file1, file2, settings) {
717-
Ok(_) => Ok(()),
718-
Err(e) => Err(USimpleError::new(1, format!("{e}"))),
719-
}
704+
exec(file1, file2, settings)
720705
}
721706

722707
pub fn uu_app() -> Command {
@@ -837,7 +822,7 @@ FILENUM is 1 or 2, corresponding to FILE1 or FILE2",
837822
)
838823
}
839824

840-
fn exec(file1: &str, file2: &str, settings: Settings) -> Result<(), JoinError> {
825+
fn exec(file1: &str, file2: &str, settings: Settings) -> UResult<()> {
841826
let stdin = stdin();
842827

843828
let mut state1 = State::new(
@@ -847,7 +832,7 @@ fn exec(file1: &str, file2: &str, settings: Settings) -> Result<(), JoinError> {
847832
settings.key1,
848833
settings.line_ending,
849834
settings.print_unpaired1,
850-
);
835+
)?;
851836

852837
let mut state2 = State::new(
853838
FileNum::File2,
@@ -856,7 +841,7 @@ fn exec(file1: &str, file2: &str, settings: Settings) -> Result<(), JoinError> {
856841
settings.key2,
857842
settings.line_ending,
858843
settings.print_unpaired2,
859-
);
844+
)?;
860845

861846
let input = Input::new(
862847
settings.separator,

0 commit comments

Comments
 (0)