dbc-rs 0.4.2

Database CAN (DBC) parsing and editing library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use core::fmt;

use super::Error;

impl Error {
    // ============================================================================
    // Constructor helpers (for creating errors without line info)
    // ============================================================================

    /// Create an UnexpectedEof error without line info.
    #[inline]
    pub fn unexpected_eof() -> Self {
        Error::UnexpectedEof { line: None }
    }

    /// Create an UnexpectedEof error with line info.
    #[inline]
    pub fn unexpected_eof_at(line: usize) -> Self {
        Error::UnexpectedEof { line: Some(line) }
    }

    /// Create an Expected error without line info.
    #[inline]
    pub fn expected(msg: &'static str) -> Self {
        Error::Expected { msg, line: None }
    }

    /// Create an Expected error with line info.
    #[inline]
    pub fn expected_at(msg: &'static str, line: usize) -> Self {
        Error::Expected {
            msg,
            line: Some(line),
        }
    }

    /// Create an InvalidChar error without line info.
    #[inline]
    pub fn invalid_char(char: char) -> Self {
        Error::InvalidChar { char, line: None }
    }

    /// Create an InvalidChar error with line info.
    #[inline]
    pub fn invalid_char_at(char: char, line: usize) -> Self {
        Error::InvalidChar {
            char,
            line: Some(line),
        }
    }

    /// Create a MaxStrLength error without line info.
    #[inline]
    pub fn max_str_length(max: usize) -> Self {
        Error::MaxStrLength { max, line: None }
    }

    /// Create a MaxStrLength error with line info.
    #[inline]
    pub fn max_str_length_at(max: usize, line: usize) -> Self {
        Error::MaxStrLength {
            max,
            line: Some(line),
        }
    }

    /// Create a Version error without line info.
    #[inline]
    pub fn version(msg: &'static str) -> Self {
        Error::Version { msg, line: None }
    }

    /// Create a Version error with line info.
    #[inline]
    pub fn version_at(msg: &'static str, line: usize) -> Self {
        Error::Version {
            msg,
            line: Some(line),
        }
    }

    /// Create a Message error without line info.
    #[inline]
    pub fn message(msg: &'static str) -> Self {
        Error::Message { msg, line: None }
    }

    /// Create a Message error with line info.
    #[inline]
    pub fn message_at(msg: &'static str, line: usize) -> Self {
        Error::Message {
            msg,
            line: Some(line),
        }
    }

    /// Create a Receivers error without line info.
    #[inline]
    pub fn receivers(msg: &'static str) -> Self {
        Error::Receivers { msg, line: None }
    }

    /// Create a Receivers error with line info.
    #[inline]
    pub fn receivers_at(msg: &'static str, line: usize) -> Self {
        Error::Receivers {
            msg,
            line: Some(line),
        }
    }

    /// Create a Nodes error without line info.
    #[inline]
    pub fn nodes(msg: &'static str) -> Self {
        Error::Nodes { msg, line: None }
    }

    /// Create a Nodes error with line info.
    #[inline]
    pub fn nodes_at(msg: &'static str, line: usize) -> Self {
        Error::Nodes {
            msg,
            line: Some(line),
        }
    }

    /// Create a Signal error without line info.
    #[inline]
    pub fn signal(msg: &'static str) -> Self {
        Error::Signal { msg, line: None }
    }

    /// Create a Signal error with line info.
    #[inline]
    pub fn signal_at(msg: &'static str, line: usize) -> Self {
        Error::Signal {
            msg,
            line: Some(line),
        }
    }

    // ============================================================================
    // Line number helpers
    // ============================================================================

    /// Get the line number associated with this error, if any.
    #[inline]
    pub fn line(&self) -> Option<usize> {
        match self {
            Error::UnexpectedEof { line } => *line,
            Error::Expected { line, .. } => *line,
            Error::InvalidChar { line, .. } => *line,
            Error::MaxStrLength { line, .. } => *line,
            Error::Version { line, .. } => *line,
            Error::Message { line, .. } => *line,
            Error::Receivers { line, .. } => *line,
            Error::Nodes { line, .. } => *line,
            Error::Signal { line, .. } => *line,
            Error::Decoding(_) | Error::Encoding(_) | Error::Validation(_) => None,
            #[cfg(feature = "std")]
            Error::Io(_) => None,
        }
    }

    /// Add line number information to an error.
    /// If the error already has line info, it is preserved.
    #[inline]
    pub fn with_line(self, line: usize) -> Self {
        match self {
            Error::UnexpectedEof { line: None } => Error::UnexpectedEof { line: Some(line) },
            Error::Expected { msg, line: None } => Error::Expected {
                msg,
                line: Some(line),
            },
            Error::InvalidChar { char, line: None } => Error::InvalidChar {
                char,
                line: Some(line),
            },
            Error::MaxStrLength { max, line: None } => Error::MaxStrLength {
                max,
                line: Some(line),
            },
            Error::Version { msg, line: None } => Error::Version {
                msg,
                line: Some(line),
            },
            Error::Message { msg, line: None } => Error::Message {
                msg,
                line: Some(line),
            },
            Error::Receivers { msg, line: None } => Error::Receivers {
                msg,
                line: Some(line),
            },
            Error::Nodes { msg, line: None } => Error::Nodes {
                msg,
                line: Some(line),
            },
            Error::Signal { msg, line: None } => Error::Signal {
                msg,
                line: Some(line),
            },
            // Already has line info or doesn't support it - return unchanged
            other => other,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::UnexpectedEof { line } => {
                if let Some(line) = line {
                    write!(f, "line {}: {}", line, Error::UNEXPECTED_EOF)
                } else {
                    write!(f, "{}", Error::UNEXPECTED_EOF)
                }
            }
            Error::Expected { msg, line } => {
                if let Some(line) = line {
                    write!(f, "line {}: {}", line, msg)
                } else {
                    write!(f, "{}", msg)
                }
            }
            Error::InvalidChar { char, line } => {
                if let Some(line) = line {
                    write!(
                        f,
                        "line {}: {}: {}",
                        line,
                        Error::INVALID_CHARACTER,
                        char.escape_debug()
                    )
                } else {
                    write!(f, "{}: {}", Error::INVALID_CHARACTER, char.escape_debug())
                }
            }
            Error::MaxStrLength { max, line } => {
                if let Some(line) = line {
                    write!(
                        f,
                        "line {}: {}: {}",
                        line,
                        Error::STRING_LENGTH_EXCEEDS_MAX,
                        max
                    )
                } else {
                    write!(f, "{}: {}", Error::STRING_LENGTH_EXCEEDS_MAX, max)
                }
            }
            Error::Version { msg, line } => {
                if let Some(line) = line {
                    write!(f, "line {}: {}: {}", line, Error::VERSION_ERROR_PREFIX, msg)
                } else {
                    write!(f, "{}: {}", Error::VERSION_ERROR_PREFIX, msg)
                }
            }
            Error::Message { msg, line } => {
                if let Some(line) = line {
                    write!(f, "line {}: {}: {}", line, Error::MESSAGE_ERROR_PREFIX, msg)
                } else {
                    write!(f, "{}: {}", Error::MESSAGE_ERROR_PREFIX, msg)
                }
            }
            Error::Receivers { msg, line } => {
                if let Some(line) = line {
                    write!(
                        f,
                        "line {}: {}: {}",
                        line,
                        Error::RECEIVERS_ERROR_PREFIX,
                        msg
                    )
                } else {
                    write!(f, "{}: {}", Error::RECEIVERS_ERROR_PREFIX, msg)
                }
            }
            Error::Nodes { msg, line } => {
                if let Some(line) = line {
                    write!(f, "line {}: {}: {}", line, Error::NODES_ERROR_PREFIX, msg)
                } else {
                    write!(f, "{}: {}", Error::NODES_ERROR_PREFIX, msg)
                }
            }
            Error::Signal { msg, line } => {
                if let Some(line) = line {
                    write!(f, "line {}: {}: {}", line, Error::SIGNAL_ERROR_PREFIX, msg)
                } else {
                    write!(f, "{}: {}", Error::SIGNAL_ERROR_PREFIX, msg)
                }
            }
            Error::Decoding(msg) => {
                write!(f, "{}: {}", Error::DECODING_ERROR_PREFIX, msg)
            }
            Error::Encoding(msg) => {
                write!(f, "{}: {}", Error::ENCODING_ERROR_PREFIX, msg)
            }
            Error::Validation(msg) => {
                write!(f, "{}: {}", Error::VALIDATION_ERROR_PREFIX, msg)
            }
            #[cfg(feature = "std")]
            Error::Io(msg) => {
                write!(f, "I/O error: {}", msg)
            }
        }
    }
}

#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Self {
        Error::Io(err.to_string())
    }
}

impl From<core::num::ParseIntError> for Error {
    fn from(_err: core::num::ParseIntError) -> Self {
        Error::expected(Error::PARSE_NUMBER_FAILED)
    }
}

// std::error::Error is only available with std feature
#[cfg(feature = "std")]
impl std::error::Error for Error {}

#[cfg(test)]
mod tests {
    #![allow(clippy::float_cmp)]

    // Tests that require std feature (for Display/ToString)
    #[cfg(feature = "std")]
    mod tests_with_std {
        use crate::Error;

        #[test]
        fn test_from_parse_int_error() {
            // Create a ParseIntError by trying to parse an invalid string
            let parse_error = "invalid".parse::<u32>().unwrap_err();
            let error: Error = parse_error.into();

            match error {
                Error::Expected { msg, line } => {
                    assert_eq!(msg, Error::PARSE_NUMBER_FAILED);
                    assert_eq!(line, None);
                }
                _ => panic!("Expected Error::Expected"),
            }
        }

        #[test]
        fn test_display_decoding_error() {
            let error = Error::Decoding("Test error message");
            let display = error.to_string();
            assert!(display.starts_with(Error::DECODING_ERROR_PREFIX));
            assert!(display.contains("Test error message"));
        }

        #[test]
        fn test_display_signal_error() {
            let error = Error::signal("Test signal error");
            let display = error.to_string();
            assert!(display.starts_with(Error::SIGNAL_ERROR_PREFIX));
            assert!(display.contains("Test signal error"));
        }

        #[test]
        fn test_display_formatting() {
            // Test that Display properly formats complex error messages
            let error = Error::Decoding(
                "Duplicate message ID: 256 (messages 'EngineData' and 'BrakeData')",
            );
            let display = error.to_string();
            assert!(display.starts_with(Error::DECODING_ERROR_PREFIX));
            assert!(display.contains("256"));
            assert!(display.contains("EngineData"));
            assert!(display.contains("BrakeData"));
        }

        #[test]
        fn test_display_from_parse_int_error() {
            let int_error = "not_a_number".parse::<u32>().unwrap_err();
            let error: Error = int_error.into();
            let display = error.to_string();

            assert!(display.contains(Error::PARSE_NUMBER_FAILED));
        }

        #[test]
        fn test_error_with_line_number() {
            let error = Error::expected_at("Expected identifier", 42);
            let display = error.to_string();
            assert!(display.contains("line 42"));
            assert!(display.contains("Expected identifier"));
        }

        #[test]
        fn test_error_without_line_number() {
            let error = Error::expected("Expected identifier");
            let display = error.to_string();
            assert!(!display.contains("line"));
            assert!(display.contains("Expected identifier"));
        }

        #[test]
        fn test_with_line_adds_line_info() {
            let error = Error::expected("Expected whitespace");
            assert_eq!(error.line(), None);

            let error_with_line = error.with_line(10);
            assert_eq!(error_with_line.line(), Some(10));
        }

        #[test]
        fn test_with_line_preserves_existing_line() {
            let error = Error::expected_at("Expected whitespace", 5);
            let error_with_line = error.with_line(10);
            // Should preserve the original line (5), not overwrite with 10
            assert_eq!(error_with_line.line(), Some(5));
        }

        #[test]
        fn test_invalid_char_display() {
            let error = Error::invalid_char_at('\t', 15);
            let display = error.to_string();
            assert!(display.contains("line 15"));
            assert!(display.contains("\\t"));
        }

        #[test]
        fn test_max_str_length_display() {
            let error = Error::max_str_length_at(256, 20);
            let display = error.to_string();
            assert!(display.contains("line 20"));
            assert!(display.contains("256"));
        }
    }

    // Tests that require std feature (for std::error::Error trait)
    // Only available when std is enabled
    #[cfg(feature = "std")]
    mod tests_std {
        use super::super::Error;
        use std::error::Error as StdError;

        #[test]
        fn test_std_error_trait() {
            let error = Error::Decoding("Test");
            // Verify it implements std::error::Error
            let _: &dyn StdError = &error;

            // Verify source() returns None (no underlying error)
            assert!(error.source().is_none());
        }
    }
}