From f9cc94c633e8d8c5a4f5c3fec8528d00bbb7517f Mon Sep 17 00:00:00 2001 From: Willem Vanhulle Date: Wed, 21 Oct 2026 11:45:36 +0200 Subject: [PATCH] refactor: derive errors with thiserror --- Cargo.lock | 1 + Cargo.toml | 1 + src/config_file.rs | 24 ++++++------------- src/watch_error.rs | 59 ++++++++++------------------------------------ 4 files changed, 22 insertions(+), 63 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5e3712f..5128cdf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -157,6 +157,7 @@ dependencies = [ "ser_nix", "serde", "signal-hook 0.4.5", + "thiserror 2.0.21", "toml", "unicode-truncate", "unicode-width", diff --git a/Cargo.toml b/Cargo.toml index acdd96c..0a676d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ rustix = { version = "1.1.5", features = ["event", "fs", "process", "stdio", "sy ser_nix = "0.2.4" serde = { version = "1.0.229", features = ["derive"] } signal-hook = "0.4.5" +thiserror = "2.0.12" toml = { version = "1.1.8", features = ["preserve_order"] } unicode-truncate = "2.0.1" unicode-width = "0.2.2" diff --git a/src/config_file.rs b/src/config_file.rs index 26e5439..2616087 100644 --- a/src/config_file.rs +++ b/src/config_file.rs @@ -1,4 +1,4 @@ -use std::{error, fmt, fs, io, path::Path, time::Duration}; +use std::{fs, io, path::Path, time::Duration}; use heck::ToKebabCase; use jiff::fmt::serde::unsigned_duration::friendly::compact::required as friendly_duration; @@ -35,26 +35,16 @@ impl Default for Timing { } } -#[derive(Debug)] +#[derive(Debug, thiserror::Error)] pub enum ConfigError { - Unreadable(io::Error), - Invalid(ParseError), + #[error("cannot read: {0}")] + Unreadable(#[from] io::Error), + #[error("invalid: {0}")] + Invalid(#[from] ParseError), } -impl fmt::Display for ConfigError { - fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Unreadable(error) => write!(formatter, "cannot read: {error}"), - Self::Invalid(error) => write!(formatter, "invalid: {error}"), - } - } -} - -impl error::Error for ConfigError {} - pub fn read_config_file(path: &Path) -> Result { - let text = fs::read_to_string(path).map_err(ConfigError::Unreadable)?; - toml::from_str(&text).map_err(ConfigError::Invalid) + Ok(toml::from_str(&fs::read_to_string(path)?)?) } pub fn default_config_table() -> Result { diff --git a/src/watch_error.rs b/src/watch_error.rs index 23ad905..5bbf82b 100644 --- a/src/watch_error.rs +++ b/src/watch_error.rs @@ -1,55 +1,22 @@ -use std::{error, fmt, io}; +use std::io; use dbus::{Error as BusError, arg::TypeMismatchError}; use rustix::io::Errno; -#[derive(Debug)] +#[derive(Debug, thiserror::Error)] pub enum Error { - Bus(BusError), - Reply(TypeMismatchError), + #[error("systemd bus: {0}")] + Bus(#[from] BusError), + #[error("unexpected reply from systemd: {0}")] + Reply(#[from] TypeMismatchError), + #[error("could not build a method call")] MalformedCall, + #[error("the service manager closed the connection")] Disconnected, - Console(io::Error), - RootSwitch(Errno), + #[error("console: {0}")] + Console(#[from] io::Error), + #[error("entering the new root: {0}")] + RootSwitch(#[from] Errno), + #[error("the service manager did not come back")] ManagerUnreachable, } - -impl fmt::Display for Error { - fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Bus(error) => write!(formatter, "systemd bus: {error}"), - Self::Reply(error) => write!(formatter, "unexpected reply from systemd: {error}"), - Self::MalformedCall => write!(formatter, "could not build a method call"), - Self::Disconnected => write!(formatter, "the service manager closed the connection"), - Self::Console(error) => write!(formatter, "console: {error}"), - Self::RootSwitch(error) => write!(formatter, "entering the new root: {error}"), - Self::ManagerUnreachable => write!(formatter, "the service manager did not come back"), - } - } -} - -impl error::Error for Error {} - -impl From for Error { - fn from(error: BusError) -> Self { - Self::Bus(error) - } -} - -impl From for Error { - fn from(error: TypeMismatchError) -> Self { - Self::Reply(error) - } -} - -impl From for Error { - fn from(error: io::Error) -> Self { - Self::Console(error) - } -} - -impl From for Error { - fn from(error: Errno) -> Self { - Self::RootSwitch(error) - } -}