From 4aba193c9c6bb9d3d69c564b7db449dbfee1ddac Mon Sep 17 00:00:00 2001 From: Solomon Victorino Date: Sat, 22 Mar 2025 18:18:15 -0600 Subject: [PATCH] expand: move to thiserror --- Cargo.lock | 1 + src/uu/expand/Cargo.toml | 1 + src/uu/expand/src/expand.rs | 36 ++++++++---------------------------- 3 files changed, 10 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 70581006f..0299274c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2736,6 +2736,7 @@ name = "uu_expand" version = "0.0.30" dependencies = [ "clap", + "thiserror 2.0.12", "unicode-width 0.2.0", "uucore", ] diff --git a/src/uu/expand/Cargo.toml b/src/uu/expand/Cargo.toml index 5c0a6d672..ad25d7dd6 100644 --- a/src/uu/expand/Cargo.toml +++ b/src/uu/expand/Cargo.toml @@ -20,6 +20,7 @@ path = "src/expand.rs" clap = { workspace = true } unicode-width = { workspace = true } uucore = { workspace = true } +thiserror = { workspace = true } [[bin]] name = "expand" diff --git a/src/uu/expand/src/expand.rs b/src/uu/expand/src/expand.rs index 3cde28ac6..47d1796de 100644 --- a/src/uu/expand/src/expand.rs +++ b/src/uu/expand/src/expand.rs @@ -6,14 +6,13 @@ // spell-checker:ignore (ToDO) ctype cwidth iflag nbytes nspaces nums tspaces uflag Preprocess use clap::{Arg, ArgAction, ArgMatches, Command}; -use std::error::Error; use std::ffi::OsString; -use std::fmt; use std::fs::File; use std::io::{BufRead, BufReader, BufWriter, Read, Write, stdin, stdout}; use std::num::IntErrorKind; use std::path::Path; use std::str::from_utf8; +use thiserror::Error; use unicode_width::UnicodeWidthChar; use uucore::display::Quotable; use uucore::error::{FromIo, UError, UResult, set_exit_code}; @@ -61,43 +60,24 @@ fn is_digit_or_comma(c: char) -> bool { } /// Errors that can occur when parsing a `--tabs` argument. -#[derive(Debug)] +#[derive(Debug, Error)] enum ParseError { + #[error("tab size contains invalid character(s): {}", .0.quote())] InvalidCharacter(String), + #[error("{} specifier not at start of number: {}", .0.quote(), .1.quote())] SpecifierNotAtStartOfNumber(String, String), + #[error("{} specifier only allowed with the last value", .0.quote())] SpecifierOnlyAllowedWithLastValue(String), + #[error("tab size cannot be 0")] TabSizeCannotBeZero, + #[error("tab stop is too large {}", .0.quote())] TabSizeTooLarge(String), + #[error("tab sizes must be ascending")] TabSizesMustBeAscending, } -impl Error for ParseError {} impl UError for ParseError {} -impl fmt::Display for ParseError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - Self::InvalidCharacter(s) => { - write!(f, "tab size contains invalid character(s): {}", s.quote()) - } - Self::SpecifierNotAtStartOfNumber(specifier, s) => write!( - f, - "{} specifier not at start of number: {}", - specifier.quote(), - s.quote(), - ), - Self::SpecifierOnlyAllowedWithLastValue(specifier) => write!( - f, - "{} specifier only allowed with the last value", - specifier.quote() - ), - Self::TabSizeCannotBeZero => write!(f, "tab size cannot be 0"), - Self::TabSizeTooLarge(s) => write!(f, "tab stop is too large {}", s.quote()), - Self::TabSizesMustBeAscending => write!(f, "tab sizes must be ascending"), - } - } -} - /// Parse a list of tabstops from a `--tabs` argument. /// /// This function returns both the vector of numbers appearing in the