regiface/
errors.rs

1//! Error types for register interface operations.
2//!
3//! This module provides a two-tier error handling system:
4//! 1. Specific error types that preserve detailed error information including the underlying error types
5//! 2. A generic [`Error`] type that can represent any error case but forgoes the specific error details
6//!
7//! This approach allows for detailed error handling when needed while also providing a simpler,
8//! unified error type when the specific details aren't required.
9
10/// Error that can occur when reading from a register.
11///
12/// Generic over the bus error type `B` and deserialization error type `D`.
13#[derive(Clone, Copy, Debug)]
14pub enum ReadRegisterError<B, D> {
15    /// An error occurred while communicating over the bus
16    BusError(B),
17    /// An error occurred while deserializing the received data
18    DeserializationError(D),
19}
20
21/// Error that can occur when writing to a register.
22///
23/// Generic over the bus error type `B` and serialization error type `S`.
24#[derive(Clone, Copy, Debug)]
25pub enum WriteRegisterError<B, S> {
26    /// An error occurred while communicating over the bus
27    BusError(B),
28    /// An error occurred while serializing the data to be sent
29    SerializationError(S),
30}
31
32/// Error that can occur when executing a command.
33///
34/// Generic over the bus error type `B`, serialization error type `S`,
35/// and deserialization error type `D`.
36#[derive(Clone, Copy, Debug)]
37pub enum CommandError<B, S, D> {
38    /// An error occurred while communicating over the bus
39    BusError(B),
40    /// An error occurred while serializing the command data
41    SerializationError(S),
42    /// An error occurred while deserializing the command response
43    DeserializationError(D),
44}
45
46/// A simplified error type that represents any error that can occur during register operations.
47///
48/// This type intentionally discards the specific error details in favor of a simpler,
49/// unified error type. Use the specific error types ([`ReadRegisterError`], [`WriteRegisterError`],
50/// [`CommandError`]) when you need access to the underlying error information.
51#[derive(Clone, Copy, Debug)]
52pub enum Error {
53    /// An error occurred while communicating over the bus
54    BusError,
55    /// An error occurred during data serialization
56    SerializationError,
57    /// An error occurred during data deserialization
58    DeserializationError,
59}
60
61impl<B, D> From<ReadRegisterError<B, D>> for Error {
62    fn from(value: ReadRegisterError<B, D>) -> Self {
63        match value {
64            ReadRegisterError::BusError(_) => Self::BusError,
65            ReadRegisterError::DeserializationError(_) => Self::DeserializationError,
66        }
67    }
68}
69
70impl<B, S> From<WriteRegisterError<B, S>> for Error {
71    fn from(value: WriteRegisterError<B, S>) -> Self {
72        match value {
73            WriteRegisterError::BusError(_) => Self::BusError,
74            WriteRegisterError::SerializationError(_) => Self::SerializationError,
75        }
76    }
77}
78
79impl<B, D, S> From<CommandError<B, D, S>> for Error {
80    fn from(value: CommandError<B, D, S>) -> Self {
81        match value {
82            CommandError::BusError(_) => Self::BusError,
83            CommandError::DeserializationError(_) => Self::DeserializationError,
84            CommandError::SerializationError(_) => Self::SerializationError,
85        }
86    }
87}