mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #5540 from cswn/join-remove-crash-macro
join: remove crash! macro
This commit is contained in:
commit
a7e5af4770
1 changed files with 16 additions and 31 deletions
|
@ -19,9 +19,9 @@ use std::num::IntErrorKind;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use std::os::unix::ffi::OsStrExt;
|
use std::os::unix::ffi::OsStrExt;
|
||||||
use uucore::display::Quotable;
|
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::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 ABOUT: &str = help_about!("join.md");
|
||||||
const USAGE: &str = help_usage!("join.md");
|
const USAGE: &str = help_usage!("join.md");
|
||||||
|
@ -334,37 +334,30 @@ impl<'a> State<'a> {
|
||||||
key: usize,
|
key: usize,
|
||||||
line_ending: LineEnding,
|
line_ending: LineEnding,
|
||||||
print_unpaired: bool,
|
print_unpaired: bool,
|
||||||
) -> State<'a> {
|
) -> UResult<State<'a>> {
|
||||||
let f = if name == "-" {
|
let file_buf = if name == "-" {
|
||||||
Box::new(stdin.lock()) as Box<dyn BufRead>
|
Box::new(stdin.lock()) as Box<dyn BufRead>
|
||||||
} else {
|
} else {
|
||||||
match File::open(name) {
|
let file = File::open(name).map_err_context(|| format!("{}", name.maybe_quote()))?;
|
||||||
Ok(file) => Box::new(BufReader::new(file)) as Box<dyn BufRead>,
|
Box::new(BufReader::new(file)) as Box<dyn BufRead>
|
||||||
Err(err) => crash!(1, "{}: {}", name.maybe_quote(), err),
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
State {
|
Ok(State {
|
||||||
key,
|
key,
|
||||||
file_name: name,
|
file_name: name,
|
||||||
file_num,
|
file_num,
|
||||||
print_unpaired,
|
print_unpaired,
|
||||||
lines: f.split(line_ending as u8),
|
lines: file_buf.split(line_ending as u8),
|
||||||
max_len: 1,
|
max_len: 1,
|
||||||
seq: Vec::new(),
|
seq: Vec::new(),
|
||||||
line_num: 0,
|
line_num: 0,
|
||||||
has_failed: false,
|
has_failed: false,
|
||||||
has_unpaired: false,
|
has_unpaired: false,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Skip the current unpaired line.
|
/// Skip the current unpaired line.
|
||||||
fn skip_line(
|
fn skip_line(&mut self, writer: &mut impl Write, input: &Input, repr: &Repr) -> UResult<()> {
|
||||||
&mut self,
|
|
||||||
writer: &mut impl Write,
|
|
||||||
input: &Input,
|
|
||||||
repr: &Repr,
|
|
||||||
) -> Result<(), JoinError> {
|
|
||||||
if self.print_unpaired {
|
if self.print_unpaired {
|
||||||
self.print_first_line(writer, repr)?;
|
self.print_first_line(writer, repr)?;
|
||||||
}
|
}
|
||||||
|
@ -375,7 +368,7 @@ impl<'a> State<'a> {
|
||||||
|
|
||||||
/// Keep reading line sequence until the key does not change, return
|
/// Keep reading line sequence until the key does not change, return
|
||||||
/// the first line whose key differs.
|
/// 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)? {
|
while let Some(line) = self.next_line(input)? {
|
||||||
let diff = input.compare(self.get_current_key(), line.get_field(self.key));
|
let diff = input.compare(self.get_current_key(), line.get_field(self.key));
|
||||||
|
|
||||||
|
@ -484,12 +477,7 @@ impl<'a> State<'a> {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn finalize(
|
fn finalize(&mut self, writer: &mut impl Write, input: &Input, repr: &Repr) -> UResult<()> {
|
||||||
&mut self,
|
|
||||||
writer: &mut impl Write,
|
|
||||||
input: &Input,
|
|
||||||
repr: &Repr,
|
|
||||||
) -> Result<(), JoinError> {
|
|
||||||
if self.has_line() {
|
if self.has_line() {
|
||||||
if self.print_unpaired {
|
if self.print_unpaired {
|
||||||
self.print_first_line(writer, repr)?;
|
self.print_first_line(writer, repr)?;
|
||||||
|
@ -713,10 +701,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
return Err(USimpleError::new(1, "both files cannot be standard input"));
|
return Err(USimpleError::new(1, "both files cannot be standard input"));
|
||||||
}
|
}
|
||||||
|
|
||||||
match exec(file1, file2, settings) {
|
exec(file1, file2, settings)
|
||||||
Ok(_) => Ok(()),
|
|
||||||
Err(e) => Err(USimpleError::new(1, format!("{e}"))),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uu_app() -> Command {
|
pub fn uu_app() -> Command {
|
||||||
|
@ -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 stdin = stdin();
|
||||||
|
|
||||||
let mut state1 = State::new(
|
let mut state1 = State::new(
|
||||||
|
@ -847,7 +832,7 @@ fn exec(file1: &str, file2: &str, settings: Settings) -> Result<(), JoinError> {
|
||||||
settings.key1,
|
settings.key1,
|
||||||
settings.line_ending,
|
settings.line_ending,
|
||||||
settings.print_unpaired1,
|
settings.print_unpaired1,
|
||||||
);
|
)?;
|
||||||
|
|
||||||
let mut state2 = State::new(
|
let mut state2 = State::new(
|
||||||
FileNum::File2,
|
FileNum::File2,
|
||||||
|
@ -856,7 +841,7 @@ fn exec(file1: &str, file2: &str, settings: Settings) -> Result<(), JoinError> {
|
||||||
settings.key2,
|
settings.key2,
|
||||||
settings.line_ending,
|
settings.line_ending,
|
||||||
settings.print_unpaired2,
|
settings.print_unpaired2,
|
||||||
);
|
)?;
|
||||||
|
|
||||||
let input = Input::new(
|
let input = Input::new(
|
||||||
settings.separator,
|
settings.separator,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue