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
For design principles, module structure, and internal details, see ARCHITECTURE.md.
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 exactly one of: std, alloc, or heapless. See ARCHITECTURE.md for details.
| Feature | Default | Description |
|---|---|---|
std |
✅ | Full std library support (includes alloc). Zero dependencies. |
alloc |
❌ | Heap allocation without std. Requires global allocator. Zero dependencies. |
heapless |
❌ | Stack allocation, no allocator needed. One dependency: heapless. |
embedded-can |
❌ | decode_frame() via embedded-can Frame trait. |
Cargo.toml examples:
# Default: std enabled
= "0.1.0"
# no_std with heap allocation
= { = "0.1.0", = false, = ["alloc"] }
# no_std with stack allocation
= { = "0.1.0", = false, = ["heapless"] }
# With embedded-can Frame decoding support
= { = "0.1.0", = ["embedded-can"] }
# no_std with stack allocation + embedded-can (embedded targets)
= { = "0.1.0", = false, = ["heapless", "embedded-can"] }
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
- Extended CAN IDs (bit 31 flag per DBC spec)
- Signal multiplexing: Basic (
M,m0,m1) and extended (SG_MUL_VAL_)
Limitations ❌
Not implemented: Value tables (VAL_TABLE_), structured comments (CM_), attributes (BA_*), signal groups, environment variables (EV_).
Note: NS_ and BS_ are parsed but ignored. Single-line // comments are parsed but not preserved on save.
Examples
Basic Parsing (works with either std, alloc, or heapless)
use Dbc;
let dbc = parse?;
println!;
Creating DBC Files
use ;
use ;
let dbc = new
.version
.nodes
.add_message
.build?;
let dbc_string = dbc.to_dbc_string;
Decoding CAN Messages
use Dbc;
let dbc = parse?;
// Decode a standard CAN message (11-bit ID)
let payload = ;
let decoded = dbc.decode?;
for signal in decoded.iter
// Decode an extended CAN message (29-bit ID)
let decoded_ext = dbc.decode?;
With the embedded-can feature, you can decode frames directly:
use Dbc;
let decoded = dbc.decode_frame?;
Error Handling
use ;
match parse
Security & Limits
Capacity limits prevent resource exhaustion (DoS protection). Limits are configurable at build time via environment variables. See ARCHITECTURE.md for the full list.
# Example: Reduce limits for embedded targets
DBC_MAX_MESSAGES=512
Note: With heapless, most limits must be powers of 2.
Troubleshooting
- "Message ID out of valid range": Standard 11-bit (0-0x7FF) or Extended 29-bit with bit 31 set (0x80000000-0x9FFFFFFF)
- "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
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
- Architecture - Internal design and module structure
- DBC Format Specification
- Security Audit
- Vector Informatik: "DBC File Format Documentation Version 01/2007"
- commaai/opendbc - Open-source DBC files