use standout_dispatch::verify::HandlerMismatchError;
use standout_render::{RegistryError, RenderError};
#[derive(Debug)]
pub enum SetupError {
Template(String),
Stylesheet(String),
ThemeNotFound(String),
Config(String),
DuplicateCommand(String),
Io(std::io::Error),
VerificationFailed(HandlerMismatchError),
}
impl std::fmt::Display for SetupError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SetupError::Template(msg) => write!(f, "template error: {}", msg),
SetupError::Stylesheet(msg) => write!(f, "stylesheet error: {}", msg),
SetupError::ThemeNotFound(name) => write!(
f,
"theme `{}` not found; add styles with .styles(embed_styles!(\"src/styles\")) or .styles_dir(\"path/to/styles\"), select an available theme with .default_theme(...), or pass a Theme directly with .theme(...)",
name
),
SetupError::Config(msg) => write!(f, "configuration error: {}", msg),
SetupError::DuplicateCommand(cmd) => write!(f, "duplicate command: {}", cmd),
SetupError::Io(err) => write!(f, "setup I/O error: {}", err),
SetupError::VerificationFailed(err) => write!(f, "verification failed:\n{}", err),
}
}
}
impl From<std::io::Error> for SetupError {
fn from(e: std::io::Error) -> Self {
SetupError::Io(e)
}
}
impl std::error::Error for SetupError {}
impl From<RenderError> for SetupError {
fn from(e: RenderError) -> Self {
SetupError::Template(e.to_string())
}
}
impl From<RegistryError> for SetupError {
fn from(e: RegistryError) -> Self {
SetupError::Template(e.to_string())
}
}
impl From<HandlerMismatchError> for SetupError {
fn from(e: HandlerMismatchError) -> Self {
SetupError::VerificationFailed(e)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_setup_error_display() {
let err = SetupError::Template("test error".into());
assert_eq!(err.to_string(), "template error: test error");
let err = SetupError::ThemeNotFound("dark".into());
assert!(err.to_string().contains("theme `dark` not found"));
assert!(err.to_string().contains(".styles(embed_styles!"));
assert!(err.to_string().contains(".theme(...)"));
}
}