rusty_modbus_frame/error.rs
1//! Frame-layer error types.
2
3use rusty_modbus_codec::DecodeError;
4
5/// Errors that can occur during frame-level decode/encode.
6#[derive(Debug, thiserror::Error)]
7pub enum FrameError {
8 /// MBAP protocol identifier is not 0x0000.
9 #[error("invalid MBAP protocol identifier: {0:#06X}")]
10 InvalidProtocolId(u16),
11
12 /// MBAP length field exceeds maximum PDU size.
13 #[error("MBAP length overflow: {0}")]
14 LengthOverflow(u16),
15
16 /// MBAP length field cannot contain the Unit Identifier plus a function code.
17 #[error("invalid MBAP length: {declared} (minimum {minimum})")]
18 InvalidLength {
19 /// Declared MBAP length field value.
20 declared: u16,
21 /// Minimum valid MBAP length field value.
22 minimum: u16,
23 },
24
25 /// MBAP length field does not match the encoded payload size.
26 #[error("MBAP length mismatch: declared {declared}, actual {actual}")]
27 LengthMismatch {
28 /// Declared MBAP length field value.
29 declared: u16,
30 /// Actual length field value implied by the Unit Identifier and PDU bytes.
31 actual: u16,
32 },
33
34 /// PDU payload is too short to contain the function code.
35 #[error("invalid PDU length: {length} (minimum {minimum})")]
36 InvalidPduLength {
37 /// Actual PDU length in bytes.
38 length: usize,
39 /// Minimum valid PDU length in bytes.
40 minimum: usize,
41 },
42
43 /// PDU payload exceeds the Modbus maximum size.
44 #[error("PDU length overflow: {length} (maximum {maximum})")]
45 PduLengthOverflow {
46 /// Actual PDU length in bytes.
47 length: usize,
48 /// Maximum valid PDU length in bytes.
49 maximum: usize,
50 },
51
52 /// RTU CRC-16 mismatch.
53 #[error("CRC mismatch: expected {expected:#06X}, got {actual:#06X}")]
54 CrcMismatch {
55 /// Expected CRC value.
56 expected: u16,
57 /// Actual CRC value from the frame.
58 actual: u16,
59 },
60
61 /// Frame is truncated (not enough bytes).
62 #[error("frame truncated")]
63 Truncated,
64
65 /// Underlying I/O error.
66 #[error("I/O error: {0}")]
67 Io(#[from] std::io::Error),
68
69 /// PDU decode error.
70 #[error("decode error: {0}")]
71 Decode(#[from] DecodeError),
72}