dbc_parse/error.rs
1//! Typed, exhaustive error taxonomy for every operation in the crate.
2//!
3//! The enum is deliberately **exhaustive** (no `#[non_exhaustive]`): callers
4//! are expected to match every variant, and a new variant is a semver-minor
5//! event by design — the same policy as `wire-kit`'s `WireError`. The parser
6//! never panics on malformed input: every failure resolves to one of these
7//! variants.
8
9use alloc::string::String;
10use core::fmt;
11
12/// The single crate-level error for parsing and signal coding.
13#[derive(Debug, Clone, PartialEq)]
14pub enum DbcError {
15 /// A grammar violation in the DBC text. `line` is the 1-based line the
16 /// violation was detected on (approximate for multi-line constructs);
17 /// `message` describes what was expected.
18 Syntax {
19 /// 1-based source line of the violation.
20 line: usize,
21 /// What was expected versus what was found.
22 message: String,
23 },
24 /// A `BO_` identifier is not representable: DBC marks extended frames
25 /// with bit 31 (`0x8000_0000`) and caps the payload at 29 bits; plain
26 /// identifiers must fit the 11-bit standard width.
27 InvalidId {
28 /// The offending raw id word from the file.
29 raw: u32,
30 /// Largest value the identifier width permits.
31 max: u32,
32 },
33 /// A physical value lies outside the signal's declared `[min|max]`
34 /// range (or is not finite — NaN and infinities are always rejected).
35 ValueOutOfRange {
36 /// The offending physical value.
37 value: f64,
38 /// Declared minimum.
39 min: f64,
40 /// Declared maximum.
41 max: f64,
42 },
43 /// The scaled raw value does not fit the signal's bit width and
44 /// signedness.
45 RawOutOfRange {
46 /// The computed raw value (pre-truncation).
47 raw: f64,
48 /// The signal's bit length.
49 bit_length: u8,
50 },
51 /// The buffer is shorter than the signal layout requires.
52 BufferTooShort {
53 /// Number of bytes the layout requires.
54 need: usize,
55 /// Number of bytes actually available.
56 have: usize,
57 },
58}
59
60impl fmt::Display for DbcError {
61 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62 match self {
63 Self::Syntax { line, message } => {
64 write!(f, "DBC syntax error at line {line}: {message}")
65 }
66 Self::InvalidId { raw, max } => {
67 write!(
68 f,
69 "DBC message id 0x{raw:X} exceeds its width limit 0x{max:X}"
70 )
71 }
72 Self::ValueOutOfRange { value, min, max } => {
73 write!(
74 f,
75 "physical value {value} outside the declared range [{min}|{max}]"
76 )
77 }
78 Self::RawOutOfRange { raw, bit_length } => {
79 write!(f, "raw value {raw} does not fit a {bit_length}-bit signal")
80 }
81 Self::BufferTooShort { need, have } => {
82 write!(f, "buffer of {have} bytes is too short: need {need}")
83 }
84 }
85 }
86}
87
88impl core::error::Error for DbcError {}