Skip to content

Commit 6c7d88b

Browse files
author
Pascal Hertleif
authored
Merge pull request #13 from andresv/prepend-x-to-rust-keywords
Prepend x to rust keywords
2 parents ef12055 + 1847ff6 commit 6c7d88b

File tree

6 files changed

+81
-27
lines changed

6 files changed

+81
-27
lines changed

README.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,46 @@ Generates Rust messages from a `dbc` file.
44

55
⚠️ This is experimental - use with caution. Breaking changes will happen when you least expect it. ⚠️
66

7-
# Usage
7+
## Usage
88

99
Generate `messages.rs` from `example.dbc`.
1010
```bash
1111
cargo run -- testing/dbc-examples/example.dbc dir/where/messages_rs/file/is/written
1212
```
1313

14-
# Development
14+
If some field name starts with a non-alphabetic character or is a Rust keyword then it is prepended with `x`.
15+
16+
For example:
17+
```
18+
VAL_ 512 Five 0 "0Off" 1 "1On" 2 "2Oner" 3 "3Onest";
19+
```
20+
..is generated to:
21+
```rust
22+
pub enum BarFive {
23+
X0off,
24+
X1on,
25+
X2oner,
26+
X3onest,
27+
Other(bool),
28+
}
29+
```
30+
31+
`Type` here:
32+
```
33+
SG_ Type : 30|1@0+ (1,0) [0|1] "boolean" Dolor
34+
```
35+
..would become Rust keyword `type` therefore it is prepended with `x`:
36+
```rust
37+
pub fn xtype(&self) -> BarType {
38+
match self.xtype_raw() {
39+
false => BarType::X0off,
40+
true => BarType::X1on,
41+
x => BarType::Other(x),
42+
}
43+
}
44+
```
45+
46+
## Development
1547

1648
```bash
1749
# generate messages.rs
@@ -27,7 +59,7 @@ cargo test --all
2759
cargo fmt --all
2860
```
2961

30-
## Generate .kdc from .dbc
62+
### Generate .kdc from .dbc
3163

