Skip to main content

epics_base_rs/server/autosave/
error.rs

1use std::fmt;
2
3use crate::error::CaError;
4
5/// Result type for autosave operations.
6pub type AutosaveResult<T> = Result<T, AutosaveError>;
7
8/// Errors that can occur during autosave operations.
9#[derive(Debug)]
10pub enum AutosaveError {
11    Io(std::io::Error),
12    RequestFile { path: String, message: String },
13    IncludeCycle { chain: Vec<String> },
14    IncludeDepthExceeded(usize),
15    UndefinedMacro { key: String, source: String, line: usize },
16    CorruptSaveFile { path: String, message: String },
17    PvNotFound(String),
18    Ca(CaError),
19}
20
21impl fmt::Display for AutosaveError {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        match self {
24            Self::Io(e) => write!(f, "I/O error: {e}"),
25            Self::RequestFile { path, message } => {
26                write!(f, "request file error in '{path}': {message}")
27            }
28            Self::IncludeCycle { chain } => {
29                write!(f, "include cycle detected: {}", chain.join(" -> "))
30            }
31            Self::IncludeDepthExceeded(depth) => {
32                write!(f, "include depth exceeded maximum of {depth}")
33            }
34            Self::UndefinedMacro { key, source, line } => {
35                write!(f, "undefined macro '{key}' in {source} at line {line}")
36            }
37            Self::CorruptSaveFile { path, message } => {
38                write!(f, "corrupt save file '{path}': {message}")
39            }
40            Self::PvNotFound(name) => write!(f, "PV not found: {name}"),
41            Self::Ca(e) => write!(f, "CA error: {e}"),
42        }
43    }
44}
45
46impl std::error::Error for AutosaveError {
47    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
48        match self {
49            Self::Io(e) => Some(e),
50            Self::Ca(e) => Some(e),
51            _ => None,
52        }
53    }
54}
55
56impl From<std::io::Error> for AutosaveError {
57    fn from(e: std::io::Error) -> Self {
58        Self::Io(e)
59    }
60}
61
62impl From<CaError> for AutosaveError {
63    fn from(e: CaError) -> Self {
64        Self::Ca(e)
65    }
66}