hc_sr04/error.rs
1// hc-sr04: Raspberry Pi Rust driver for the HC-SR04 ultrasonic distance sensor.
2// Copyright (C) 2022 Marco Radocchia
3//
4// This program is free software: you can redistribute it and/or modify it under
5// the terms of the GNU General Public License as published by the Free Software
6// Foundation, either version 3 of the License, or (at your option) any later
7// version.
8//
9// This program is distributed in the hope that it will be useful, but WITHOUT
10// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12// details.
13//
14// You should have received a copy of the GNU General Public License along with
15// this program. If not, see https://www.gnu.org/licenses/.
16
17use rppal::gpio;
18use std::{
19 self,
20 fmt::{Display, Formatter, Result},
21};
22
23#[derive(Debug)]
24/// HC-SR04 runtime errors.
25pub enum Error {
26 /// Occurs on Raspberry Pi GPIO error.
27 Gpio(gpio::Error),
28}
29
30impl Display for Error {
31 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
32 match self {
33 Self::Gpio(error) => write!(f, "GPIO error: {}", error),
34 }
35 }
36}
37
38impl std::error::Error for Error {}
39
40impl From<gpio::Error> for Error {
41 fn from(error: gpio::Error) -> Self {
42 Self::Gpio(error)
43 }
44}