3264
Use [canmatrix](https://github.com/ebroecker/canmatrix) if you need to generate a new `.kcd` file from a `.dbc`.
3365

src/keywords.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// https://doc.rust-lang.org/stable/reference/keywords.html
2+
const KEYWORDS: [&str; 52] = [
3+
"as", "break", "const", "continue", "crate", "else", "enum", "extern", "false", "fn", "for",
4+
"if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref", "return",
5+
"self", "Self", "static", "struct", "super", "trait", "true", "type", "unsafe", "use", "where",
6+
"while", "async", "await", "dyn", "abstract", "become", "box", "do", "final", "macro",
7+
"override", "priv", "typeof", "unsized", "virtual", "yield", "try", "union",
8+
];
9+
10+
pub fn is_keyword(x: &str) -> bool {
11+
KEYWORDS.contains(&x.to_lowercase().as_str())
12+
}
13+
14+
#[test]
15+
fn value_is_a_keyword() {
16+
assert!(is_keyword("type"));
17+
assert!(is_keyword("TYPE"));
18+
}
19+
20+
#[test]
21+
fn value_is_not_a_keyword() {
22+
assert!(!is_keyword("rpms"));
23+
}

src/main.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{fs::File, io::BufWriter, io::Write, path::PathBuf};
66
use structopt::StructOpt;
77

88
mod includes;
9+
mod keywords;
910
mod pad;
1011

1112
#[derive(Debug, StructOpt)]
@@ -522,7 +523,11 @@ fn type_name(x: &str) -> String {
522523
}
523524

524525
fn field_name(x: &str) -> String {
525-
x.to_snake_case()
526+
if keywords::is_keyword(x) || !x.starts_with(|c: char| c.is_ascii_alphabetic()) {
527+
format!("x{}", x.to_snake_case())
528+
} else {
529+
x.to_snake_case()
530+
}
526531
}
527532

528533
fn enum_name(msg: &Message, signal: &Signal) -> String {

testing/can-messages/src/messages.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,13 @@ impl Bar {
180180
pub const MESSAGE_ID: u32 = 512;
181181

182182
/// Construct new Bar from values
183-
pub fn new(one: u8, two: f32, three: u8, four: u8, five: bool) -> Result<Self, CanError> {
183+
pub fn new(one: u8, two: f32, three: u8, four: u8, xtype: bool) -> Result<Self, CanError> {
184184
let mut res = Self { raw: [0u8; 8] };
185185
res.set_one(one)?;
186186
res.set_two(two)?;
187187
res.set_three(three)?;
188188
res.set_four(four)?;
189-
res.set_five(five)?;
189+
res.set_xtype(xtype)?;
190190
Ok(res)
191191
}
192192

@@ -369,24 +369,22 @@ impl Bar {
369369
Ok(())
370370
}
371371

372-
/// Five
372+
/// Type
373373
///
374374
/// - Min: 0
375375
/// - Max: 1
376376
/// - Unit: "boolean"
377377
/// - Receivers: Dolor
378378
#[inline(always)]
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),
379+
pub fn xtype(&self) -> BarType {
380+
match self.xtype_raw() {
381+
false => BarType::X0off,
382+
true => BarType::X1on,
383+
x => BarType::Other(x),
386384
}
387385
}
388386

389-
/// Get raw value of Five
387+
/// Get raw value of Type
390388
///
391389
/// - Start bit: 30
392390
/// - Signal size: 1 bits
@@ -395,15 +393,15 @@ impl Bar {
395393
/// - Byte order: BigEndian
396394
/// - Value type: Unsigned
397395
#[inline(always)]
398-
pub fn five_raw(&self) -> bool {
396+
pub fn xtype_raw(&self) -> bool {
399397
let signal = u8::unpack_be_bits(&self.raw, (30 - (1 - 1)), 1);
400398

401399
signal == 1
402400
}
403401

404-
/// Set value of Five
402+
/// Set value of Type
405403
#[inline(always)]
406-
pub fn set_five(&mut self, value: bool) -> Result<(), CanError> {
404+
pub fn set_xtype(&mut self, value: bool) -> Result<(), CanError> {
407405
let value = value as u8;
408406
let start_bit = 30;
409407
let bits = 1;
@@ -446,14 +444,12 @@ pub enum BarFour {
446444
Onest,
447445
Other(u8),
448446
}
449-
/// Defined values for Five
447+
/// Defined values for Type
450448
#[derive(Clone, Copy, PartialEq)]
451449
#[cfg_attr(feature = "debug", derive(Debug))]
452-
pub enum BarFive {
450+
pub enum BarType {
453451
X0off,
454452
X1on,
455-
X2oner,
456-
X3onest,
457453
Other(bool),
458454
}
459455

testing/dbc-examples/example.dbc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ BO_ 512 Bar: 8 Ipsum
1717
SG_ Two : 7|8@0+ (0.39,0) [0.00|100] "%" Dolor
1818
SG_ Three : 13|3@0+ (1,0) [0|7] "" Dolor
1919
SG_ Four : 10|2@0+ (1,0) [0|3] "" Dolor
20-
SG_ Five : 30|1@0+ (1,0) [0|1] "boolean" Dolor
20+
SG_ Type : 30|1@0+ (1,0) [0|1] "boolean" Dolor
2121

2222

2323

@@ -29,4 +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";
32+
VAL_ 512 Type 0 "0Off" 1 "1On";

testing/dbc-examples/example.kcd

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,14 @@
5757
<Label name="Onest" value="3"/>
5858
</LabelSet>
5959
</Signal>
60-
<Signal name="Five" offset="25" endianess="big">
60+
<Signal name="Type" offset="25" endianess="big">
6161
<Consumer>
6262
<NodeRef id="3"/>
6363
</Consumer>
6464
<Value unit="boolean"/>
6565
<LabelSet>
6666
<Label name="0Off" value="0"/>
6767
<Label name="1On" value="1"/>
68-
<Label name="2Oner" value="2"/>
69-
<Label name="3Onest" value="3"/>
7068
</LabelSet>
7169
</Signal>
7270
</Message>

0 commit comments

Comments
 (0)