Skip to content

Commit ef12055

Browse files
author
Pascal Hertleif
authored
Merge pull request #9 from andresv/prepend-x-to-enums-that-start-with-numeric
Prepend X to enum fields that start with a numeric
2 parents d911a7e + d97fe63 commit ef12055

File tree

5 files changed

+84
-9
lines changed

5 files changed

+84
-9
lines changed

README.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
1-
# CAN DBC code-gen for embedded Rust
1+
# CAN DBC code generator for Rust
22

3-
Experimental code generator.
4-
Use with caution.
5-
Breaking changes will happen when you least expect it.
3+
Generates Rust messages from a `dbc` file.
4+
5+
⚠️ This is experimental - use with caution. Breaking changes will happen when you least expect it. ⚠️
6+
7+
# Usage
8+
9+
Generate `messages.rs` from `example.dbc`.
10+
```bash
11+
cargo run -- testing/dbc-examples/example.dbc dir/where/messages_rs/file/is/written
12+
```
13+
14+
# Development
15+
16+
```bash
17+
# generate messages.rs
18+
cargo run -- testing/dbc-examples/example.dbc testing/can-messages/src
19+
20+
# build all binaries
21+
cargo build --all
22+
23+
# run all tests
24+
cargo test --all
25+
26+
# format before commiting anything
27+
cargo fmt --all
28+
```
29+
30+
## Generate .kdc from .dbc
31+
32+
Use [canmatrix](https://github.com/ebroecker/canmatrix) if you need to generate a new `.kcd` file from a `.dbc`.
33+
34+
```bash
35+
# https://canmatrix.readthedocs.io/en/latest/installation.html
36+
pip install canmatrix
37+
pip install git+https://github.com/ebroecker/canmatrix#egg=canmatrix[kcd]
38+
39+
# generate .kcd
40+
canconvert testing/dbc-examples/example.dbc testing/dbc-examples/example.kcd
41+
```

src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,5 +534,9 @@ fn enum_name(msg: &Message, signal: &Signal) -> String {
534534
}
535535

536536
fn enum_variant_name(x: &str) -> String {
537-
x.to_camel_case()
537+
if !x.starts_with(|c: char| c.is_ascii_alphabetic()) {
538+
format!("X{}", x.to_camel_case())
539+
} else {
540+
x.to_camel_case()
541+
}
538542
}

testing/can-messages/src/messages.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,14 @@ impl Bar {
376376
/// - Unit: "boolean"
377377
/// - Receivers: Dolor
378378
#[inline(always)]
379-
pub fn five(&self) -> bool {
380-
self.five_raw()
379+
pub fn five(&self) -> BarFive {
380+
match self.five_raw() {
381+
false => BarFive::X0off,
382+
true => BarFive::X1on,
383+
false => BarFive::X2oner,
384+
false => BarFive::X3onest,
385+
x => BarFive::Other(x),
386+
}
381387
}
382388

383389
/// Get raw value of Five
@@ -440,6 +446,16 @@ pub enum BarFour {
440446
Onest,
441447
Other(u8),
442448
}
449+
/// Defined values for Five
450+
#[derive(Clone, Copy, PartialEq)]
451+
#[cfg_attr(feature = "debug", derive(Debug))]
452+
pub enum BarFive {
453+
X0off,
454+
X1on,
455+
X2oner,
456+
X3onest,
457+
Other(bool),
458+
}
443459

444460
/// This is just to make testing easier
445461
fn main() {}

testing/dbc-examples/example.dbc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ NS_ :
55

66
BS_:
77

8-
BU_: Lorem Ipsum Dolor
8+
BU_: Lorem Ipsum Dolor
99

1010

1111
BO_ 256 Foo: 4 Lorem
@@ -29,3 +29,4 @@ BO_ 512 Bar: 8 Ipsum
2929

3030
VAL_ 512 Three 0 "OFF" 1 "ON" 2 "ONER" 3 "ONEST";
3131
VAL_ 512 Four 0 "Off" 1 "On" 2 "Oner" 3 "Onest";
32+
VAL_ 512 Five 0 "0Off" 1 "1On" 2 "2Oner" 3 "3Onest";

testing/dbc-examples/example.kcd

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<Node name="Lorem" id="1"/>
44
<Node name="Ipsum" id="2"/>
55
<Node name="Dolor" id="3"/>
6-
<Bus name="Example">
6+
<Bus name="testing/dbc-examples/example">
77
<Message id="0x100" name="Foo" length="4">
88
<Notes></Notes>
99
<Producer>
@@ -38,6 +38,12 @@
3838
<NodeRef id="3"/>
3939
</Consumer>
4040
<Value max="7"/>
41+
<LabelSet>
42+
<Label name="OFF" value="0"/>
43+
<Label name="ON" value="1"/>
44+
<Label name="ONER" value="2"/>
45+
<Label name="ONEST" value="3"/>
46+
</LabelSet>
4147
</Signal>
4248
<Signal name="Four" offset="13" length="2" endianess="big">
4349
<Consumer>
@@ -51,6 +57,18 @@
5157
<Label name="Onest" value="3"/>
5258
</LabelSet>
5359
</Signal>
60+
<Signal name="Five" offset="25" endianess="big">
61+
<Consumer>
62+
<NodeRef id="3"/>
63+
</Consumer>
64+
<Value unit="boolean"/>
65+
<LabelSet>
66+
<Label name="0Off" value="0"/>
67+
<Label name="1On" value="1"/>
68+
<Label name="2Oner" value="2"/>
69+
<Label name="3Onest" value="3"/>
70+
</LabelSet>
71+
</Signal>
5472
</Message>
5573
</Bus>
5674
</NetworkDefinition>

0 commit comments

Comments
 (0)