echo_comment/
error.rs

1use std::fmt;
2use std::io;
3
4pub type Result<T> = std::result::Result<T, EchoCommentError>;
5
6#[derive(Debug)]
7pub enum EchoCommentError {
8    FileRead {
9        path: String,
10        source: io::Error,
11    },
12    FileWrite {
13        source: io::Error,
14    },
15    ScriptExecution {
16        message: String,
17        source: io::Error,
18    },
19    TempFileCreation {
20        source: io::Error,
21    },
22    #[cfg(unix)]
23    PermissionSet {
24        source: io::Error,
25    },
26}
27
28impl fmt::Display for EchoCommentError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            EchoCommentError::FileRead { path, source } => {
32                write!(f, "Failed to read script '{}': {}", path, source)
33            }
34            EchoCommentError::FileWrite { source } => {
35                write!(f, "Failed to write processed script: {}", source)
36            }
37            EchoCommentError::ScriptExecution { message, source } => {
38                write!(f, "{}: {}", message, source)
39            }
40            EchoCommentError::TempFileCreation { source } => {
41                write!(f, "Failed to create temporary file: {}", source)
42            }
43            #[cfg(unix)]
44            EchoCommentError::PermissionSet { source } => {
45                write!(f, "Failed to set executable permissions: {}", source)
46            }
47        }
48    }
49}
50
51impl std::error::Error for EchoCommentError {
52    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
53        match self {
54            EchoCommentError::FileRead { source, .. }
55            | EchoCommentError::FileWrite { source }
56            | EchoCommentError::ScriptExecution { source, .. }
57            | EchoCommentError::TempFileCreation { source } => Some(source),
58            #[cfg(unix)]
59            EchoCommentError::PermissionSet { source } => Some(source),
60        }
61    }
62}