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.
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1607,7 +1607,6 @@ dependencies = [
|
||||
"serde",
|
||||
"tempfile",
|
||||
"tera",
|
||||
"thiserror 2.0.18",
|
||||
"toml 0.8.23",
|
||||
"tree-house",
|
||||
"tree-house-bindings",
|
||||
|
||||
@@ -8,7 +8,6 @@ version = "0.1.0"
|
||||
[dependencies]
|
||||
gray_matter = "0.2"
|
||||
pulldown-cmark = "0.12"
|
||||
thiserror = "2"
|
||||
walkdir = "2"
|
||||
|
||||
# Syntax highlighting
|
||||
|
||||
67
src/error.rs
67
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<T> = std::result::Result<T, Error>;
|
||||
|
||||
Reference in New Issue
Block a user