mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-01 05:27: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 }
|
rayon = { workspace = true }
|
||||||
self_cell = { workspace = true }
|
self_cell = { workspace = true }
|
||||||
tempfile = { workspace = true }
|
tempfile = { workspace = true }
|
||||||
|
thiserror = { workspace = true }
|
||||||
unicode-width = { workspace = true }
|
unicode-width = { workspace = true }
|
||||||
uucore = { workspace = true, features = ["fs", "version-cmp"] }
|
uucore = { workspace = true, features = ["fs", "version-cmp"] }
|
||||||
|
|
||||||
|
|
|
@ -30,9 +30,7 @@ use rand::{thread_rng, Rng};
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::error::Error;
|
|
||||||
use std::ffi::{OsStr, OsString};
|
use std::ffi::{OsStr, OsString};
|
||||||
use std::fmt::Display;
|
|
||||||
use std::fs::{File, OpenOptions};
|
use std::fs::{File, OpenOptions};
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write};
|
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::Path;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::str::Utf8Error;
|
use std::str::Utf8Error;
|
||||||
|
use thiserror::Error;
|
||||||
use unicode_width::UnicodeWidthStr;
|
use unicode_width::UnicodeWidthStr;
|
||||||
use uucore::display::Quotable;
|
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::line_ending::LineEnding;
|
||||||
use uucore::parse_size::{ParseSizeError, Parser};
|
use uucore::parse_size::{ParseSizeError, Parser};
|
||||||
use uucore::shortcut_value_parser::ShortcutValueParser;
|
use uucore::shortcut_value_parser::ShortcutValueParser;
|
||||||
|
@ -119,44 +119,43 @@ const POSITIVE: char = '+';
|
||||||
// available memory into consideration, instead of relying on this constant only.
|
// available memory into consideration, instead of relying on this constant only.
|
||||||
const DEFAULT_BUF_SIZE: usize = 1_000_000_000; // 1 GB
|
const DEFAULT_BUF_SIZE: usize = 1_000_000_000; // 1 GB
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Error)]
|
||||||
enum SortError {
|
pub enum SortError {
|
||||||
|
#[error("{}", format_disorder(.file, .line_number, .line, .silent))]
|
||||||
Disorder {
|
Disorder {
|
||||||
file: OsString,
|
file: OsString,
|
||||||
line_number: usize,
|
line_number: usize,
|
||||||
line: String,
|
line: String,
|
||||||
silent: bool,
|
silent: bool,
|
||||||
},
|
},
|
||||||
OpenFailed {
|
|
||||||
path: String,
|
#[error("open failed: {}: {}", .path.maybe_quote(), strip_errno(.error))]
|
||||||
error: std::io::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 {
|
ReadFailed {
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
error: std::io::Error,
|
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 {
|
impl UError for SortError {
|
||||||
fn code(&self) -> i32 {
|
fn code(&self) -> i32 {
|
||||||
|
@ -167,60 +166,11 @@ impl UError for SortError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for SortError {
|
fn format_disorder(file: &OsString, line_number: &usize, line: &String, silent: &bool) -> String {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
if *silent {
|
||||||
match self {
|
String::new()
|
||||||
Self::Disorder {
|
} else {
|
||||||
file,
|
format!("{}:{}: disorder: {}", file.maybe_quote(), line_number, line)
|
||||||
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}"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue