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    /// Validation-related error (post-parse, no line info).
52    Validation(&'static str),
53}
54
55impl Error {
56    // ============================================================================
57    // Error message constants
58    // ============================================================================
59
60    pub const UNEXPECTED_EOF: &'static str = lang::UNEXPECTED_EOF;
61    pub const EXPECTED_WHITESPACE: &'static str = lang::EXPECTED_WHITESPACE;
62    pub const EXPECTED_PATTERN: &'static str = lang::EXPECTED_PATTERN;
63    pub const EXPECTED_KEYWORD: &'static str = lang::EXPECTED_KEYWORD;
64    pub const EXPECTED_NUMBER: &'static str = lang::EXPECTED_NUMBER;
65    pub const EXPECTED_IDENTIFIER: &'static str = lang::EXPECTED_IDENTIFIER;
66    pub const INVALID_UTF8: &'static str = lang::INVALID_UTF8;
67    pub const INVALID_NUMBER_FORMAT: &'static str = lang::INVALID_NUMBER_FORMAT;
68    pub const PARSE_NUMBER_FAILED: &'static str = lang::PARSE_NUMBER_FAILED;
69    pub const INVALID_CHARACTER: &'static str = lang::INVALID_CHARACTER;
70    pub const STRING_LENGTH_EXCEEDS_MAX: &'static str = lang::STRING_LENGTH_EXCEEDS_MAX;
71    pub const MAX_NAME_SIZE_EXCEEDED: &'static str = lang::MAX_NAME_SIZE_EXCEEDED;
72
73    // Error prefix constants (used in Display impl)
74    pub const DECODING_ERROR_PREFIX: &'static str = lang::DECODING_ERROR_PREFIX;
75    pub const VALIDATION_ERROR_PREFIX: &'static str = lang::VALIDATION_ERROR_PREFIX;
76    pub const VERSION_ERROR_PREFIX: &'static str = lang::VERSION_ERROR_PREFIX;
77    pub const MESSAGE_ERROR_PREFIX: &'static str = lang::MESSAGE_ERROR_PREFIX;
78    pub const RECEIVERS_ERROR_PREFIX: &'static str = lang::RECEIVERS_ERROR_PREFIX;
79    pub const NODES_ERROR_PREFIX: &'static str = lang::NODES_ERROR_PREFIX;
80    pub const SIGNAL_ERROR_PREFIX: &'static str = lang::SIGNAL_ERROR_PREFIX;
81
82    // Signal error constants
83    pub const SIGNAL_PARSE_INVALID_START_BIT: &'static str = lang::SIGNAL_PARSE_INVALID_START_BIT;
84    pub const SIGNAL_PARSE_INVALID_LENGTH: &'static str = lang::SIGNAL_PARSE_INVALID_LENGTH;
85    pub const SIGNAL_PARSE_INVALID_FACTOR: &'static str = lang::SIGNAL_PARSE_INVALID_FACTOR;
86    pub const SIGNAL_PARSE_INVALID_OFFSET: &'static str = lang::SIGNAL_PARSE_INVALID_OFFSET;
87    pub const SIGNAL_PARSE_INVALID_MIN: &'static str = lang::SIGNAL_PARSE_INVALID_MIN;
88    pub const SIGNAL_PARSE_INVALID_MAX: &'static str = lang::SIGNAL_PARSE_INVALID_MAX;
89    pub const SIGNAL_PARSE_UNIT_TOO_LONG: &'static str = lang::SIGNAL_PARSE_UNIT_TOO_LONG;
90    pub const SIGNAL_NAME_EMPTY: &'static str = lang::SIGNAL_NAME_EMPTY;
91    pub const SIGNAL_LENGTH_TOO_SMALL: &'static str = lang::SIGNAL_LENGTH_TOO_SMALL;
92    pub const SIGNAL_LENGTH_TOO_LARGE: &'static str = lang::SIGNAL_LENGTH_TOO_LARGE;
93    #[cfg(feature = "std")]
94    pub const SIGNAL_LENGTH_REQUIRED: &'static str = lang::SIGNAL_LENGTH_REQUIRED;
95    #[cfg(feature = "std")]
96    pub const SIGNAL_START_BIT_REQUIRED: &'static str = lang::SIGNAL_START_BIT_REQUIRED;
97    #[cfg(feature = "std")]
98    pub const SIGNAL_BYTE_ORDER_REQUIRED: &'static str = lang::SIGNAL_BYTE_ORDER_REQUIRED;
99    #[cfg(feature = "std")]
100    pub const SIGNAL_UNSIGNED_REQUIRED: &'static str = lang::SIGNAL_UNSIGNED_REQUIRED;
101    #[cfg(feature = "std")]
102    pub const SIGNAL_FACTOR_REQUIRED: &'static str = lang::SIGNAL_FACTOR_REQUIRED;
103    #[cfg(feature = "std")]
104    pub const SIGNAL_OFFSET_REQUIRED: &'static str = lang::SIGNAL_OFFSET_REQUIRED;
105    #[cfg(feature = "std")]
106    pub const SIGNAL_MIN_REQUIRED: &'static str = lang::SIGNAL_MIN_REQUIRED;
107    #[cfg(feature = "std")]
108    pub const SIGNAL_MAX_REQUIRED: &'static str = lang::SIGNAL_MAX_REQUIRED;
109    pub const SIGNAL_OVERLAP: &'static str = lang::SIGNAL_OVERLAP;
110    pub const SIGNAL_EXTENDS_BEYOND_MESSAGE: &'static str = lang::SIGNAL_EXTENDS_BEYOND_MESSAGE;
111    pub const SIGNAL_EXTENDS_BEYOND_DATA: &'static str = lang::SIGNAL_EXTENDS_BEYOND_DATA;
112    pub const SIGNAL_RECEIVERS_TOO_MANY: &'static str = lang::SIGNAL_RECEIVERS_TOO_MANY;
113
114    // Validation and decoding error constants
115    pub const NODES_DUPLICATE_NAME: &'static str = lang::NODES_DUPLICATE_NAME;
116    pub const NODES_TOO_MANY: &'static str = lang::NODES_TOO_MANY;
117    pub const DUPLICATE_MESSAGE_ID: &'static str = lang::DUPLICATE_MESSAGE_ID;
118    pub const SENDER_NOT_IN_NODES: &'static str = lang::SENDER_NOT_IN_NODES;
119    pub const INVALID_RANGE: &'static str = lang::INVALID_RANGE;
120    pub const MESSAGE_TOO_MANY_SIGNALS: &'static str = lang::MESSAGE_TOO_MANY_SIGNALS;
121    pub const EXTENDED_MULTIPLEXING_TOO_MANY: &'static str = lang::EXTENDED_MULTIPLEXING_TOO_MANY;
122    pub const MESSAGE_NAME_EMPTY: &'static str = lang::MESSAGE_NAME_EMPTY;
123    pub const MESSAGE_SENDER_EMPTY: &'static str = lang::MESSAGE_SENDER_EMPTY;
124    pub const MESSAGE_DLC_TOO_SMALL: &'static str = lang::MESSAGE_DLC_TOO_SMALL;
125    pub const MESSAGE_DLC_TOO_LARGE: &'static str = lang::MESSAGE_DLC_TOO_LARGE;
126    #[cfg(feature = "std")]
127    pub const MESSAGE_DLC_REQUIRED: &'static str = lang::MESSAGE_DLC_REQUIRED;
128    pub const MESSAGE_ID_OUT_OF_RANGE: &'static str = lang::MESSAGE_ID_OUT_OF_RANGE;
129    #[cfg(feature = "std")]
130    pub const MESSAGE_ID_REQUIRED: &'static str = lang::MESSAGE_ID_REQUIRED;
131    pub const MESSAGE_INVALID_ID: &'static str = lang::MESSAGE_INVALID_ID;
132    pub const MESSAGE_INVALID_DLC: &'static str = lang::MESSAGE_INVALID_DLC;
133    pub const MESSAGE_NOT_FOUND: &'static str = lang::MESSAGE_NOT_FOUND;
134    pub const PAYLOAD_LENGTH_MISMATCH: &'static str = lang::PAYLOAD_LENGTH_MISMATCH;
135    pub const MULTIPLEXER_SWITCH_NEGATIVE: &'static str = lang::MULTIPLEXER_SWITCH_NEGATIVE;
136    #[cfg(feature = "std")]
137    pub const RECEIVERS_DUPLICATE_NAME: &'static str = lang::RECEIVERS_DUPLICATE_NAME;
138
139    // Value description error constants (no_std)
140    pub const VALUE_DESCRIPTION_MESSAGE_NOT_FOUND: &'static str =
141        lang::VALUE_DESCRIPTION_MESSAGE_NOT_FOUND;
142    pub const VALUE_DESCRIPTION_SIGNAL_NOT_FOUND: &'static str =
143        lang::VALUE_DESCRIPTION_SIGNAL_NOT_FOUND;
144    pub const VALUE_DESCRIPTIONS_TOO_MANY: &'static str = lang::VALUE_DESCRIPTIONS_TOO_MANY;
145    pub const VALUE_DESCRIPTIONS_EMPTY: &'static str = lang::VALUE_DESCRIPTIONS_EMPTY;
146
147    // Extended multiplexing error constants (no_std)
148    pub const EXT_MUX_MESSAGE_NOT_FOUND: &'static str = lang::EXT_MUX_MESSAGE_NOT_FOUND;
149    pub const EXT_MUX_SIGNAL_NOT_FOUND: &'static str = lang::EXT_MUX_SIGNAL_NOT_FOUND;
150    pub const EXT_MUX_SWITCH_NOT_FOUND: &'static str = lang::EXT_MUX_SWITCH_NOT_FOUND;
151    pub const EXT_MUX_INVALID_RANGE: &'static str = lang::EXT_MUX_INVALID_RANGE;
152}
153
154/// Result type alias for operations that can return an `Error`.
155pub type Result<T> = core::result::Result<T, Error>;