mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-31 04:57:45 +00:00
sort: move to thiserror
This commit is contained in:
parent
0a230e58b7
commit
740cea7a46
2 changed files with 35 additions and 84 deletions
|
@ -28,6 +28,7 @@ rand = { workspace = true }
|
|||
rayon = { workspace = true }
|
||||
self_cell = { workspace = true }
|
||||
tempfile = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
unicode-width = { workspace = true }
|
||||
uucore = { workspace = true, features = ["fs", "version-cmp"] }
|
||||
|
||||
|
|
|
@ -30,9 +30,7 @@ use rand::{thread_rng, Rng};
|
|||
use rayon::prelude::*;
|
||||
use std::cmp::Ordering;
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::fmt::Display;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write};
|
||||
|
@ -40,9 +38,11 @@ use std::ops::Range;
|
|||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::str::Utf8Error;
|
||||
use thiserror::Error;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
use uucore::display::Quotable;
|
||||
use uucore::error::{set_exit_code, strip_errno, UError, UResult, USimpleError, UUsageError};
|
||||
use uucore::error::strip_errno;
|
||||
use uucore::error::{set_exit_code, UError, UResult, USimpleError, UUsageError};
|
||||
use uucore::line_ending::LineEnding;
|
||||
use uucore::parse_size::{ParseSizeError, Parser};
|
||||
use uucore::shortcut_value_parser::ShortcutValueParser;
|
||||
|
@ -119,44 +119,43 @@ const POSITIVE: char = '+';
|
|||
// available memory into consideration, instead of relying on this constant only.
|
||||
const DEFAULT_BUF_SIZE: usize = 1_000_000_000; // 1 GB
|
||||
|
||||
#[derive(Debug)]
|
||||
enum SortError {
|
||||
#[derive(Debug, Error)]
|
||||
pub enum SortError {
|
||||
#[error("{}", format_disorder(.file, .line_number, .line, .silent))]
|
||||
Disorder {
|
||||
file: OsString,
|
||||
line_number: usize,
|
||||
line: String,
|
||||
silent: bool,
|
||||
},
|
||||
OpenFailed {
|
||||
path: String,
|
||||
error: std::io::Error,
|
||||
},
|
||||
|
||||
#[error("open failed: {}: {}", .path.maybe_quote(), strip_errno(.error))]
|
||||
OpenFailed { path: String, error: std::io::Error },
|
||||
|
||||
#[error("failed to parse key {}: {}", .key.quote(), .msg)]
|
||||
ParseKeyError { key: String, msg: String },
|
||||
|
||||
#[error("cannot read: {}: {}", .path.maybe_quote(), strip_errno(.error))]
|
||||
ReadFailed {
|
||||
path: PathBuf,
|
||||
error: std::io::Error,
|
||||
},
|
||||
ParseKeyError {
|
||||
key: String,
|
||||
msg: String,
|
||||
},
|
||||
OpenTmpFileFailed {
|
||||
error: std::io::Error,
|
||||
},
|
||||
CompressProgExecutionFailed {
|
||||
code: i32,
|
||||
},
|
||||
CompressProgTerminatedAbnormally {
|
||||
prog: String,
|
||||
},
|
||||
TmpFileCreationFailed {
|
||||
path: PathBuf,
|
||||
},
|
||||
Uft8Error {
|
||||
error: Utf8Error,
|
||||
},
|
||||
}
|
||||
|
||||
impl Error for SortError {}
|
||||
#[error("failed to open temporary file: {}", strip_errno(.error))]
|
||||
OpenTmpFileFailed { error: std::io::Error },
|
||||
|
||||
#[error("couldn't execute compress program: errno {code}")]
|
||||
CompressProgExecutionFailed { code: i32 },
|
||||
|
||||
#[error("{} terminated abnormally", .prog.quote())]
|
||||
CompressProgTerminatedAbnormally { prog: String },
|
||||
|
||||
#[error("cannot create temporary file in '{}':", .path.display())]
|
||||
TmpFileCreationFailed { path: PathBuf },
|
||||
|
||||
#[error("{error}")]
|
||||
Uft8Error { error: Utf8Error },
|
||||
}
|
||||
|
||||
impl UError for SortError {
|
||||
fn code(&self) -> i32 {
|
||||
|
@ -167,60 +166,11 @@ impl UError for SortError {
|
|||
}
|
||||
}
|
||||
|
||||
impl Display for SortError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Disorder {
|
||||
file,
|
||||
line_number,
|
||||
line,
|
||||
silent,
|
||||
} => {
|
||||
if *silent {
|
||||
Ok(())
|
||||
} else {
|
||||
write!(
|
||||
f,
|
||||
"{}:{}: disorder: {}",
|
||||
file.maybe_quote(),
|
||||
line_number,
|
||||
line
|
||||
)
|
||||
}
|
||||
}
|
||||
Self::OpenFailed { path, error } => {
|
||||
write!(
|
||||
f,
|
||||
"open failed: {}: {}",
|
||||
path.maybe_quote(),
|
||||
strip_errno(error)
|
||||
)
|
||||
}
|
||||
Self::ParseKeyError { key, msg } => {
|
||||
write!(f, "failed to parse key {}: {}", key.quote(), msg)
|
||||
}
|
||||
Self::ReadFailed { path, error } => {
|
||||
write!(
|
||||
f,
|
||||
"cannot read: {}: {}",
|
||||
path.maybe_quote(),
|
||||
strip_errno(error)
|
||||
)
|
||||
}
|
||||
Self::OpenTmpFileFailed { error } => {
|
||||
write!(f, "failed to open temporary file: {}", strip_errno(error))
|
||||
}
|
||||
Self::CompressProgExecutionFailed { code } => {
|
||||
write!(f, "couldn't execute compress program: errno {code}")
|
||||
}
|
||||
Self::CompressProgTerminatedAbnormally { prog } => {
|
||||
write!(f, "{} terminated abnormally", prog.quote())
|
||||
}
|
||||
Self::TmpFileCreationFailed { path } => {
|
||||
write!(f, "cannot create temporary file in '{}':", path.display())
|
||||
}
|
||||
Self::Uft8Error { error } => write!(f, "{error}"),
|
||||
}
|
||||
fn format_disorder(file: &OsString, line_number: &usize, line: &String, silent: &bool) -> String {
|
||||
if *silent {
|
||||
String::new()
|
||||
} else {
|
||||
format!("{}:{}: disorder: {}", file.maybe_quote(), line_number, line)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue