1use std::path::PathBuf;
7
8#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum PromptError {
12 #[error("template not found: {0}")]
14 TemplateNotFound(String),
15
16 #[error("render error: {0}")]
18 RenderError(#[from] minijinja::Error),
19
20 #[error("I/O error at '{path}': {source}")]
22 IoError {
23 path: PathBuf,
25 #[source]
27 source: std::io::Error,
28 },
29}
30
31impl PromptError {
32 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
41pub 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}