1use thiserror::Error;
4
5#[derive(Error, Debug)]
7pub enum ViewError {
8 #[error("Template not found: {0}")]
10 TemplateNotFound(String),
11
12 #[error("Template rendering failed: {0}")]
14 RenderError(String),
15
16 #[error("Template parsing failed: {0}")]
18 ParseError(String),
19
20 #[error("Context serialization failed: {0}")]
22 SerializationError(String),
23
24 #[error("Template engine not initialized")]
26 NotInitialized,
27
28 #[error("IO error: {0}")]
30 IoError(#[from] std::io::Error),
31
32 #[error("Tera error: {0}")]
34 Tera(#[from] tera::Error),
35}
36
37impl ViewError {
38 pub fn not_found(template: impl Into<String>) -> Self {
40 Self::TemplateNotFound(template.into())
41 }
42
43 pub fn render_error(msg: impl Into<String>) -> Self {
45 Self::RenderError(msg.into())
46 }
47
48 pub fn parse_error(msg: impl Into<String>) -> Self {
50 Self::ParseError(msg.into())
51 }
52
53 pub fn serialization_error(msg: impl Into<String>) -> Self {
55 Self::SerializationError(msg.into())
56 }
57}
58
59impl From<ViewError> for rustapi_core::ApiError {
60 fn from(err: ViewError) -> Self {
61 match err {
62 ViewError::TemplateNotFound(name) => {
63 rustapi_core::ApiError::internal(format!("Template not found: {}", name))
64 }
65 ViewError::NotInitialized => {
66 rustapi_core::ApiError::internal("Template engine not initialized")
67 }
68 _ => rustapi_core::ApiError::internal(err.to_string()),
69 }
70 }
71}