blob: 84af6bfd0c06f8e8d91e2298de3a63a121eb0999 [file] [log] [blame]
Woyten190adff2020-03-13 17:04:53 +01001use core::fmt;
2
3pub use libtock_core::result::*;
4
5pub type TockResult<T> = Result<T, TockError>;
6
7#[derive(Copy, Clone)]
8pub 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")))]
17impl 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
23impl From<SubscribeError> for TockError {
24 fn from(subscribe_error: SubscribeError) -> Self {
25 TockError::Subscribe(subscribe_error)
26 }
27}
28
29impl From<CommandError> for TockError {
30 fn from(command_error: CommandError) -> Self {
31 TockError::Command(command_error)
32 }
33}
34
35impl From<AllowError> for TockError {
36 fn from(allow_error: AllowError) -> Self {
37 TockError::Allow(allow_error)
38 }
39}
40
41impl From<fmt::Error> for TockError {
42 fn from(fmt::Error: fmt::Error) -> Self {
43 TockError::Format
44 }
45}
46
47#[derive(Copy, Clone)]
48pub enum OtherError {
49 ButtonsDriverInvalidState,
50 GpioDriverInvalidState,
51 TimerDriverDurationOutOfRange,
52 TimerDriverErroneousClockFrequency,
Woytence2bc6a2020-01-19 00:33:30 +010053 DriversAlreadyTaken,
54 OutOfRange,
Woyten190adff2020-03-13 17:04:53 +010055}
56
57impl From<OtherError> for TockError {
58 fn from(other: OtherError) -> Self {
59 TockError::Other(other)
60 }
61}
62
63pub struct OutOfRangeError;
64
65impl From<OutOfRangeError> for TockError {
Woytence2bc6a2020-01-19 00:33:30 +010066 fn from(_: OutOfRangeError) -> Self {
67 TockError::Other(OtherError::OutOfRange)
Woyten190adff2020-03-13 17:04:53 +010068 }
69}