canparse 0.1.4

A CAN signal and definition parser
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
//! CANdb definition parsing

#![allow(non_upper_case_globals)]

use std::error::Error;
use std::fmt;
use std::fmt::{Display, Formatter};
use std::str::FromStr;

pub mod library;
pub mod nom;

pub use self::library::DbcLibrary;

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Version(pub String);

#[derive(Debug, Clone, PartialEq)]
pub struct BusConfiguration(pub f32);

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct MessageDefinition {
    pub id: u32,
    pub name: String,
    pub message_len: u32,
    pub sending_node: String,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct MessageDescription {
    pub id: u32,
    // TODO: Remove this
    pub signal_name: String,
    pub description: String,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct MessageAttribute {
    pub name: String,
    pub id: u32,
    pub signal_name: String,
    pub value: String,
}

#[derive(Debug, Clone, PartialEq)]
pub struct SignalDefinition {
    pub name: String,
    pub start_bit: usize,
    pub bit_len: usize,
    pub little_endian: bool,
    pub signed: bool,
    pub scale: f32,
    pub offset: f32,
    pub min_value: f32,
    pub max_value: f32,
    pub units: String,
    pub receiving_node: String,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SignalDescription {
    pub id: u32,
    pub signal_name: String,
    pub description: String,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SignalAttribute {
    pub name: String,
    pub id: u32,
    pub signal_name: String,
    pub value: String,
}

/// Composed DBC entry.
#[derive(Debug, Clone, PartialEq)]
pub enum Entry {
    /// `VERSION`
    Version(Version),

    /// BS_: <Speed>
    BusConfiguration(BusConfiguration),

    // TODO: ??
    // CanNodes,
    // `CM_ BU_ [can id] [signal name] "[description]"`
    // CanNodesDescription,
    // CanNodesAttribute,
    /// `BO_ [can id] [message name]: [message length] [sending node]`
    MessageDefinition(MessageDefinition),
    /// `CM_ BO_ [can id] [signal name] "[description]"`
    MessageDescription(MessageDescription),
    /// `BA_ "[attribute name]" BO_ [node|can id] [signal name] [attribute value];`
    MessageAttribute(MessageAttribute),

    /// `SG_ [signal name] [...] : [start bit]|[length]@[endian][sign] [[min]|[max]] "[unit]" [receiving nodes]`
    SignalDefinition(SignalDefinition),
    /// `CM_ SG_ [can id] [signal name] "[description]"`
    SignalDescription(SignalDescription),
    /// `BA_ "[attribute name]" SG_ [node|can id] [signal name] [attribute value];`
    SignalAttribute(SignalAttribute),

    // `CM_ [BU_|BO_|SG_] [can id] [signal name] "[description]"`
    // Description, -- flatten subtypes instead

    // `BA_DEF_ ...`
    // AttributeDefinition,

    // `BA_DEF_DEF_ ...`
    // AttributeDefault,

    // `BA_ "[attribute name]" [BU_|BO_|SG_] [node|can id] [signal name] [attribute value];`
    // Attribute
    Unknown(String),
}

impl Entry {
    /// Returns an opaque `EntryType` for an `Entry` structure variant.
    // TODO: Finalize naming convention and expose
    pub(super) fn get_type(&self) -> EntryType {
        match self {
            Entry::Version(_) => EntryType::Version,
            Entry::BusConfiguration(_) => EntryType::BusConfiguration,
            Entry::MessageDefinition(_) => EntryType::MessageDefinition,
            Entry::MessageDescription(_) => EntryType::MessageDescription,
            Entry::MessageAttribute(_) => EntryType::MessageAttribute,
            Entry::SignalDefinition(_) => EntryType::SignalDefinition,
            Entry::SignalDescription(_) => EntryType::SignalDescription,
            Entry::SignalAttribute(_) => EntryType::SignalAttribute,
            Entry::Unknown(_) => EntryType::Unknown,
        }
    }
}

impl Display for Entry {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        self.get_type().fmt(f)
    }
}

/// Internal type for DBC `Entry` line.
enum_from_primitive! {
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum EntryType {
    Version = 0,

    BusConfiguration,

    // CanNodes,
    // CanNodesDescription,
    // CanNodesAttribute

    MessageDefinition,
    MessageDescription,
    MessageAttribute,
//    MessageAttributeDefinition,

    SignalDefinition,
    SignalDescription,
    SignalAttribute,
//    SignalAttributeDefinition,

    // AttributeDefinition,
    // AttributeDefault,
    // Attribute

    Unknown,
}
}

impl Display for EntryType {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let entry_str = match *self {
            EntryType::Version => "Version",
            EntryType::BusConfiguration => "BusConfiguration",
            EntryType::MessageDefinition => "MessageDefinition",
            EntryType::MessageDescription => "MessageDescription",
            EntryType::MessageAttribute => "MessageAttribute",
            EntryType::SignalDefinition => "SignalDefinition",
            EntryType::SignalDescription => "SignalDescription",
            EntryType::SignalAttribute => "SignalAttribute",

            EntryType::Unknown => "Unknown",
        };
        write!(f, "{}", entry_str)
    }
}

/// Error returned on failure to parse DBC `Entry`.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ParseEntryError {
    kind: EntryErrorKind,
}

impl ParseEntryError {
    #[doc(hidden)]
    pub fn __description(&self) -> &str {
        self.kind.__description()
    }

    #[doc(hidden)]
    pub fn __cause(&self) -> Option<&Error> {
        self.kind.__cause()
    }
}

impl Display for ParseEntryError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}", self.__description())
    }
}

