From 740cea7a463245fd3486741809a384735a32ac07 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 19 Jan 2025 23:42:17 +0100 Subject: [PATCH] sort: move to thiserror --- src/uu/sort/Cargo.toml | 1 + src/uu/sort/src/sort.rs | 118 ++++++++++++---------------------------- 2 files changed, 35 insertions(+), 84 deletions(-) diff --git a/src/uu/sort/Cargo.toml b/src/uu/sort/Cargo.toml index 99c1254c0..d0e45a344 100644 --- a/src/uu/sort/Cargo.toml +++ b/src/uu/sort/Cargo.toml @@ -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"] } diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index 8b6fcbb25..0c39c2442 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -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) } }