doip_definitions/
error.rs

1use derive_more::From;
2
3/// Custom Result type which allows for easier typing of errors across the API.
4pub type Result<T> = core::result::Result<T, Error>;
5
6/// Custom Error enum for deriving error types across the application and API.
7///
8/// Expand to fit new dependencies using `#[from]` and implement custom error types
9/// with context.
10#[derive(Debug, From)]
11pub enum Error {
12    /// When utilising the builder pattern the payload is not present
13    PayloadNotBuilt,
14    /// When utilising the builder pattern the header is not present
15    HeaderNotBuilt,
16
17    /// Errors when accessing a range out of the slices scope.
18    OutOfBounds {
19        /// Source struct
20        source: &'static str,
21
22        /// Destination variable
23        variable: &'static str,
24    },
25
26    /// When a slice is too small to be written to
27    SliceTooSmall,
28
29    /// Invalid `ProtocolVersion`
30    InvalidProtocolVersion {
31        /// Value
32        value: u8,
33    },
34
35    /// Invalid `NackCode`
36    InvalidNackCode {
37        /// Value
38        value: u8,
39    },
40
41    /// Invalid `ActionCode`
42    InvalidActionCode {
43        /// Value
44        value: u8,
45    },
46
47    /// Invalid `SyncStatus`
48    InvalidSyncStatus {
49        /// Value
50        value: u8,
51    },
52
53    /// Invalid `ActivationCode`
54    InvalidActivationCode {
55        /// Value
56        value: u8,
57    },
58
59    /// Invalid `ActivationType`
60    InvalidActivationType {
61        /// Value
62        value: u8,
63    },
64
65    /// Invalid `NodeType`
66    InvalidNodeType {
67        /// Value
68        value: u8,
69    },
70
71    /// Invalid `PowerMode`
72    InvalidPowerMode {
73        /// Value
74        value: u8,
75    },
76
77    /// Invalid `DiagnosticNackCode`
78    InvalidDiagnosticNackCode {
79        /// Value
80        value: u8,
81    },
82
83    /// Invalid `DiagnosticAckCode`
84    InvalidDiagnosticAckCode {
85        /// Value
86        value: u8,
87    },
88
89    /// Invalid `PayloadType`
90    InvalidPayloadType {
91        /// Value
92        value: [u8; 2],
93    },
94
95    /// When the destination buffer is too small
96    BufferTooSmall {
97        /// Size of the buffer
98        size: usize,
99    },
100
101    /// Derived implementation for standard library IO errors
102    #[from]
103    #[allow(clippy::enum_variant_names)]
104    SliceError(core::array::TryFromSliceError),
105}
106
107impl core::fmt::Display for Error {
108    fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
109        write!(fmt, "{self:?}")
110    }
111}