impl Error for ParseEntryError {
    fn description(&self) -> &str {
        self.__description()
    }

    fn cause(&self) -> Option<&Error> {
        self.__cause()
    }
}

/// Internal type DBC `Entry` parsing error.
#[derive(Debug, Clone, Eq, PartialEq)]
enum EntryErrorKind {
    /// Could not find a regex match for input
    RegexNoMatch,
    /// Integer could not be converted into valid `EntryType`
    UnknownEntryType(i32),
    /// Failure to combine all values from regex capture
    RegexCapture,
}

impl EntryErrorKind {
    #[doc(hidden)]
    pub fn __description(&self) -> &str {
        match *self {
            EntryErrorKind::RegexNoMatch => "could not find a regex match for input",
            EntryErrorKind::UnknownEntryType(_) => {
                "integer could not be converted into valid EntryType"
            }
            EntryErrorKind::RegexCapture => "failure to combine all values from regex capture",
        }
    }
    #[doc(hidden)]
    pub fn __cause(&self) -> Option<&Error> {
        match *self {
            EntryErrorKind::RegexNoMatch => None,
            EntryErrorKind::UnknownEntryType(_) => None,
            EntryErrorKind::RegexCapture => None,
        }
    }
}

impl Display for EntryErrorKind {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let s = self.__description();
        write!(f, "{}", s)
    }
}

impl From<EntryErrorKind> for ParseEntryError {
    fn from(kind: EntryErrorKind) -> Self {
        ParseEntryError { kind }
    }
}

impl FromStr for Entry {
    type Err = ParseEntryError;

    fn from_str(line: &str) -> Result<Self, Self::Err> {
        nom::entry(line)
            .map_err(|_e| EntryErrorKind::RegexNoMatch.into())
            .map(|(_i, entry)| entry)
    }
}

/// Probably some spec to determine a type when generating structs
/// Here an enum will be dispatched instead (e.g., VAL_)
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ValueDefinition {
    values: Vec<String>,
}

