async_modbus/
error.rs

1/// Errors that can occur when talking to a Modbus server.
2#[derive(Debug, thiserror_no_std::Error)]
3pub enum Error<Io> {
4    /// IO error.
5    #[error(transparent)]
6    Io(Io),
7    /// Unexpected end of file when reading.
8    #[error("unexpected end of file")]
9    UnexpectedEof,
10    /// Invalid CRC checksum.
11    #[error(transparent)]
12    Crc(#[from] CrcError),
13    /// Unexpected response from the Modbus server.
14    #[error("unexpected response from server")]
15    UnexpectedResponse,
16}
17
18/// Error indicating a CRC validation failure.
19#[derive(Debug, Clone, Copy, thiserror_no_std::Error)]
20#[error("CRC validation failed")]
21pub struct CrcError;
22
23/// Errors that can occur when validating a Modbus response.
24#[derive(Debug, thiserror_no_std::Error)]
25pub enum ValidationError {
26    /// CRC validation failed.
27    #[error(transparent)]
28    Crc(#[from] CrcError),
29    /// The response did not match the request.
30    #[error("unexpected response")]
31    UnexpectedResponse,
32}
33
34impl<E> From<ValidationError> for Error<E> {
35    fn from(e: ValidationError) -> Self {
36        match e {
37            ValidationError::Crc(crc) => Error::Crc(crc),
38            ValidationError::UnexpectedResponse => Error::UnexpectedResponse,
39        }
40    }
41}