mbus_core/transport/
error.rs1#[cfg(all(feature = "defmt-format", target_os = "none"))]
4use defmt;
5
6use crate::errors::MbusError;
7
8use super::config::SerialMode;
9
10#[derive(Debug, PartialEq, Eq)]
12pub enum TransportError {
13 ConnectionFailed,
15 ConnectionClosed,
17 IoError,
19 Timeout,
21 BufferTooSmall,
23 Unexpected,
25 InvalidConfiguration,
27 FramingError,
30}
31
32#[cfg(all(feature = "defmt-format", target_os = "none"))]
33impl defmt::Format for TransportError {
34 fn format(&self, f: defmt::Formatter) {
35 match self {
36 TransportError::ConnectionFailed => defmt::write!(f, "Connection failed"),
37 TransportError::ConnectionClosed => defmt::write!(f, "Connection closed"),
38 TransportError::IoError => defmt::write!(f, "I/O error"),
39 TransportError::Timeout => defmt::write!(f, "Timeout"),
40 TransportError::BufferTooSmall => defmt::write!(f, "Buffer too small"),
41 TransportError::Unexpected => defmt::write!(f, "An unexpected error occurred"),
42 TransportError::InvalidConfiguration => defmt::write!(f, "Invalid configuration"),
43 TransportError::FramingError => {
44 defmt::write!(f, "Framing error: protocol timing violation")
45 }
46 }
47 }
48}
49
50#[cfg(feature = "error-trait")]
51impl core::fmt::Display for TransportError {
52 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
53 match self {
54 TransportError::ConnectionFailed => write!(f, "Connection failed"),
55 TransportError::ConnectionClosed => write!(f, "Connection closed"),
56 TransportError::IoError => write!(f, "I/O error"),
57 TransportError::Timeout => write!(f, "Timeout"),
58 TransportError::BufferTooSmall => write!(f, "Buffer too small"),
59 TransportError::Unexpected => write!(f, "Unexpected error"),
60 TransportError::InvalidConfiguration => write!(f, "Invalid configuration"),
61 TransportError::FramingError => write!(f, "Framing error"),
62 }
63 }
64}
65
66#[cfg(feature = "error-trait")]
67impl core::error::Error for TransportError {}
68
69impl From<TransportError> for MbusError {
70 fn from(err: TransportError) -> Self {
71 match err {
72 TransportError::ConnectionFailed => MbusError::ConnectionFailed,
73 TransportError::ConnectionClosed => MbusError::ConnectionClosed,
74 TransportError::IoError => MbusError::IoError,
75 TransportError::Timeout => MbusError::Timeout,
76 TransportError::BufferTooSmall => MbusError::BufferTooSmall,
77 TransportError::Unexpected => MbusError::Unexpected,
78 TransportError::InvalidConfiguration => MbusError::InvalidConfiguration,
79 TransportError::FramingError => MbusError::FramingError,
80 }
81 }
82}
83
84#[derive(Debug, Clone, Copy, PartialEq, Eq)]
86pub enum TransportType {
87 StdTcp,
89 StdSerial(SerialMode),
91 CustomTcp,
93 CustomSerial(SerialMode),
95}
96
97impl TransportType {
98 pub fn is_tcp_type(&self) -> bool {
100 matches!(self, TransportType::StdTcp | TransportType::CustomTcp)
101 }
102
103 pub fn is_serial_type(&self) -> bool {
105 matches!(
106 self,
107 TransportType::StdSerial(_) | TransportType::CustomSerial(_)
108 )
109 }
110}