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    /// A macro whose value resolves back into itself. Distinct from
26    /// [`Self::UndefinedMacro`] because the two faults have different
27    /// causes and different fixes: one name is missing from the
28    /// substitution set, the other is defined in terms of itself.
29    RecursiveMacro {
30        key: String,
31        source: String,
32        line: usize,
33    },
34    /// A `$(`/`${` whose closing delimiter never arrived. Distinct from
35    /// the two above because macLib names no macro for it — it copies
36    /// the reference and the whole rest of the line through verbatim
37    /// (`macCore.c:862-875`) — so what a `.req` author is shown is the
38    /// text that was passed through, not a key to go and define.
39    UnterminatedMacro {
40        reference: String,
41        source: String,
42        line: usize,
43    },
44    CorruptSaveFile {
45        path: String,
46        message: String,
47    },
48    /// The set's member list came out empty. Refused rather than carried,
49    /// because a set with no members still rotates and rewrites the files
50    /// holding the values it was configured to protect.
51    EmptySaveSet {
52        name: String,
53        reason: String,
54    },
55    PvNotFound(String),
56    Ca(CaError),
57}
58
59impl fmt::Display for AutosaveError {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        match self {
62            Self::Io(e) => write!(f, "I/O error: {e}"),
63            Self::RequestFile { path, message } => {
64                write!(f, "request file error in '{path}': {message}")
65            }
66            Self::IncludeCycle { chain } => {
67                write!(f, "include cycle detected: {}", chain.join(" -> "))
68            }
69            Self::IncludeDepthExceeded(depth) => {
70                write!(f, "include depth exceeded maximum of {depth}")
71            }
72            Self::UndefinedMacro { key, source, line } => {
73                write!(f, "undefined macro '{key}' in {source} at line {line}")
74            }
75            Self::RecursiveMacro { key, source, line } => {
76                write!(f, "recursive macro '{key}' in {source} at line {line}")
77            }
78            Self::UnterminatedMacro {
79                reference,
80                source,
81                line,
82            } => {
83                write!(
84                    f,
85                    "unterminated macro reference '{reference}' in {source} at line {line}"
86                )
87            }
88            Self::CorruptSaveFile { path, message } => {
89                write!(f, "corrupt save file '{path}': {message}")
90            }
91            Self::EmptySaveSet { name, reason } => {
92                write!(f, "save set '{name}' has no PVs to save: {reason}")
93            }
94            Self::PvNotFound(name) => write!(f, "PV not found: {name}"),
95            Self::Ca(e) => write!(f, "CA error: {e}"),
96        }
97    }
98}
99
100impl std::error::Error for AutosaveError {
101    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
102        match self {
103            Self::Io(e) => Some(e),
104            Self::Ca(e) => Some(e),
105            _ => None,
106        }
107    }
108}
109
110impl From<std::io::Error> for AutosaveError {
111    fn from(e: std::io::Error) -> Self {
112        Self::Io(e)
113    }
114}
115
116impl From<CaError> for AutosaveError {
117    fn from(e: CaError) -> Self {
118        Self::Ca(e)
119    }
120}