dbc_rs/error/
mod.rs

1mod helpers;
2mod impls;
3mod lang;
4
5pub(crate) use helpers::{check_max_limit, map_val_error, map_val_error_with_line};
6
7/// Error type for DBC operations.
8///
9/// This enum represents all possible errors that can occur when working with DBC files.
10/// Parsing errors include line number information to help locate issues in the source file.
11#[derive(Debug, PartialEq, Clone)]
12pub enum Error {
13    /// Unexpected end of input encountered.
14    UnexpectedEof { line: Option<usize> },
15    /// Expected a specific token or value.
16    Expected {
17        msg: &'static str,
18        line: Option<usize>,
19    },
20    /// Invalid character encountered.
21    InvalidChar { char: char, line: Option<usize> },
22    /// String length exceeds the maximum allowed length.
23    MaxStrLength { max: usize, line: Option<usize> },
24    /// Version-related error.
25    Version {
26        msg: &'static str,
27        line: Option<usize>,
28    },
29    /// Message-related error.
30    Message {
31        msg: &'static str,
32        line: Option<usize>,
33    },
34    /// Receivers-related error.
35    Receivers {
36        msg: &'static str,
37        line: Option<usize>,
38    },
39    /// Nodes-related error.
40    Nodes {
41        msg: &'static str,
42        line: Option<usize>,
43    },
44    /// Signal-related error.
45    Signal {
46        msg: &'static str,
47        line: Option<usize>,
48    },
49    /// Decoding-related error (runtime, no line info).
50    Decoding(&'static str),
51    /// Encoding-related error (runtime, no line info).
52    Encoding(&'static str),
53    /// Validation-related error (post-parse, no line info).
54    Validation(&'static str),
55    /// I/O error (std-only, for file operations).
56    #[cfg(feature = "std")]
57    Io(String),
58}
59
60impl Error {
61    // ============================================================================
62    // Error message constants
63    // ============================================================================
64
65    pub const UNEXPECTED_EOF: &'static str = lang::UNEXPECTED_EOF;
66    pub const EXPECTED_WHITESPACE: &'static str = lang::EXPECTED_WHITESPACE;
67    pub const EXPECTED_PATTERN: &'static str = lang::EXPECTED_PATTERN;
68    pub const EXPECTED_KEYWORD: &'static str = lang::EXPECTED_KEYWORD;
69    pub const EXPECTED_NUMBER: &'static str = lang::EXPECTED_NUMBER;
70    pub const EXPECTED_IDENTIFIER: &'static str = lang::EXPECTED_IDENTIFIER;
71    pub const INVALID_UTF8: &'static str = lang::INVALID_UTF8;
72    pub const INVALID_NUMBER_FORMAT: &'static str = lang::INVALID_NUMBER_FORMAT;
73    pub const PARSE_NUMBER_FAILED: &'static str = lang::PARSE_NUMBER_FAILED;
74    pub const INVALID_CHARACTER: &'static str = lang::INVALID_CHARACTER;
75    pub const STRING_LENGTH_EXCEEDS_MAX: &'static str = lang::STRING_LENGTH_EXCEEDS_MAX;
76    pub const MAX_NAME_SIZE_EXCEEDED: &'static str = lang::MAX_NAME_SIZE_EXCEEDED;
77
78    // Error prefix constants (used in Display impl)
79    pub const DECODING_ERROR_PREFIX: &'static str = lang::DECODING_ERROR_PREFIX;
80    pub const VALIDATION_ERROR_PREFIX: &'static str = lang::VALIDATION_ERROR_PREFIX;
81    pub const VERSION_ERROR_PREFIX: &'static str = lang::VERSION_ERROR_PREFIX;
82    pub const MESSAGE_ERROR_PREFIX: &'static str = lang::MESSAGE_ERROR_PREFIX;
83    pub const RECEIVERS_ERROR_PREFIX: &'static str = lang::RECEIVERS_ERROR_PREFIX;
84    pub const NODES_ERROR_PREFIX: &'static str = lang::NODES_ERROR_PREFIX;
85    pub const SIGNAL_ERROR_PREFIX: &'static str = lang::SIGNAL_ERROR_PREFIX;
86
87    // Signal error constants
88    pub const SIGNAL_PARSE_INVALID_START_BIT: &'static str = lang::SIGNAL_PARSE_INVALID_START_BIT;
89    pub const SIGNAL_PARSE_INVALID_LENGTH: &'static str = lang::SIGNAL_PARSE_INVALID_LENGTH;
90    pub const SIGNAL_PARSE_INVALID_FACTOR: &'static str = lang::SIGNAL_PARSE_INVALID_FACTOR;
91    pub const SIGNAL_PARSE_INVALID_OFFSET: &'static str = lang::SIGNAL_PARSE_INVALID_OFFSET;
92    pub const SIGNAL_PARSE_INVALID_MIN: &'static str = lang::SIGNAL_PARSE_INVALID_MIN;
93    pub const SIGNAL_PARSE_INVALID_MAX: &'static str = lang::SIGNAL_PARSE_INVALID_MAX;
94    pub const SIGNAL_PARSE_UNIT_TOO_LONG: &'static str = lang::SIGNAL_PARSE_UNIT_TOO_LONG;
95    pub const SIGNAL_NAME_EMPTY: &'static str = lang::SIGNAL_NAME_EMPTY;
96    pub const SIGNAL_LENGTH_TOO_SMALL: &'static str = lang::SIGNAL_LENGTH_TOO_SMALL;
97    pub const SIGNAL_LENGTH_TOO_LARGE: &'static str = lang::SIGNAL_LENGTH_TOO_LARGE;
98    #[cfg(feature = "std")]
99    pub const SIGNAL_LENGTH_REQUIRED: &'static str = lang::SIGNAL_LENGTH_REQUIRED;
100    #[cfg(feature = "std")]
101    pub const SIGNAL_START_BIT_REQUIRED: &'static str = lang::SIGNAL_START_BIT_REQUIRED;
102    #[cfg(feature = "std")]
103    pub const SIGNAL_BYTE_ORDER_REQUIRED: &'static str = lang::SIGNAL_BYTE_ORDER_REQUIRED;
104    #[cfg(feature = "std")]
105    pub const SIGNAL_UNSIGNED_REQUIRED: &'static str = lang::SIGNAL_UNSIGNED_REQUIRED;
106    #[cfg(feature = "std")]
107    pub const SIGNAL_FACTOR_REQUIRED: &'static str = lang::SIGNAL_FACTOR_REQUIRED;
108    #[cfg(feature = "std")]
109    pub const SIGNAL_OFFSET_REQUIRED: &'static str = lang::SIGNAL_OFFSET_REQUIRED;
110    #[cfg(feature = "std")]
111    pub const SIGNAL_MIN_REQUIRED: &'static str = lang::SIGNAL_MIN_REQUIRED;
112    #[cfg(feature = "std")]
113    pub const SIGNAL_MAX_REQUIRED: &'static str = lang::SIGNAL_MAX_REQUIRED;
114    pub const SIGNAL_OVERLAP: &'static str = lang::SIGNAL_OVERLAP;
115    pub const SIGNAL_EXTENDS_BEYOND_MESSAGE: &'static str = lang::SIGNAL_EXTENDS_BEYOND_MESSAGE;
116    pub const SIGNAL_EXTENDS_BEYOND_DATA: &'static str = lang::SIGNAL_EXTENDS_BEYOND_DATA;
117    pub const SIGNAL_RECEIVERS_TOO_MANY: &'static str = lang::SIGNAL_RECEIVERS_TOO_MANY;
118
119    // Validation and decoding error constants
120    pub const NODES_DUPLICATE_NAME: &'static str = lang::NODES_DUPLICATE_NAME;
121    pub const NODES_TOO_MANY: &'static str = lang::NODES_TOO_MANY;
122    pub const DUPLICATE_MESSAGE_ID: &'static str = lang::DUPLICATE_MESSAGE_ID;
123    pub const SENDER_NOT_IN_NODES: &'static str = lang::SENDER_NOT_IN_NODES;
124    pub const INVALID_RANGE: &'static str = lang::INVALID_RANGE;
125    pub const MESSAGE_TOO_MANY_SIGNALS: &'static str = lang::MESSAGE_TOO_MANY_SIGNALS;
126    pub const EXTENDED_MULTIPLEXING_TOO_MANY: &'static str = lang::EXTENDED_MULTIPLEXING_TOO_MANY;
127    pub const MESSAGE_NAME_EMPTY: &'static str = lang::MESSAGE_NAME_EMPTY;
128    pub const MESSAGE_SENDER_EMPTY: &'static str = lang::MESSAGE_SENDER_EMPTY;
129    pub const MESSAGE_DLC_TOO_SMALL: &'static str = lang::MESSAGE_DLC_TOO_SMALL;
130    pub const MESSAGE_DLC_TOO_LARGE: &'static str = lang::MESSAGE_DLC_TOO_LARGE;
131    #[cfg(feature = "std")]
132    pub const MESSAGE_DLC_REQUIRED: &'static str = lang::MESSAGE_DLC_REQUIRED;
133    pub const MESSAGE_ID_OUT_OF_RANGE: &'static str = lang::MESSAGE_ID_OUT_OF_RANGE;
134    #[cfg(feature = "std")]
135    pub const MESSAGE_ID_REQUIRED: &'static str = lang::MESSAGE_ID_REQUIRED;
136    pub const MESSAGE_INVALID_ID: &'static str = lang::MESSAGE_INVALID_ID;
137    pub const MESSAGE_INVALID_DLC: &'static str = lang::MESSAGE_INVALID_DLC;
138    pub const MESSAGE_NOT_FOUND: &'static str = lang::MESSAGE_NOT_FOUND;
139    pub const PAYLOAD_LENGTH_MISMATCH: &'static str = lang::PAYLOAD_LENGTH_MISMATCH;
140    pub const MULTIPLEXER_SWITCH_NEGATIVE: &'static str = lang::MULTIPLEXER_SWITCH_NEGATIVE;
141    #[cfg(feature = "std")]
142    pub const RECEIVERS_DUPLICATE_NAME: &'static str = lang::RECEIVERS_DUPLICATE_NAME;
143
144    // Value description error constants (no_std)
145    pub const VALUE_DESCRIPTION_MESSAGE_NOT_FOUND: &'static str =
146        lang::VALUE_DESCRIPTION_MESSAGE_NOT_FOUND;
147    pub const VALUE_DESCRIPTION_SIGNAL_NOT_FOUND: &'static str =
148        lang::VALUE_DESCRIPTION_SIGNAL_NOT_FOUND;
149    pub const VALUE_DESCRIPTIONS_TOO_MANY: &'static str = lang::VALUE_DESCRIPTIONS_TOO_MANY;
150    pub const VALUE_DESCRIPTIONS_EMPTY: &'static str = lang::VALUE_DESCRIPTIONS_EMPTY;
151
152    // Extended multiplexing error constants (no_std)
153    pub const EXT_MUX_MESSAGE_NOT_FOUND: &'static str = lang::EXT_MUX_MESSAGE_NOT_FOUND;
154    pub const EXT_MUX_SIGNAL_NOT_FOUND: &'static str = lang::EXT_MUX_SIGNAL_NOT_FOUND;
155    pub const EXT_MUX_SWITCH_NOT_FOUND: &'static str = lang::EXT_MUX_SWITCH_NOT_FOUND;
156    pub const EXT_MUX_INVALID_RANGE: &'static str = lang::EXT_MUX_INVALID_RANGE;
157
158    // Encoding error constants (no_std)
159    pub const ENCODING_ERROR_PREFIX: &'static str = lang::ENCODING_ERROR_PREFIX;
160    pub const ENCODING_SIGNAL_NOT_FOUND: &'static str = lang::ENCODING_SIGNAL_NOT_FOUND;
161    pub const ENCODING_VALUE_OUT_OF_RANGE: &'static str = lang::ENCODING_VALUE_OUT_OF_RANGE;
162    pub const ENCODING_VALUE_OVERFLOW: &'static str = lang::ENCODING_VALUE_OVERFLOW;
163
164    // Attribute error constants (no_std)
165    pub const ATTRIBUTE_DEFINITIONS_TOO_MANY: &'static str = lang::ATTRIBUTE_DEFINITIONS_TOO_MANY;
166    pub const ATTRIBUTE_VALUES_TOO_MANY: &'static str = lang::ATTRIBUTE_VALUES_TOO_MANY;
167    pub const ATTRIBUTE_ENUM_VALUES_TOO_MANY: &'static str = lang::ATTRIBUTE_ENUM_VALUES_TOO_MANY;
168    pub const ATTRIBUTE_DEFINITION_NOT_FOUND: &'static str = lang::ATTRIBUTE_DEFINITION_NOT_FOUND;
169    pub const ATTRIBUTE_VALUE_TYPE_MISMATCH: &'static str = lang::ATTRIBUTE_VALUE_TYPE_MISMATCH;
170    pub const ATTRIBUTE_VALUE_OUT_OF_RANGE: &'static str = lang::ATTRIBUTE_VALUE_OUT_OF_RANGE;
171    pub const ATTRIBUTE_ENUM_VALUE_INVALID: &'static str = lang::ATTRIBUTE_ENUM_VALUE_INVALID;
172
173    // Attribute error constants (std-only)
174    #[cfg(feature = "std")]
175    pub const ATTRIBUTE_NAME_REQUIRED: &'static str = lang::ATTRIBUTE_NAME_REQUIRED;
176    #[cfg(feature = "std")]
177    pub const ATTRIBUTE_VALUE_TYPE_REQUIRED: &'static str = lang::ATTRIBUTE_VALUE_TYPE_REQUIRED;
178}
179
180/// Result type alias for operations that can return an `Error`.
181pub type Result<T> = core::result::Result<T, Error>;