1use crate::core::FieldKind;
5use thiserror_no_std::Error;
6
7#[derive(Error, Debug)]
8pub enum CanIdBuildError {
10 #[error("Invalid data")]
12 InvalidData,
13
14 #[error("Invalid destination")]
16 InvalidDestination,
17
18 #[error("Invalid for broadcast message: PF is too low")]
20 InvalidForBroadcast,
21
22 #[error("Invalid for addressed message: PF is too high: {pgn}")]
24 InvalidForFocusedMessage { pgn: u8 },
25
26 #[error("PDU1 PGNs require PS = 0")]
28 PsFocusMessageMustBeNull,
29
30 #[error("Payload is empty: unable to build")]
32 EmptyPayload,
33}
34
35#[derive(Error, Debug)]
36pub enum ClaimError<E: core::fmt::Debug> {
38 #[error("CAN bus send error: {0:?}")]
40 SendError(E),
41
42 #[error("CAN bus receive error: {0:?}")]
44 ReceiveError(E),
45
46 #[error(transparent)]
48 Fault(#[from] ClaimFault),
49}
50
51#[derive(Error, Debug)]
52pub enum ClaimFault {
54 #[error("Inconsistent match between IsoName and Address Claim Strategy")]
56 InconsistentStrategy,
57
58 #[error("Request address claim error")]
60 RequestAddressClaimErr,
61
62 #[error(transparent)]
64 Extraction(#[from] ExtractionError),
65}
66
67#[derive(Debug, Error)]
68pub enum ExtractionError {
70 #[error("Invalid incoming N2K frame")]
72 InvalidIncomingFrame,
73
74 #[error("Invalid data length for PGN")]
76 InvalidDataLen,
77}
78
79#[derive(Debug, Error)]
81pub enum SerializationError {
83 #[error("Buffer too small")]
85 BufferTooSmall,
86
87 #[error("Invalid data")]
89 InvalidData,
90
91 #[error("Invalid repetitive PGN definition for {data}")]
93 RepeatitiveError { data: u32 },
94
95 #[error("Invalid field bit length for {field_name}")]
97 InvalidFieldBits { field_name: &'static str },
98
99 #[error("BitWrite error: {err}")]
101 BitWriteError { err: BitWriterError },
102
103 #[error("Unsupported field kind")]
105 UnsupportedFieldKind,
106
107 #[error("Field {field_id} not found")]
109 FieldNotFound { field_id: &'static str },
110
111 #[error("Codec Error: {source}")]
113 CodecError { source: CodecError },
114}
115
116#[derive(Error, Debug)]
117pub enum DeserializationError {
119 #[error("Invalid data length")]
121 InvalidDataLength,
122
123 #[error("Malformed data")]
125 MalformedData,
126
127 #[error("Functionality not implemented for this PGN")]
129 NotImplemented,
130
131 #[error("Missing Indirect Lookup Reference for descriptor {desc}: {pgn}")]
133 MissingIndirectLookupRef { desc: u32, pgn: &'static str },
134
135 #[error("Dependency field not found {dep} for pgn {desc}")]
137 DependencyFieldNotFound { dep: &'static str, desc: u32 },
138
139 #[error("Unsupported field kind {field_kind:?}")]
141 UnsupportedFieldKind { field_kind: FieldKind },
142
143 #[error("Field assignment failed {desc}")]
145 FieldAssignmentFailed { desc: &'static str },
146
147 #[error("Invalid field bit length for {field_name}")]
149 InvalidFieldBits { field_name: &'static str },
150
151 #[error("Codec Error: {source}")]
153 CodecError { source: CodecError },
154
155 #[error("BitReader error: {err}")]
157 BitReaderError { err: BitReaderError },
158}
159
160#[derive(Error, Debug)]
161pub enum CodecError {
163 #[error("Data type mismatch for {value_kind}, function: {func}")]
165 DataTypeMismatch {
166 value_kind: &'static str,
167 func: &'static str,
168 },
169}
170
171#[derive(Debug, Error)]
173pub enum SendPgnError<E: core::fmt::Debug> {
175 #[error("Serialization failed")]
177 Serialization,
178
179 #[error("Frame build failed: {0:?}")]
181 Build(CanIdBuildError),
182
183 #[error("CAN bus send error: {0:?}")]
185 Send(E),
186
187 #[error("No address claimed")]
189 NotClaimed,
190}
191
192#[derive(Debug, Error)]
194pub enum BitReaderError {
196 #[error("Attempted to read out of bounds -> asked: {asked}, available: {available}")]
198 OutOfBounds { asked: usize, available: usize },
199
200 #[error("Cannot read more than {max} bits. Requested: {asked}")]
202 TooLongForType { max: u8, asked: u8 },
203
204 #[error("Non aligned bit. Cursor: {cursor}")]
206 NonAlignedBit { cursor: usize },
207}
208#[derive(Debug, Error)]
210pub enum BitWriterError {
212 #[error("Attempted to write out of bounds -> asked: {asked}, available: {available}")]
214 OutOfBounds { asked: usize, available: usize },
215
216 #[error("Cannot write more than {max} bits. Requested: {asked}")]
218 TooLongForType { max: u8, asked: u8 },
219
220 #[error("Non aligned bit. Cursor: {cursor}")]
222 NonAlignedBit { cursor: usize },
223}
224
225const _: () = assert!(core::mem::size_of::<DeserializationError>() <= 48);
229const _: () = assert!(core::mem::size_of::<SerializationError>() <= 48);