modbus_rtu/error/
mod.rs

1//! modbus-rtu crate error types
2
3mod request_packet;
4pub use request_packet::*;
5
6mod response_packet;
7pub use response_packet::*;
8
9use crate::Exception;
10
11
12/// Error type that captures failures encountered when talking to a
13/// Modbus RTU device.
14#[derive(Debug)]
15pub enum Error {
16    /// The slave device replied with a Modbus exception response.
17    Exception(Exception),
18
19    /// The request packet could not be constructed; see [`RequestPacketError`]
20    /// for details.
21    Request(RequestPacketError),
22
23    /// The response packet failed validation or decoding; see
24    /// [`ResponsePacketError`] for the specific cause.
25    Response(ResponsePacketError),
26
27    /// Any I/O error surfaced by the underlying serial transport.
28    IO(std::io::Error),
29}
30
31
32impl core::fmt::Display for Error {
33    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
34        match self {
35            Error::Exception(exception) => write!(f, "device responsed {exception}"),
36            Error::Request(request_packet_error) => write!(f, "{request_packet_error}"),
37            Error::Response(response_packet_error) => write!(f, "{response_packet_error}"),
38            Error::IO(error) => write!(f, "{error}"),
39        }
40    }
41}
42
43
44impl core::error::Error for Error {}