1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 11:07:44 +00:00

join: fix workaround for IntErrorKind

In Rust versions before 1.55, there was no enum for parse errors. With the bump of the MSRV to 1.56 we can simplify this.
This commit is contained in:
Terts Diepraam 2022-04-01 18:50:42 +02:00 committed by Sylvestre Ledru
parent 4701ea0c95
commit 0fc667730d

View file

@ -18,6 +18,7 @@ use std::error::Error;
use std::fmt::Display;
use std::fs::File;
use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Split, Stdin, Write};
use std::num::IntErrorKind;
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
use uucore::display::Quotable;
@ -965,13 +966,9 @@ fn get_field_number(keys: Option<usize>, key: Option<usize>) -> UResult<usize> {
/// Parse the specified field string as a natural number and return
/// the zero-based field number.
fn parse_field_number(value: &str) -> UResult<usize> {
// TODO: use ParseIntError.kind() once MSRV >= 1.55
// For now, store an overflow Err from parsing a value 10x 64 bit usize::MAX
// Adapted from https://github.com/rust-lang/rust/issues/22639
let overflow = "184467440737095516150".parse::<usize>().err().unwrap();
match value.parse::<usize>() {
Ok(result) if result > 0 => Ok(result - 1),
Err(ref e) if *e == overflow => Ok(usize::MAX),
Err(e) if e.kind() == &IntErrorKind::PosOverflow => Ok(usize::MAX),
_ => Err(USimpleError::new(
1,
format!("invalid field number: {}", value.quote()),