Skip to main content

agent_runbook/
error.rs

1use std::{fmt, path::PathBuf};
2
3pub type Result<T> = std::result::Result<T, RunbookError>;
4
5#[derive(Debug)]
6pub enum RunbookError {
7    CurrentDir(std::io::Error),
8    Io {
9        action: &'static str,
10        path: PathBuf,
11        source: std::io::Error,
12    },
13    PreferenceParse {
14        path: PathBuf,
15        source: serde_yaml::Error,
16    },
17    PreferenceWrite {
18        path: PathBuf,
19        source: serde_yaml::Error,
20    },
21}
22
23impl RunbookError {
24    pub fn current_dir(error: std::io::Error) -> Self {
25        Self::CurrentDir(error)
26    }
27
28    pub fn io(action: &'static str, path: PathBuf, source: std::io::Error) -> Self {
29        Self::Io {
30            action,
31            path,
32            source,
33        }
34    }
35
36    pub fn preference_parse(path: PathBuf, source: serde_yaml::Error) -> Self {
37        Self::PreferenceParse { path, source }
38    }
39
40    pub fn preference_write(path: PathBuf, source: serde_yaml::Error) -> Self {
41        Self::PreferenceWrite { path, source }
42    }
43}
44
45impl fmt::Display for RunbookError {
46    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
47        match self {
48            Self::CurrentDir(error) => {
49                write!(formatter, "Failed to read current directory: {error}")
50            }
51            Self::Io {
52                action,
53                path,
54                source,
55            } => write!(formatter, "Failed to {action} {}: {source}", path.display()),
56            Self::PreferenceParse { path, source } => write!(
57                formatter,
58                "Failed to parse preference file {}: {source}",
59                path.display()
60            ),
61            Self::PreferenceWrite { path, source } => write!(
62                formatter,
63                "Failed to serialize preference file {}: {source}",
64                path.display()
65            ),
66        }
67    }
68}
69
70impl std::error::Error for RunbookError {}