dbc-rs
A clean, zero-dependency DBC (CAN Database) file parser and editor for Rust.
Features
- ✅ Zero dependencies with
alloc/stdfeatures (optionalheaplessfor embedded) - ✅ no_std support - Works on embedded targets
- ✅ Full editing & writing - Modify and save DBC files
- ✅ Well tested - Tested with real-world DBC files
Quick Start
use Dbc;
let content = read_to_string.unwrap;
let dbc = parse.expect;
if let Some = dbc.messages.iter.find
Feature Flags
⚠️ Important: You MUST enable either alloc OR heapless (or use std which includes alloc).
| Feature | Default | Description |
|---|---|---|
alloc |
❌ | Heap-allocated collections via alloc crate. Requires global allocator. Zero dependencies. |
heapless |
❌ | Stack-allocated, bounded collections (no allocator). One dependency: heapless. |
std |
✅ | Includes alloc + std library (builders, I/O, formatting). Zero dependencies. |
Examples:
# Default: std enabled
= "1"
# no_std with heap allocation
= { = "1", = false, = ["alloc"] }
# no_std with stack allocation
= { = "1", = false, = ["heapless"] }
DBC Format Support
Core Features ✅
- Version (
VERSION), Nodes (BU_), Messages (BO_), Signals (SG_), Value Descriptions (VAL_) - All signal features: start bit, length, byte order, sign, factor, offset, min/max, unit, receivers
- 29-bit Extended CAN IDs (2048-536870911)
Limitations ❌
Not implemented: Value tables (VAL_TABLE_), structured comments (CM_), attributes (BA_*), signal groups, environment variables (EV_), signal multiplexing.
Note: NS_ and BS_ are parsed but ignored. Single-line // comments are parsed but not preserved on save.
Examples
Basic Parsing
use Dbc;
let dbc = parse?;
println!;
Creating DBC Files
use ;
let version = new.version.build?;
let nodes = new.add_node.build?;
let signal = new
.name
.start_bit
.length
.byte_order
.factor
.unit
.build?;
let message = new
.id
.name
.dlc
.sender
.add_signal
.build?;
let dbc = new
.version
.nodes
.add_message
.build?;
let dbc_string = dbc.to_dbc_string;
Error Handling
use ;
match parse
Security & Limits
Capacity limits prevent resource exhaustion (DoS protection). Defaults accommodate typical DBC files. Limits are configurable at build time:
| Environment Variable | Default | Description |
|---|---|---|
DBC_MAX_MESSAGES |
8192 |
Maximum number of messages per DBC file (must be power of 2 for heapless) |
DBC_MAX_SIGNALS_PER_MESSAGE |
64 |
Maximum number of signals per message |
DBC_MAX_NODES |
256 |
Maximum number of nodes in the bus |
DBC_MAX_VALUE_DESCRIPTIONS |
64 |
Maximum number of value descriptions |
DBC_MAX_RECEIVER_NODES |
64 |
Maximum number of receiver nodes per signal |
DBC_MAX_NAME_SIZE |
64 |
Maximum length of names (messages, signals, nodes, etc.) |
Example:
# Reduce capacity limits for embedded targets (recommended for heapless)
DBC_MAX_MESSAGES=512
Performance Notes:
alloc/std: Heap-allocated, dynamic sizingheapless: Stack-allocated, fixed-size arrays (default 8192 messages; reduce to 256-512 for embedded targets. All values must be powers of 2.)- Parsing: O(n) complexity, entire file parsed into memory
Troubleshooting
- "Message ID out of valid range": Standard 11-bit (0-0x7FF) or Extended 29-bit (0x800-0x1FFFFFFF)
- "Signal extends beyond message": Ensure
start_bit + length <= DLC * 8 - "Signal overlap": Signals must not occupy overlapping bit ranges
- "Sender not in nodes": Add message sender to nodes list
- "Duplicate message ID": Use unique CAN IDs
Design
- Immutability: All data structures are immutable after creation
- Validation: Input validation at construction time
- no_std First: Designed for
no_std, optionalstdfeature - Zero Dependencies: No dependencies with
alloc/stdfeatures - Result-based errors: All fallible operations return
Result<T>
Contributing
Contributions welcome! Areas needing work: Value tables, structured comments, attributes, environment variables, signal multiplexing.
License
Available under MIT OR Apache-2.0 (open source) or commercial licensing. See LICENSING.md for details.
References
- DBC Format Specification
- Security Audit
- Vector Informatik: "DBC File Format Documentation Version 01/2007"
- commaai/opendbc - Open-source DBC files