epics_base_rs/server/autosave/
error.rs1use std::fmt;
2
3use crate::error::CaError;
4
5pub type AutosaveResult<T> = Result<T, AutosaveError>;
7
8#[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 RecursiveMacro {
30 key: String,
31 source: String,
32 line: usize,
33 },
34 UnterminatedMacro {
40 reference: String,
41 source: String,
42 line: usize,
43 },
44 CorruptSaveFile {
45 path: String,
46 message: String,
47 },
48 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}