ricecoder_generation/templates/
error.rs

1//! Error types for template operations
2
3use thiserror::Error;
4
5/// Errors that can occur during template operations
6#[derive(Debug, Error)]
7pub enum TemplateError {
8    /// Template not found
9    #[error("Template not found: {0}")]
10    NotFound(String),
11
12    /// Missing required placeholder
13    #[error("Missing required placeholder: {0}")]
14    MissingPlaceholder(String),
15
16    /// Invalid template syntax
17    #[error("Invalid template syntax at line {line}: {message}")]
18    InvalidSyntax {
19        /// Line number where syntax error occurred
20        line: usize,
21        /// Error message describing the syntax issue
22        message: String,
23    },
24
25    /// Template rendering error
26    #[error("Render error: {0}")]
27    RenderError(String),
28
29    /// Validation failed
30    #[error("Validation failed: {0}")]
31    ValidationFailed(String),
32
33    /// IO error
34    #[error("IO error: {0}")]
35    IoError(#[from] std::io::Error),
36}
37
38impl PartialEq for TemplateError {
39    fn eq(&self, other: &Self) -> bool {
40        match (self, other) {
41            (TemplateError::NotFound(a), TemplateError::NotFound(b)) => a == b,
42            (TemplateError::MissingPlaceholder(a), TemplateError::MissingPlaceholder(b)) => a == b,
43            (
44                TemplateError::InvalidSyntax {
45                    line: line_a,
46                    message: msg_a,
47                },
48                TemplateError::InvalidSyntax {
49                    line: line_b,
50                    message: msg_b,
51                },
52            ) => line_a == line_b && msg_a == msg_b,
53            (TemplateError::RenderError(a), TemplateError::RenderError(b)) => a == b,
54            (TemplateError::ValidationFailed(a), TemplateError::ValidationFailed(b)) => a == b,
55            (TemplateError::IoError(_), TemplateError::IoError(_)) => {
56                // IO errors can't be compared, so we consider them equal if both are IO errors
57                true
58            }
59            _ => false,
60        }
61    }
62}
63
64/// Errors that can occur during boilerplate operations
65#[derive(Debug, Error)]
66pub enum BoilerplateError {
67    /// Boilerplate not found
68    #[error("Boilerplate not found: {0}")]
69    NotFound(String),
70
71    /// Invalid boilerplate structure
72    #[error("Invalid boilerplate structure: {0}")]
73    InvalidStructure(String),
74
75    /// File conflict during scaffolding
76    #[error("File conflict at {path}: {reason}")]
77    FileConflict {
78        /// Path to the conflicting file
79        path: String,
80        /// Reason for the conflict
81        reason: String,
82    },
83
84    /// Boilerplate validation failed
85    #[error("Boilerplate validation failed: {0}")]
86    ValidationFailed(String),
87
88    /// IO error
89    #[error("IO error: {0}")]
90    IoError(#[from] std::io::Error),
91}
92
93impl PartialEq for BoilerplateError {
94    fn eq(&self, other: &Self) -> bool {
95        match (self, other) {
96            (BoilerplateError::NotFound(a), BoilerplateError::NotFound(b)) => a == b,
97            (BoilerplateError::InvalidStructure(a), BoilerplateError::InvalidStructure(b)) => {
98                a == b
99            }
100            (
101                BoilerplateError::FileConflict {
102                    path: path_a,
103                    reason: reason_a,
104                },
105                BoilerplateError::FileConflict {
106                    path: path_b,
107                    reason: reason_b,
108                },
109            ) => path_a == path_b && reason_a == reason_b,
110            (BoilerplateError::ValidationFailed(a), BoilerplateError::ValidationFailed(b)) => {
111                a == b
112            }
113            (BoilerplateError::IoError(_), BoilerplateError::IoError(_)) => {
114                // IO errors can't be compared, so we consider them equal if both are IO errors
115                true
116            }
117            _ => false,
118        }
119    }
120}