Skip to main content

epics_base_rs/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum CaError {
5    #[error("I/O error: {0}")]
6    Io(#[from] std::io::Error),
7
8    #[error("timeout waiting for response")]
9    Timeout,
10
11    #[error("channel not found: {0}")]
12    ChannelNotFound(String),
13
14    #[error("protocol error: {0}")]
15    Protocol(String),
16
17    #[error("unsupported DBR type: {0}")]
18    UnsupportedType(u16),
19
20    #[error("write failed: ECA status {0:#06x}")]
21    WriteFailed(u32),
22
23    #[error("field not found: {0}")]
24    FieldNotFound(String),
25
26    #[error("field is read-only: {0}")]
27    ReadOnlyField(String),
28
29    #[error("type mismatch for field {0}")]
30    TypeMismatch(String),
31
32    #[error("invalid value: {0}")]
33    InvalidValue(String),
34
35    #[error("put disabled (DISP=1) for field {0}")]
36    PutDisabled(String),
37
38    #[error("link error: {0}")]
39    LinkError(String),
40
41    #[error("DB parse error at line {line}, column {column}: {message}")]
42    DbParseError {
43        line: usize,
44        column: usize,
45        message: String,
46    },
47
48    #[error("calc error: {0}")]
49    CalcError(String),
50
51    #[error("channel disconnected")]
52    Disconnected,
53
54    #[error("client shut down")]
55    Shutdown,
56}
57
58impl CaError {
59    pub fn to_eca_status(&self) -> u32 {
60        match self {
61            CaError::Timeout => crate::protocol::ECA_TIMEOUT,
62            CaError::ReadOnlyField(_) => crate::protocol::ECA_NOWTACCESS,
63            CaError::PutDisabled(_) => crate::protocol::ECA_PUTFAIL,
64            CaError::TypeMismatch(_) => crate::protocol::ECA_BADTYPE,
65            CaError::UnsupportedType(_) => crate::protocol::ECA_BADTYPE,
66            CaError::InvalidValue(_) => crate::protocol::ECA_BADTYPE,
67            CaError::FieldNotFound(_) => crate::protocol::ECA_PUTFAIL,
68            _ => crate::protocol::ECA_PUTFAIL,
69        }
70    }
71}
72
73pub type CaResult<T> = Result<T, CaError>;