Skip to main content

gba_pm/
error.rs

1//! Error types for the prompt manager.
2//!
3//! This module defines error types for template operations including
4//! loading, rendering, and I/O operations.
5
6use std::path::PathBuf;
7
8/// Error type for prompt manager operations.
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum PromptError {
12    /// Template not found in the manager.
13    #[error("template not found: {0}")]
14    TemplateNotFound(String),
15
16    /// Error rendering a template.
17    #[error("render error: {0}")]
18    RenderError(#[from] minijinja::Error),
19
20    /// I/O error during template loading.
21    #[error("I/O error at '{path}': {source}")]
22    IoError {
23        /// The path that caused the error.
24        path: PathBuf,
25        /// The underlying I/O error.
26        #[source]
27        source: std::io::Error,
28    },
29}
30
31impl PromptError {
32    /// Create a new I/O error with path context.
33    pub fn io_error(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
34        Self::IoError {
35            path: path.into(),
36            source,
37        }
38    }
39}
40
41/// Result type alias for prompt manager operations.
42pub type Result<T> = std::result::Result<T, PromptError>;
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_should_display_template_not_found_error() {
50        let err = PromptError::TemplateNotFound("test_template".to_string());
51        assert_eq!(err.to_string(), "template not found: test_template");
52    }
53
54    #[test]
55    fn test_should_display_io_error_with_path() {
56        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
57        let err = PromptError::io_error("/path/to/template.j2", io_err);
58        assert!(err.to_string().contains("/path/to/template.j2"));
59        assert!(err.to_string().contains("I/O error"));
60    }
61}