From 4895b0c9bda9e46fc7db173e239d155dac957186 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Fri, 28 Jul 2023 12:44:57 -0700 Subject: [PATCH] config: Implement `fmt::Display` and `std::error::Error` for `Error` (#136) --- cosmic-config/src/lib.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cosmic-config/src/lib.rs b/cosmic-config/src/lib.rs index 20e03a55..e3d82138 100644 --- a/cosmic-config/src/lib.rs +++ b/cosmic-config/src/lib.rs @@ -9,7 +9,7 @@ use notify::{ use serde::{de::DeserializeOwned, Serialize}; use std::{ borrow::Cow, - fs, + fmt, fs, hash::Hash, io::Write, path::{Path, PathBuf}, @@ -33,6 +33,22 @@ pub enum Error { RonSpanned(ron::error::SpannedError), } +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Self::AtomicWrites(err) => err.fmt(f), + Self::InvalidName(name) => write!(f, "invalid config name '{}'", name), + Self::Io(err) => err.fmt(f), + Self::NoConfigDirectory => write!(f, "cosmic config directory not found"), + Self::Notify(err) => err.fmt(f), + Self::Ron(err) => err.fmt(f), + Self::RonSpanned(err) => err.fmt(f), + } + } +} + +impl std::error::Error for Error {} + impl From> for Error { fn from(f: atomicwrites::Error) -> Self { Self::AtomicWrites(f)