Skip to main content

divecomputer_core/
error.rs

1//! The error type shared by transports and protocols.
2
3use alloc::string::String;
4
5/// A failure while talking to a dive computer or decoding what it sent.
6///
7/// The variants separate the cases a host reacts to differently: retry after a
8/// [`Timeout`](Self::Timeout), reconnect after [`Disconnected`](Self::Disconnected), report
9/// a [`Protocol`](Self::Protocol) violation or a [`Rejected`](Self::Rejected) command.
10#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
11#[non_exhaustive]
12pub enum Error {
13    /// The device did not answer within the timeout.
14    #[error("timed out waiting for the device")]
15    Timeout,
16    /// The connection to the device was closed.
17    #[error("the connection to the device was closed")]
18    Disconnected,
19    /// The transport backend failed; the message comes from the backend.
20    #[error("transport error: {0}")]
21    Transport(String),
22    /// The device sent data that does not follow the protocol. The message names the
23    /// field or offset and what was expected.
24    #[error("protocol violation: {0}")]
25    Protocol(String),
26    /// The device answered a command with a negative response.
27    #[error("device rejected command {command:#04x} with code {code:#04x}")]
28    Rejected {
29        /// The command the device rejected.
30        command: u8,
31        /// The device's error code.
32        code: u8,
33    },
34    /// The device or the requested operation is not supported.
35    #[error("unsupported: {0}")]
36    Unsupported(&'static str),
37}
38
39/// Result type with [`Error`] as the error.
40pub type Result<T, E = Error> = core::result::Result<T, E>;