waveshare_ups_hat_e/
error.rs

1// Copyright (c) 2025 Stuart Stock
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use i2cdev::linux::LinuxI2CError;
5use std::fmt;
6
7#[derive(Debug)]
8pub enum Error {
9    I2CError(LinuxI2CError),
10    InvalidDataLen(u8, usize, usize),
11    InvalidChargerActivity(u8),
12}
13
14impl fmt::Display for Error {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        match self {
17            Error::I2CError(e) => write!(f, "I2C error: {e}"),
18            Error::InvalidDataLen(reg, expected, got) => {
19                write!(
20                    f,
21                    "Reading register {reg} invalid data length: expected {expected}, got {got}"
22                )
23            }
24            Error::InvalidChargerActivity(val) => {
25                write!(f, "Invalid charger activity value: {val}")
26            }
27        }
28    }
29}
30
31impl std::error::Error for Error {
32    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
33        match self {
34            Error::I2CError(e) => Some(e),
35            Error::InvalidDataLen(..) | Error::InvalidChargerActivity(_) => None,
36        }
37    }
38}
39
40impl From<LinuxI2CError> for Error {
41    fn from(err: LinuxI2CError) -> Self {
42        Error::I2CError(err)
43    }
44}