From 13e728de80265b26a9be998e55888d45ccfa6ca2 Mon Sep 17 00:00:00 2001 From: Timothy DeHerrera Date: Thu, 5 Feb 2026 23:46:14 -0700 Subject: [PATCH] refactor(error): remove thiserror, implement Error manually Manual Display and Error trait implementations replace derive macro. Preserves source() chaining for all io::Error and tera::Error variants. One less dependency, same behavior. --- Cargo.lock | 1 - Cargo.toml | 1 - src/error.rs | 67 ++++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 452c523..f2ab52b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1607,7 +1607,6 @@ dependencies = [ "serde", "tempfile", "tera", - "thiserror 2.0.18", "toml 0.8.23", "tree-house", "tree-house-bindings", diff --git a/Cargo.toml b/Cargo.toml index 7de0c18..561f814 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,6 @@ version = "0.1.0" [dependencies] gray_matter = "0.2" pulldown-cmark = "0.12" -thiserror = "2" walkdir = "2" # Syntax highlighting diff --git a/src/error.rs b/src/error.rs index f20c1a5..2d9e7de 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,62 +1,99 @@ //! Custom error types for the sukr compiler. +use std::error::Error as StdError; +use std::fmt; use std::path::PathBuf; /// All errors that can occur during site compilation. -#[derive(Debug, thiserror::Error)] +#[derive(Debug)] pub enum Error { /// Failed to read a content file. - #[error("failed to read {path}: {source}")] ReadFile { path: PathBuf, - #[source] source: std::io::Error, }, /// Failed to parse frontmatter. - #[error("invalid frontmatter in {path}: {message}")] Frontmatter { path: PathBuf, message: String }, /// Failed to write output file. - #[error("failed to write {path}: {source}")] WriteFile { path: PathBuf, - #[source] source: std::io::Error, }, /// Failed to create output directory. - #[error("failed to create directory {path}: {source}")] CreateDir { path: PathBuf, - #[source] source: std::io::Error, }, /// Content directory not found. - #[error("content directory not found: {0}")] ContentDirNotFound(PathBuf), /// Failed to parse configuration file. - #[error("invalid config in {path}: {message}")] Config { path: PathBuf, message: String }, /// Failed to load templates. - #[error("failed to load templates: {0}")] - TemplateLoad(#[source] tera::Error), + TemplateLoad(tera::Error), /// Failed to render template. - #[error("failed to render template '{template}'")] TemplateRender { template: String, - #[source] source: tera::Error, }, /// Failed to bundle CSS. - #[error("CSS bundle error: {0}")] CssBundle(String), } +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Error::ReadFile { path, source } => { + write!(f, "failed to read {}: {}", path.display(), source) + } + Error::Frontmatter { path, message } => { + write!(f, "invalid frontmatter in {}: {}", path.display(), message) + } + Error::WriteFile { path, source } => { + write!(f, "failed to write {}: {}", path.display(), source) + } + Error::CreateDir { path, source } => { + write!( + f, + "failed to create directory {}: {}", + path.display(), + source + ) + } + Error::ContentDirNotFound(path) => { + write!(f, "content directory not found: {}", path.display()) + } + Error::Config { path, message } => { + write!(f, "invalid config in {}: {}", path.display(), message) + } + Error::TemplateLoad(e) => write!(f, "failed to load templates: {}", e), + Error::TemplateRender { template, .. } => { + write!(f, "failed to render template '{}'", template) + } + Error::CssBundle(msg) => write!(f, "CSS bundle error: {}", msg), + } + } +} + +impl StdError for Error { + fn source(&self) -> Option<&(dyn StdError + 'static)> { + match self { + Error::ReadFile { source, .. } => Some(source), + Error::WriteFile { source, .. } => Some(source), + Error::CreateDir { source, .. } => Some(source), + Error::TemplateLoad(e) => Some(e), + Error::TemplateRender { source, .. } => Some(source), + _ => None, + } + } +} + /// Result type alias for compiler operations. pub type Result = std::result::Result;