max6675_hal/
error.rs

1/// Some problem with the MAX6675 or its connections
2#[derive(Copy, Clone, PartialEq, PartialOrd)]
3pub enum Max6675Error<SpiError> {
4    SpiError(SpiError),
5    OpenCircuitError,
6}
7
8impl<SpiError> Max6675Error<SpiError> {
9    fn message(&self) -> &'static str {
10        match *self {
11            Max6675Error::SpiError(_) => {
12                "An error occured while attempting to reach the MAX6675 over SPI."
13            }
14            Max6675Error::OpenCircuitError => {
15                "The MAX6675 detected an open circuit (bit D2 was high). \
16                Please check the thermocouple connection and try again."
17            }
18        }
19    }
20}
21
22// implicit `?` syntax for SpiError to Max6675Error
23impl<SpiError> core::convert::From<SpiError> for Max6675Error<SpiError> {
24    fn from(value: SpiError) -> Self {
25        Max6675Error::SpiError(value)
26    }
27}
28
29// print... if you must
30impl<SpiError> core::fmt::Display for Max6675Error<SpiError> {
31    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
32        write!(f, "{}", self.message())
33    }
34}
35
36impl<SpiError> ufmt::uDisplay for Max6675Error<SpiError> {
37    fn fmt<W>(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
38    where
39        W: ufmt::uWrite + ?Sized,
40    {
41        f.write_str(self.message())
42    }
43}
44
45// debug impls
46impl<SpiError> core::fmt::Debug for Max6675Error<SpiError> {
47    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
48        write!(f, "{}", self.message())
49    }
50}
51
52impl<SpiError> ufmt::uDebug for Max6675Error<SpiError> {
53    fn fmt<W>(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
54    where
55        W: ufmt::uWrite + ?Sized,
56    {
57        f.write_str(self.message())
58    }
59}
60
61// implement error if it's feasible
62// FIXME: use core::error::Error once stable! <3
63#[cfg(feature = "std")]
64impl<SpiError: std::fmt::Debug> std::error::Error for Max6675Error<SpiError> {
65    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
66        None // other types typically don't impl error :p
67    }
68
69    fn description(&self) -> &str {
70        "error description is deprecated. use display instead!"
71    }
72
73    fn cause(&self) -> Option<&dyn std::error::Error> {
74        self.source() // (none)
75    }
76}