pub enum AttributeType {
    /// Integer type with min/max values
    Int {
        min: i32,
        max: i32,
    },
    /// Float type with min/max values
    Float {
        min: f32,
        max: f32,
    },
    /// String type
    String,
    /// Enum type, represented as a vector of `String`s
    Enum(Vec<String>),
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    macro_rules! test_entry {
        ($test_name: ident, $entry_type: ident, $test_line: expr, $expected: expr) => {
            mod $test_name {
                use dbc::*;
                use std::str::FromStr;

                #[test]
                fn from_str() {
                    assert_eq!(
                        Entry::from_str($test_line),
                        Ok(Entry::$entry_type($expected))
                    );
                }

                /*
                // FIXME: This test ends up failing because of `Entry::Unknown`
                #[test]
                fn from_str_err() {
                    let failstr = format!("GONNAFAIL {}\n", $test_line);
                    let res = Entry::from_str(&failstr);
                    println!("res: {:?}", res);
                    assert!(
                        res.is_err(),
                        "Result of entry parse failure should be Err"
                    );
                }
                */

                #[test]
                fn entry_type() {
                    let entry = Entry::$entry_type($expected);
                    let entry_type = EntryType::$entry_type;

                    assert_eq!(entry.get_type(), entry_type);
                    assert_eq!(format!("{}", entry), format!("{}", entry_type),);
                }

                #[test]
                fn nom_parse() {
                    assert_eq!(nom::$test_name($test_line).unwrap().1, $expected);
                    assert_eq!(
                        nom::entry($test_line).unwrap().1,
                        Entry::$entry_type($expected)
                    );
                }
            }
        };
    }

    test_entry!(
        version,
        Version,
        "VERSION \"A version string\"\n",
        Version("A version string".to_string())
    );

    test_entry!(
        message_definition,
        MessageDefinition,
        "BO_ 2364539904 EEC1 : 8 Vector__XXX\n",
        MessageDefinition {
            id: 2364539904,
            name: "EEC1".to_string(),
            message_len: 8,
            sending_node: "Vector__XXX".to_string()
        }
    );

    test_entry!(
        message_description,
        MessageDescription,
        "CM_ BO_ 2364539904 \"Engine Controller\";\n",
        MessageDescription {
            id: 2364539904,
            signal_name: "".to_string(),
            description: "Engine Controller".to_string()
        }
    );

    test_entry!(
        message_attribute,
        MessageAttribute,
        "BA_ \"SingleFrame\" BO_ 2364539904 0;\n",
        MessageAttribute {
            name: "SingleFrame".to_string(),
            signal_name: "".to_string(),
            id: 2364539904,
            value: "0".to_string()
        }
    );

    test_entry!(
        signal_definition,
        SignalDefinition,
        " SG_ Engine_Speed : 24|16@1+ (0.125,0) [0|8031.88] \"rpm\" Vector__XXX\n",
        SignalDefinition {
            name: "Engine_Speed".to_string(),
            start_bit: 24,
            bit_len: 16,
            little_endian: true,
            signed: false,
            scale: 0.125,
            offset: 0.0,
            min_value: 0.0,
            max_value: 8031.88,
            units: "rpm".to_string(),
            receiving_node: "Vector__XXX".to_string()
        }
    );

    test_entry!(
        signal_description,
        SignalDescription,
        "CM_ SG_ 2364539904 Engine_Speed \"A description for Engine speed.\";\n",
        SignalDescription {
            id: 2364539904,
            signal_name: "Engine_Speed".to_string(),
            description: "A description for Engine speed.".to_string()
        }
    );

    test_entry!(
        signal_attribute,
        SignalAttribute,
        "BA_ \"SPN\" SG_ 2364539904 Engine_Speed 190;\n",
        SignalAttribute {
            name: "SPN".to_string(),
            id: 2364539904,
            signal_name: "Engine_Speed".to_string(),
            value: "190".to_string()
        }
    );

    mod multiline {
        test_entry!(
            signal_description,
            SignalDescription,
            "CM_ SG_ 2364539904 Actual_Engine___Percent_Torque_High_Resolution \"A multi- \r \
             \r \
             line description for Engine torque.\";\n",
            SignalDescription {
                id: 2364539904,
                signal_name: "Actual_Engine___Percent_Torque_High_Resolution".to_string(),
                description: "A multi- \r \
                              \r \
                              line description for Engine torque."
                    .to_string()
            }
        );
    }
}