Woyten | 190adff | 2020-03-13 17:04:53 +0100 | [diff] [blame] | 1 | use core::fmt; |
| 2 | |
| 3 | pub use libtock_core::result::*; |
| 4 | |
| 5 | pub type TockResult<T> = Result<T, TockError>; |
| 6 | |
| 7 | #[derive(Copy, Clone)] |
| 8 | pub enum TockError { |
| 9 | Subscribe(SubscribeError), |
| 10 | Command(CommandError), |
| 11 | Allow(AllowError), |
| 12 | Format, |
| 13 | Other(OtherError), |
| 14 | } |
| 15 | |
| 16 | #[cfg(not(any(target_arch = "arm", target_arch = "riscv32")))] |
| 17 | impl core::fmt::Debug for TockError { |
| 18 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 19 | writeln!(f, "impl Debug only for test builds") |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | impl From<SubscribeError> for TockError { |
| 24 | fn from(subscribe_error: SubscribeError) -> Self { |
| 25 | TockError::Subscribe(subscribe_error) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | impl From<CommandError> for TockError { |
| 30 | fn from(command_error: CommandError) -> Self { |
| 31 | TockError::Command(command_error) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl From<AllowError> for TockError { |
| 36 | fn from(allow_error: AllowError) -> Self { |
| 37 | TockError::Allow(allow_error) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl From<fmt::Error> for TockError { |
| 42 | fn from(fmt::Error: fmt::Error) -> Self { |
| 43 | TockError::Format |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | #[derive(Copy, Clone)] |
| 48 | pub enum OtherError { |
| 49 | ButtonsDriverInvalidState, |
| 50 | GpioDriverInvalidState, |
| 51 | TimerDriverDurationOutOfRange, |
| 52 | TimerDriverErroneousClockFrequency, |
Woyten | ce2bc6a | 2020-01-19 00:33:30 +0100 | [diff] [blame] | 53 | DriversAlreadyTaken, |
| 54 | OutOfRange, |
Woyten | 190adff | 2020-03-13 17:04:53 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | impl From<OtherError> for TockError { |
| 58 | fn from(other: OtherError) -> Self { |
| 59 | TockError::Other(other) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | pub struct OutOfRangeError; |
| 64 | |
| 65 | impl From<OutOfRangeError> for TockError { |
Woyten | ce2bc6a | 2020-01-19 00:33:30 +0100 | [diff] [blame] | 66 | fn from(_: OutOfRangeError) -> Self { |
| 67 | TockError::Other(OtherError::OutOfRange) |
Woyten | 190adff | 2020-03-13 17:04:53 +0100 | [diff] [blame] | 68 | } |
| 69 | } |