refactor: derive errors with thiserror

This commit is contained in:
Willem Vanhulle committed 2026-10-21 12:12:49 +02:00
1 parent c11a509090
commit f9cc94c633
4 files changed
+22 -63

No files matched your search

Generated
+1
View File
@@ -157,6 +157,7 @@ dependencies = [
"ser_nix",
"serde",
"signal-hook 0.4.5",
"thiserror 2.0.21",
"toml",
"unicode-truncate",
"unicode-width",
+1
View File
@@ -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"
+7 -17
View File
@@ -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<Config, ConfigError> {
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<Table, SerializeError> {
+13 -46
View File
@@ -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<BusError> for Error {
fn from(error: BusError) -> Self {
Self::Bus(error)
}
}
impl From<TypeMismatchError> for Error {
fn from(error: TypeMismatchError) -> Self {
Self::Reply(error)
}
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Self::Console(error)
}
}
impl From<Errno> for Error {
fn from(error: Errno) -> Self {
Self::RootSwitch(error)
}
}