From 94b655c07330afb8698b3ead3ea25a6d0c095ce5 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Mon, 20 Jan 2025 19:00:29 +0100 Subject: [PATCH] tsort: move to thiserror --- src/uu/tsort/Cargo.toml | 1 + src/uu/tsort/src/tsort.rs | 25 ++++++------------------- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/src/uu/tsort/Cargo.toml b/src/uu/tsort/Cargo.toml index ba53e0d7b..77c2686a4 100644 --- a/src/uu/tsort/Cargo.toml +++ b/src/uu/tsort/Cargo.toml @@ -18,6 +18,7 @@ path = "src/tsort.rs" [dependencies] clap = { workspace = true } +thiserror = { workspace = true } uucore = { workspace = true } [[bin]] diff --git a/src/uu/tsort/src/tsort.rs b/src/uu/tsort/src/tsort.rs index aac0a055f..c051ff364 100644 --- a/src/uu/tsort/src/tsort.rs +++ b/src/uu/tsort/src/tsort.rs @@ -5,8 +5,8 @@ //spell-checker:ignore TAOCP indegree use clap::{crate_version, Arg, Command}; use std::collections::{HashMap, HashSet, VecDeque}; -use std::fmt::Display; use std::path::Path; +use thiserror::Error; use uucore::display::Quotable; use uucore::error::{UError, UResult}; use uucore::{format_usage, help_about, help_usage, show}; @@ -18,43 +18,30 @@ mod options { pub const FILE: &str = "file"; } -#[derive(Debug)] +#[derive(Debug, Error)] enum TsortError { /// The input file is actually a directory. + #[error("{0}: read error: Is a directory")] IsDir(String), /// The number of tokens in the input data is odd. /// /// The list of edges must be even because each edge has two /// components: a source node and a target node. + #[error("{input}: input contains an odd number of tokens", input = .0.maybe_quote())] NumTokensOdd(String), /// The graph contains a cycle. + #[error("{0}: input contains a loop:")] Loop(String), /// A particular node in a cycle. (This is mainly used for printing.) + #[error("{0}")] LoopNode(String), } -impl std::error::Error for TsortError {} - impl UError for TsortError {} -impl Display for TsortError { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - match self { - Self::IsDir(d) => write!(f, "{d}: read error: Is a directory"), - Self::NumTokensOdd(i) => write!( - f, - "{}: input contains an odd number of tokens", - i.maybe_quote() - ), - Self::Loop(i) => write!(f, "{i}: input contains a loop:"), - Self::LoopNode(v) => write!(f, "{v}"), - } - } -} - #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app().try_get_matches_from(args)?;