Skip to main content

mbus_core/transport/
error.rs

1//! Transport-layer error and type-classification types.
2
3use core::fmt;
4
5use crate::errors::MbusError;
6
7use super::config::SerialMode;
8
9/// Represents errors that can occur at the Modbus transport layer.
10#[derive(Debug, PartialEq, Eq)]
11pub enum TransportError {
12    /// The connection attempt failed.
13    ConnectionFailed,
14    /// The connection was unexpectedly closed.
15    ConnectionClosed,
16    /// An I/O error occurred during send or receive.
17    IoError,
18    /// A timeout occurred during a network operation.
19    Timeout,
20    /// The received data was too large for the buffer.
21    BufferTooSmall,
22    /// An unexpected error occurred.
23    Unexpected,
24    /// Invalid configuration.
25    InvalidConfiguration,
26}
27
28impl fmt::Display for TransportError {
29    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30        match self {
31            TransportError::ConnectionFailed => write!(f, "Connection failed"),
32            TransportError::ConnectionClosed => write!(f, "Connection closed"),
33            TransportError::IoError => write!(f, "I/O error"),
34            TransportError::Timeout => write!(f, "Timeout"),
35            TransportError::BufferTooSmall => write!(f, "Buffer too small"),
36            TransportError::Unexpected => write!(f, "An unexpected error occurred"),
37            TransportError::InvalidConfiguration => write!(f, "Invalid configuration"),
38        }
39    }
40}
41
42impl core::error::Error for TransportError {}
43
44impl From<TransportError> for MbusError {
45    fn from(err: TransportError) -> Self {
46        match err {
47            TransportError::ConnectionFailed => MbusError::ConnectionFailed,
48            TransportError::ConnectionClosed => MbusError::ConnectionClosed,
49            TransportError::IoError => MbusError::IoError,
50            TransportError::Timeout => MbusError::Timeout,
51            TransportError::BufferTooSmall => MbusError::BufferTooSmall,
52            TransportError::Unexpected => MbusError::Unexpected,
53            TransportError::InvalidConfiguration => MbusError::InvalidConfiguration,
54        }
55    }
56}
57
58/// An enumeration to specify the type of transport to use.
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub enum TransportType {
61    /// Standard library TCP transport implementation.
62    StdTcp,
63    /// Standard library Serial transport implementation.
64    StdSerial(SerialMode),
65    /// Custom TCP transport implementation.
66    CustomTcp,
67    /// Custom Serial transport implementation.
68    CustomSerial(SerialMode),
69}
70
71impl TransportType {
72    /// Returns `true` if the transport type is TCP (StdTcp or CustomTcp).
73    pub fn is_tcp_type(&self) -> bool {
74        matches!(self, TransportType::StdTcp | TransportType::CustomTcp)
75    }
76
77    /// Returns `true` if the transport type is serial (RTU or ASCII).
78    pub fn is_serial_type(&self) -> bool {
79        matches!(
80            self,
81            TransportType::StdSerial(_) | TransportType::CustomSerial(_)
82        )
83    }
84}