CAN DBC code generator for Rust
Generates Rust messages from a dbc
file.
⚠️ This is experimental - use with caution. ⚠️
Installation
Install published version using cargo:
Install latest version from the git repository:
Using dbc-codegen
Generate messages.rs
from example.dbc
using the CLI:
Or put something like this into your build.rs
file:
Using generated Rust code
dbc-codegen generates a Rust file that is expected to be in a cargo project.
Here is an example testing/can-messages/Cargo.toml
which defines dependencies and features that are used in generated message file.
Project setup
For this to work you need to add following dependencies to Cargo.toml
:
= { = "1.0", = false }
= { = "1.0", = true } # Enable with `arb` feature
To use the code, add mod messages
to your lib.rs
(or main.rs
).
You will most likely want to interact with the generated Messages
enum, and call Messages::from_can_message(id, &payload)
.
Note: The generated code contains a lot of documentation. Give it a try:
Feature flags
The following (optional) features can be specified:
debug
: enables#[derive(Debug)
for messagesrange_checked
: adds range checks in settersarb
: enables implementation of [Arbitrary
] trait. Also requires you to addarbitrary
crate (version 1.x) as a dependency of the feature, usingarb = ["arbitrary"]
. [Arbitrary
]: https://docs.rs/arbitrary/1.0.0/arbitrary/trait.Arbitrary.htmlstd
: Implementsstd::error::Error
forCanError
. This makes it easy to useanyhow
for error handling.
To enable all features add this to your Cargo.toml
:
# features for dbc-codegen `messages.rs` file
[]
= ["debug", "arb", "range_checked", "std"]
= ["arbitrary"]
= []
= []
= []
Field/variant rename rules
If some field name starts with a non-alphabetic character or is a Rust keyword then it is prefixed with x
.
For example:
VAL_ 512 Five 0 "0Off" 1 "1On" 2 "2Oner" 3 "3Onest";
…is generated as:
Type
here:
SG_ Type : 30|1@0+ (1,0) [0|1] "boolean" Dolor
…conflicts with the Rust keyword type
. Therefore we prefix it with x
:
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.