use thiserror::Error;
#[derive(Error, Debug)]
pub enum ViewError {
#[error("Template not found: {0}")]
TemplateNotFound(String),
#[error("Template rendering failed: {0}")]
RenderError(String),
#[error("Template parsing failed: {0}")]
ParseError(String),
#[error("Context serialization failed: {0}")]
SerializationError(String),
#[error("Template engine not initialized")]
NotInitialized,
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
#[error("Tera error: {0}")]
Tera(#[from] tera::Error),
}
impl ViewError {
pub fn not_found(template: impl Into<String>) -> Self {
Self::TemplateNotFound(template.into())
}
pub fn render_error(msg: impl Into<String>) -> Self {
Self::RenderError(msg.into())
}
pub fn parse_error(msg: impl Into<String>) -> Self {
Self::ParseError(msg.into())
}
pub fn serialization_error(msg: impl Into<String>) -> Self {
Self::SerializationError(msg.into())
}
}
impl From<ViewError> for rustapi_core::ApiError {
fn from(err: ViewError) -> Self {
match err {
ViewError::TemplateNotFound(name) => {
rustapi_core::ApiError::internal(format!("Template not found: {}", name))
}
ViewError::NotInitialized => {
rustapi_core::ApiError::internal("Template engine not initialized")
}
_ => rustapi_core::ApiError::internal(err.to_string()),
}
}
}