From 9099f342e0c1fbaa829a73277ac1e8577233e493 Mon Sep 17 00:00:00 2001 From: Solomon Victorino Date: Sat, 22 Mar 2025 18:18:16 -0600 Subject: [PATCH] unexpand: move to thiserror --- Cargo.lock | 1 + src/uu/unexpand/Cargo.toml | 1 + src/uu/unexpand/src/unexpand.rs | 23 ++++++----------------- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 05108abcb..c48a73751 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3415,6 +3415,7 @@ name = "uu_unexpand" version = "0.0.30" dependencies = [ "clap", + "thiserror 2.0.12", "unicode-width 0.2.0", "uucore", ] diff --git a/src/uu/unexpand/Cargo.toml b/src/uu/unexpand/Cargo.toml index a096e7b6c..79248a70f 100644 --- a/src/uu/unexpand/Cargo.toml +++ b/src/uu/unexpand/Cargo.toml @@ -17,6 +17,7 @@ readme.workspace = true path = "src/unexpand.rs" [dependencies] +thiserror = { workspace = true } clap = { workspace = true } unicode-width = { workspace = true } uucore = { workspace = true } diff --git a/src/uu/unexpand/src/unexpand.rs b/src/uu/unexpand/src/unexpand.rs index 07abe456f..a0d2db47c 100644 --- a/src/uu/unexpand/src/unexpand.rs +++ b/src/uu/unexpand/src/unexpand.rs @@ -6,13 +6,12 @@ // spell-checker:ignore (ToDO) nums aflag uflag scol prevtab amode ctype cwidth nbytes lastcol pctype Preprocess use clap::{Arg, ArgAction, Command}; -use std::error::Error; -use std::fmt; use std::fs::File; use std::io::{BufRead, BufReader, BufWriter, Read, Stdout, 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, USimpleError}; @@ -23,30 +22,20 @@ const ABOUT: &str = help_about!("unexpand.md"); const DEFAULT_TABSTOP: usize = 8; -#[derive(Debug)] +#[derive(Debug, Error)] enum ParseError { + #[error("tab size contains invalid character(s): {}", _0.quote())] InvalidCharacter(String), + #[error("tab size cannot be 0")] TabSizeCannotBeZero, + #[error("tab stop value is too large")] TabSizeTooLarge, + #[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::TabSizeCannotBeZero => write!(f, "tab size cannot be 0"), - Self::TabSizeTooLarge => write!(f, "tab stop value is too large"), - Self::TabSizesMustBeAscending => write!(f, "tab sizes must be ascending"), - } - } -} - fn tabstops_parse(s: &str) -> Result, ParseError> { let words = s.split(',');