Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 16 additions & 31 deletions src/uu/join/src/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use std::num::IntErrorKind;
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
use uucore::display::Quotable;
use uucore::error::{set_exit_code, UError, UResult, USimpleError};
use uucore::error::{set_exit_code, FromIo, UError, UResult, USimpleError};
use uucore::line_ending::LineEnding;
use uucore::{crash, crash_if_err, format_usage, help_about, help_usage};
use uucore::{crash_if_err, format_usage, help_about, help_usage};

const ABOUT: &str = help_about!("join.md");
const USAGE: &str = help_usage!("join.md");
Expand Down Expand Up @@ -334,37 +334,30 @@ impl<'a> State<'a> {
key: usize,
line_ending: LineEnding,
print_unpaired: bool,
) -> State<'a> {
let f = if name == "-" {
) -> UResult<State<'a>> {
let file_buf = if name == "-" {
Box::new(stdin.lock()) as Box<dyn BufRead>
} else {
match File::open(name) {
Ok(file) => Box::new(BufReader::new(file)) as Box<dyn BufRead>,
Err(err) => crash!(1, "{}: {}", name.maybe_quote(), err),
}
let file = File::open(name).map_err_context(|| format!("{}", name.maybe_quote()))?;
Box::new(BufReader::new(file)) as Box<dyn BufRead>
};

State {
Ok(State {
key,
file_name: name,
file_num,
print_unpaired,
lines: f.split(line_ending as u8),
lines: file_buf.split(line_ending as u8),
max_len: 1,
seq: Vec::new(),
line_num: 0,
has_failed: false,
has_unpaired: false,
}
})
}

/// Skip the current unpaired line.
fn skip_line(
&mut self,
writer: &mut impl Write,
input: &Input,
repr: &Repr,
) -> Result<(), JoinError> {
fn skip_line(&mut self, writer: &mut impl Write, input: &Input, repr: &Repr) -> UResult<()> {
if self.print_unpaired {
self.print_first_line(writer, repr)?;
}
Expand All @@ -375,7 +368,7 @@ impl<'a> State<'a> {

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

Expand Down Expand Up @@ -484,12 +477,7 @@ impl<'a> State<'a> {
0
}

fn finalize(
&mut self,
writer: &mut impl Write,
input: &Input,
repr: &Repr,
) -> Result<(), JoinError> {
fn finalize(&mut self, writer: &mut impl Write, input: &Input, repr: &Repr) -> UResult<()> {
if self.has_line() {
if self.print_unpaired {
self.print_first_line(writer, repr)?;
Expand Down Expand Up @@ -713,10 +701,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
return Err(USimpleError::new(1, "both files cannot be standard input"));
}

match exec(file1, file2, settings) {
Ok(_) => Ok(()),
Err(e) => Err(USimpleError::new(1, format!("{e}"))),
}
exec(file1, file2, settings)
}

pub fn uu_app() -> Command {
Expand Down Expand Up @@ -837,7 +822,7 @@ FILENUM is 1 or 2, corresponding to FILE1 or FILE2",
)
}

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

let mut state1 = State::new(
Expand All @@ -847,7 +832,7 @@ fn exec(file1: &str, file2: &str, settings: Settings) -> Result<(), JoinError> {
settings.key1,
settings.line_ending,
settings.print_unpaired1,
);
)?;

let mut state2 = State::new(
FileNum::File2,
Expand All @@ -856,7 +841,7 @@ fn exec(file1: &str, file2: &str, settings: Settings) -> Result<(), JoinError> {
settings.key2,
settings.line_ending,
settings.print_unpaired2,
);
)?;

let input = Input::new(
settings.separator,
Expand Down