mini_oled/error.rs
1//! # Error
2//!
3//! This module defines the errors that can occur when using the library.
4//!
5//! ## Example
6//!
7//! Handling errors from the library.
8//!
9//! ```rust
10//! use mini_oled::error::MiniOledError;
11//!
12//! fn check_error(result: Result<(), MiniOledError>) {
13//! match result {
14//! Ok(_) => {},
15//! Err(MiniOledError::CommandBufferSizeError) => {
16//! // Handle command buffer overflow
17//! },
18//! Err(MiniOledError::DataBufferSizeError) => {
19//! // Handle data buffer overflow
20//! },
21//! Err(MiniOledError::I2cError(_)) => {
22//! // Handle I2C communication error
23//! },
24//! Err(MiniOledError::SpiBusError(_)) => {
25//! // Handle SPI communication error
26//! },
27//! }
28//! }
29//! ```
30
31use core::{
32 error::Error,
33 fmt::{self, Display},
34};
35
36use embedded_hal::{i2c, spi};
37
38#[derive(Debug)]
39pub enum MiniOledError {
40 /// Error when the command buffer size is exceeded.
41 CommandBufferSizeError,
42 /// Error when the data buffer size is exceeded.
43 DataBufferSizeError,
44 /// Error wrapping an I2C communication error.
45 I2cError(i2c::ErrorKind),
46 /// Error wrapping an SPI communication error.
47 SpiBusError(spi::ErrorKind),
48}
49
50impl Display for MiniOledError {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 match self {
53 MiniOledError::CommandBufferSizeError => {
54 write!(f, "Mini Oled Library Error: Command Buffer Size Exceeded")
55 }
56 MiniOledError::DataBufferSizeError => {
57 write!(f, "Mini Oled Library Error: Data Buffer Size Exceeded")
58 }
59 MiniOledError::I2cError(error_kind) => {
60 write!(f, "Embedded Hal I2C Error: {}", error_kind)
61 }
62 MiniOledError::SpiBusError(error_kind) => {
63 write!(f, "Embedded Hal Spi Bus Error: {}", error_kind)
64 }
65 }
66 }
67}
68
69impl Error for MiniOledError {}