diff --git a/Cargo.lock b/Cargo.lock index c86bab4b9..05108abcb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3297,6 +3297,7 @@ dependencies = [ "memchr", "memmap2", "regex", + "thiserror 2.0.12", "uucore", ] diff --git a/src/uu/tac/Cargo.toml b/src/uu/tac/Cargo.toml index 3d1fb6c5d..e6ded1a4a 100644 --- a/src/uu/tac/Cargo.toml +++ b/src/uu/tac/Cargo.toml @@ -24,6 +24,7 @@ memmap2 = { workspace = true } regex = { workspace = true } clap = { workspace = true } uucore = { workspace = true } +thiserror = { workspace = true } [[bin]] name = "tac" diff --git a/src/uu/tac/src/error.rs b/src/uu/tac/src/error.rs index 7a737ad9b..fc01bbffd 100644 --- a/src/uu/tac/src/error.rs +++ b/src/uu/tac/src/error.rs @@ -3,33 +3,37 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. //! Errors returned by tac during processing of a file. -use std::error::Error; -use std::fmt::Display; +use thiserror::Error; use uucore::display::Quotable; use uucore::error::UError; -#[derive(Debug)] +#[derive(Debug, Error)] pub enum TacError { /// A regular expression given by the user is invalid. + #[error("invalid regular expression: {0}")] InvalidRegex(regex::Error), /// An argument to tac is invalid. + #[error("{}: read error: Invalid argument", _0.maybe_quote())] InvalidArgument(String), /// The specified file is not found on the filesystem. + #[error("failed to open {} for reading: No such file or directory", _0.quote())] FileNotFound(String), /// An error reading the contents of a file or stdin. /// /// The parameters are the name of the file and the underlying /// [`std::io::Error`] that caused this error. + #[error("failed to read from {0}: {1}")] ReadError(String, std::io::Error), /// An error writing the (reversed) contents of a file or stdin. /// /// The parameter is the underlying [`std::io::Error`] that caused /// this error. + #[error("failed to write to stdout: {0}")] WriteError(std::io::Error), } @@ -38,23 +42,3 @@ impl UError for TacError { 1 } } - -impl Error for TacError {} - -impl Display for TacError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::InvalidRegex(e) => write!(f, "invalid regular expression: {e}"), - Self::InvalidArgument(s) => { - write!(f, "{}: read error: Invalid argument", s.maybe_quote()) - } - Self::FileNotFound(s) => write!( - f, - "failed to open {} for reading: No such file or directory", - s.quote() - ), - Self::ReadError(s, e) => write!(f, "failed to read from {s}: {e}"), - Self::WriteError(e) => write!(f, "failed to write to stdout: {e}"), - } - } -}