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}
28
29#[cfg(all(feature = "defmt-format", target_os = "none"))]
30impl defmt::Format for TransportError {
31 fn format(&self, f: defmt::Formatter) {
32 match self {
33 TransportError::ConnectionFailed => defmt::write!(f, "Connection failed"),
34 TransportError::ConnectionClosed => defmt::write!(f, "Connection closed"),
35 TransportError::IoError => defmt::write!(f, "I/O error"),
36 TransportError::Timeout => defmt::write!(f, "Timeout"),
37 TransportError::BufferTooSmall => defmt::write!(f, "Buffer too small"),
38 TransportError::Unexpected => defmt::write!(f, "An unexpected error occurred"),
39 TransportError::InvalidConfiguration => defmt::write!(f, "Invalid configuration"),
40 }
41 }
42}
43
44#[cfg(feature = "error-trait")]
45impl core::fmt::Display for TransportError {
46 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
47 match self {
48 TransportError::ConnectionFailed => write!(f, "Connection failed"),
49 TransportError::ConnectionClosed => write!(f, "Connection closed"),
50 TransportError::IoError => write!(f, "I/O error"),
51 TransportError::Timeout => write!(f, "Timeout"),
52 TransportError::BufferTooSmall => write!(f, "Buffer too small"),
53 TransportError::Unexpected => write!(f, "Unexpected error"),
54 TransportError::InvalidConfiguration => write!(f, "Invalid configuration"),
55 }
56 }
57}
58
59#[cfg(feature = "error-trait")]
60impl core::error::Error for TransportError {}
61
62impl From<TransportError> for MbusError {
63 fn from(err: TransportError) -> Self {
64 match err {
65 TransportError::ConnectionFailed => MbusError::ConnectionFailed,
66 TransportError::ConnectionClosed => MbusError::ConnectionClosed,
67 TransportError::IoError => MbusError::IoError,
68 TransportError::Timeout => MbusError::Timeout,
69 TransportError::BufferTooSmall => MbusError::BufferTooSmall,
70 TransportError::Unexpected => MbusError::Unexpected,
71 TransportError::InvalidConfiguration => MbusError::InvalidConfiguration,
72 }
73 }
74}
75
76#[derive(Debug, Clone, Copy, PartialEq, Eq)]
78pub enum TransportType {
79 StdTcp,
81 StdSerial(SerialMode),
83 CustomTcp,
85 CustomSerial(SerialMode),
87}
88
89impl TransportType {
90 pub fn is_tcp_type(&self) -> bool {
92 matches!(self, TransportType::StdTcp | TransportType::CustomTcp)
93 }
94
95 pub fn is_serial_type(&self) -> bool {
97 matches!(
98 self,
99 TransportType::StdSerial(_) | TransportType::CustomSerial(_)
100 )
101 }
102}