A pragmatic DBC (CAN database) parser and Rust code generator.
This repository is a Rust workspace that currently provides:
dbcparser: library that parses DBC files and exposes a domain model used for code generation.dbcparser-cli: command-line tool that generates Rust code from a DBC file, with filtering and configuration support.parse-dbc(indbcparser-check-cli/): a small helper CLI used to run the generator against a DBC file and a candump log (primarily for checks/demos).
Status: used with real DBC examples, still evolving. Public API and CLI options may change.
This matches the current repository:
.
├── Cargo.toml # workspace
├── Cargo.lock
├── README.md
├── spec-grammar.md # DBC grammar notes
├── CONTRIBUTE.md
├── deny.toml
├── rustfmt.toml
├── .pre-commit-config.yaml
├── dbcparser/
│ ├── Cargo.toml
│ └── src/
│ ├── lib.rs # lib root (re-exports parser/data/gencode)
│ └── gencode.rs # Rust code generation
├── dbcparser-cli/
│ ├── Cargo.toml
│ └── src/
│ └── main.rs # CLI: generate Rust from DBC
└── dbcparser-check-cli/
├── Cargo.toml
└── src/
└── parse-dbc.rs # bin: parse-dbc
Library (dbcparser):
- DBC parsing into an internal domain model (messages, signals, attributes, etc.).
- Domain types and helpers in
src/data.rs. - Code generator in
src/gencode.rsthat turns a DBC into Rust modules and types.
CLI (dbcparser-cli):
- Generate Rust code from a DBC file:
- optional whitelist/blacklist of CAN IDs,
- optional header injection (custom file) or header removal,
- configuration via YAML file,
- ability to save the effective configuration to YAML for later reuse,
- verbose mode to print the effective configuration as YAML.
Helper CLI (parse-dbc):
- Runs the generator with a DBC file and an output path (typically used with a candump log workflow).
- Intended mainly as a lightweight check/demo tool.
Real-world examples:
- Example code and/or generated Rust files live under
dbcparser-cli/examples/.
These are not fully implemented yet, but drive the design:
- Clear pipeline:
lexer (&[u8]) → parser (AST) → validator → IR (domain model) → generators. - Precise error model with spans (line/column) and categories, using
thiserrorin the lib and a rich reporter (color-eyre/miette) in the CLI. - Additional CLI subcommands (besides code generation):
parse,validate,dump,convert,query,stats,grep-db. - Optional converters to JSON/YAML/TOML via
serde. - Strong validation rules: overlapping signals, multiplexing, ID uniqueness, value ranges, etc.
- Fuzzing and benchmarks for robustness and performance.
- Rust stable (edition 2021)
Optional developer tools:
pre-commit(recommended if you want local hooks)cargo-deny(to usedeny.toml)
git clone https://github.com/redpesk/canforge-rs.git
cd canforge-rs
cargo buildBuild only the CLI:
cargo build -p dbcparser-cliBuild only the helper CLI:
cargo build -p parse-dbcThe main binary is dbcparser-cli.
It currently focuses on generating Rust code from a DBC file.
Usage: dbcparser-cli [OPTIONS]
Options:
-i, --in <INFILE> Input DBC file (required unless a YAML config is provided)
-o, --out <OUTFILE> Output Rust file path (required unless a YAML config is provided)
--uid <UID> Optional UID (module/namespace root in generated code) [default: DbcSimple]
--header-file <HEADER_FILE> Header text file to prepend (overrides built-in header if provided)
--no-header Disable default header completely
--whitelist <WHITELIST> Whitelist CAN IDs (CSV, hex 0xABC or decimal): e.g. "0x101,0x121,201"
--blacklist <BLACKLIST> Blacklist CAN IDs (CSV, hex 0xABC or decimal): e.g. "0x101,0x121,201"
--config <YAML> Load parameters from a YAML configuration file
--save-config <YAML> Save the effective parameters to this YAML file
-v, --verbose Verbose mode: print effective configuration as YAML
-h, --help Print help
-V, --version Print version
Run CLI help:
cargo run -p dbcparser-cli -- --helpWhitelist and blacklist options accept:
- hexadecimal with or without
0x, - decimal integers.
Examples:
--whitelist "0x101,0x121,201"
--blacklist "0x101,0x200"Generate Rust code with defaults:
cargo run -p dbcparser-cli -- \
--in dbcparser-cli/examples/canforge_dbc_complete_norm/dbc/canforge_dbc_complete_norm.dbc \
--out ./__canforge_dbc_complete_norm.rsWhitelist and blacklist filtering:
cargo run -p dbcparser-cli -- \
--in dbcparser-cli/examples/canforge_dbc_complete_norm/dbc/canforge_dbc_complete_norm.dbcc \
--out ../__canforge_dbc_complete_norm.rs \
--whitelist "100" \
--blacklist "401"Load parameters from a YAML file:
cargo run -p dbcparser-cli -- --config ./config.yamlSave the effective configuration (after CLI parsing) to a YAML file:
cargo run -p dbcparser-cli -- \
--in dbcparser-cli/examples/canforge_dbc_complete_norm/dbc/canforge_dbc_complete_norm.dbc \
--out ./generated_canforge_dbc_complete_norm.rs \
--whitelist "0x101,0x121" \
--save-config ./effective.yamlVerbose mode prints the effective configuration as YAML to stdout:
cargo run -p dbcparser-cli -- --config ./config.yaml --verboseThe helper binary is parse-dbc (package: parse-dbc).
cargo run -p parse-dbc -- <DBC_FILE> <OUTPUT_FILE>The tool is intentionally minimal and is mainly used for checks/demos. For production-style code generation, prefer dbcparser-cli.
The library is available as the dbcparser crate within this workspace.
Current internal structure:
src/parser.rs— parsing logic (nom-based).src/data.rs— data structures representing messages, signals, attributes, etc.src/gencode.rs— code generator using the parsed DBC representation.src/lib.rs— crate root, re-exporting the main types and functions.
API details are still evolving; expect breaking changes while the internal design converges toward:
Dbc::from_str(&str) -> Result<Dbc, DbcError>Dbc::from_reader<R: Read>(R) -> Result<Dbc, DbcError>- iterators over messages/signals, explicit validation, and optional
serdesupport.
Run all tests:
cargo testTest only the library:
cargo test -p dbcparserTest only the CLI:
cargo test -p dbcparser-cliThe repository includes:
- integration tests for the library in
dbcparser/tests/test.rs, - CLI integration tests in
dbcparser-cli/tests/using real DBC files (BMS, Model3).
High-level design direction:
- Parsing:
parser.rsusesnomto read DBC files into an internal representation. - Domain model:
data.rsholds message/signal/attribute structures used by the generator. - Code generation:
gencode.rstransforms the parsed DBC into Rust modules and types. - Future pipeline (roadmap):
lexer (&[u8]) → parser (AST) → validator → IR (domain model) → generators.
Validation goals include:
- consistent CAN IDs,
- non-overlapping bit ranges for signals,
- consistent multiplexing,
- sensible value ranges and units.
Planned improvements (subject to change):
- Extend grammar coverage for all DBC constructs (attributes, env vars, etc.).
- Introduce a lexer/AST/IR/validator pipeline for clearer separation of concerns.
- Introduce CLI subcommands:
parse: syntax-only parsing with diagnostics,validate: semantic checks and return codes,dump: structured listing,stats: statistics on messages/signals/bit usage,grep-db: search messages/signals by name/attributes.
- Add more real-world DBC fixtures for integration tests.
- Set up benchmarks and fuzzing for robustness and performance.
This project is licensed under the APACHE V2 License.
See LICENSE-APACHE.
- Inspired by existing open-source DBC parsers and tools.
- Built using the Rust ecosystem, in particular
nomfor parsing. - Thanks to contributors and users providing real-world DBC samples.