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 {
13        path: String,
14        message: String,
15    },
16    IncludeCycle {
17        chain: Vec<String>,
18    },
19    IncludeDepthExceeded(usize),
20    UndefinedMacro {
21        key: String,
22        source: String,
23        line: usize,
24    },
25    CorruptSaveFile {
26        path: String,
27        message: String,
28    },
29    PvNotFound(String),
30    Ca(CaError),
31}
32
33impl fmt::Display for AutosaveError {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match self {
36            Self::Io(e) => write!(f, "I/O error: {e}"),
37            Self::RequestFile { path, message } => {
38                write!(f, "request file error in '{path}': {message}")
39            }
40            Self::IncludeCycle { chain } => {
41                write!(f, "include cycle detected: {}", chain.join(" -> "))
42            }
43            Self::IncludeDepthExceeded(depth) => {
44                write!(f, "include depth exceeded maximum of {depth}")
45            }
46            Self::UndefinedMacro { key, source, line } => {
47                write!(f, "undefined macro '{key}' in {source} at line {line}")
48            }
49            Self::CorruptSaveFile { path, message } => {
50                write!(f, "corrupt save file '{path}': {message}")
51            }
52            Self::PvNotFound(name) => write!(f, "PV not found: {name}"),
53            Self::Ca(e) => write!(f, "CA error: {e}"),
54        }
55    }
56}
57
58impl std::error::Error for AutosaveError {
59    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
60        match self {
61            Self::Io(e) => Some(e),
62            Self::Ca(e) => Some(e),
63            _ => None,
64        }
65    }
66}
67
68impl From<std::io::Error> for AutosaveError {
69    fn from(e: std::io::Error) -> Self {
70        Self::Io(e)
71    }
72}
73
74impl From<CaError> for AutosaveError {
75    fn from(e: CaError) -> Self {
76        Self::Ca(e)
77    }
78}