dbc_rs/
lib.rs

1//! # dbc-rs
2//!
3//! A `no_std` compatible Rust library for parsing and working with DBC (CAN database) files.
4//!
5//! ## Features
6//!
7//! - **`no_std` compatible**: Works in embedded environments without the standard library
8//! - **Minimal dependencies**: Only `heapless` when using `heapless` feature (zero dependencies with `alloc`/`std`)
9//! - **Memory efficient**: Uses `Vec` (via `alloc`) for dynamic collections
10//! - **Type-safe**: Strong typing for all DBC elements
11//!
12//! ## Usage
13//!
14//! ```rust,no_run
15//! use dbc_rs::Dbc;
16//!
17//! let dbc_content = r#"VERSION "1.0"
18//!
19//! BU_: ECM TCM
20//!
21//! BO_ 256 EngineData : 8 ECM
22//!  SG_ RPM : 0|16@0+ (0.25,0) [0|8000] "rpm" TCM
23//! "#;
24//!
25//! let dbc = Dbc::parse(dbc_content)?;
26//! # Ok::<(), dbc_rs::Error>(())
27//! ```
28
29#![cfg_attr(not(feature = "std"), no_std)]
30#![deny(unused_must_use)]
31#![forbid(unsafe_code)]
32
33#[cfg(feature = "std")]
34extern crate std;
35
36#[cfg(all(feature = "alloc", not(feature = "heapless")))]
37extern crate alloc;
38
39mod bit_timing;
40mod byte_order;
41mod compat;
42mod dbc;
43mod error;
44mod extended_multiplexing;
45mod message;
46mod nodes;
47mod parser;
48mod receivers;
49mod signal;
50mod value_descriptions;
51mod version;
52
53// Builder infrastructure
54// Note: Macros are available in all configurations since they're compile-time only
55// and have no runtime dependencies. Builders themselves are std-only.
56mod macros;
57
58// High-performance wrapper (std-only)
59#[cfg(feature = "std")]
60mod fast_dbc;
61
62pub use bit_timing::BitTiming;
63pub use byte_order::ByteOrder;
64pub use dbc::{Dbc, DecodedSignal};
65pub use error::{Error, Result};
66pub use extended_multiplexing::ExtendedMultiplexing;
67pub use message::{Message, Signals};
68pub use nodes::{Node, Nodes};
69pub use receivers::Receivers;
70pub use signal::Signal;
71pub use value_descriptions::ValueDescriptions;
72pub use version::Version;
73
74/// Builders
75#[cfg(feature = "std")]
76pub use bit_timing::BitTimingBuilder;
77#[cfg(feature = "std")]
78pub use dbc::DbcBuilder;
79#[cfg(feature = "std")]
80pub use extended_multiplexing::ExtendedMultiplexingBuilder;
81#[cfg(feature = "std")]
82pub use message::MessageBuilder;
83#[cfg(feature = "std")]
84pub use nodes::NodesBuilder;
85#[cfg(feature = "std")]
86pub use receivers::ReceiversBuilder;
87#[cfg(feature = "std")]
88pub use signal::SignalBuilder;
89#[cfg(feature = "std")]
90pub use value_descriptions::ValueDescriptionsBuilder;
91#[cfg(feature = "std")]
92pub use version::VersionBuilder;
93
94// High-performance wrapper
95#[cfg(feature = "std")]
96pub use fast_dbc::FastDbc;
97
98pub(crate) use parser::Parser;
99
100/// The version of this crate as specified in `Cargo.toml`.
101///
102/// This constant is only available when the `std` feature is enabled.
103#[cfg(feature = "std")]
104pub const PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
105
106// Maximum limits for two-pass parsing (no alloc)
107// Note: All MAX_* constants are now defined in limits.rs (generated by build.rs)
108// and can be overridden at build time via environment variables:
109// - DBC_MAX_MESSAGES (default: 8192, must be power of 2 for heapless)
110// - DBC_MAX_SIGNALS_PER_MESSAGE (default: 64)
111// - DBC_MAX_NODES (default: 256)
112// - DBC_MAX_VALUE_DESCRIPTIONS (default: 64)
113// - DBC_MAX_NAME_SIZE (default: 32, per DBC specification)
114include!(concat!(env!("OUT_DIR"), "/limits.rs"));
115
116// DBC file format keywords
117pub(crate) const VERSION: &str = "VERSION";
118pub(crate) const CM_: &str = "CM_";
119pub(crate) const NS_: &str = "NS_";
120pub(crate) const BS_: &str = "BS_";
121pub(crate) const BU_: &str = "BU_";
122pub(crate) const BO_: &str = "BO_";
123pub(crate) const SG_: &str = "SG_";
124pub(crate) const VAL_TABLE_: &str = "VAL_TABLE_";
125pub(crate) const BA_DEF_: &str = "BA_DEF_";
126pub(crate) const BA_DEF_DEF_: &str = "BA_DEF_DEF_";
127pub(crate) const BA_: &str = "BA_";
128pub(crate) const VAL_: &str = "VAL_";
129pub(crate) const SIG_GROUP_: &str = "SIG_GROUP_";
130pub(crate) const SIG_VALTYPE_: &str = "SIG_VALTYPE_";
131pub(crate) const EV_: &str = "EV_";
132pub(crate) const BO_TX_BU_: &str = "BO_TX_BU_";
133
134// Additional DBC keywords
135pub(crate) const VECTOR_INDEPENDENT_SIG_MSG: &str = "VECTOR__INDEPENDENT_SIG_MSG";
136pub(crate) const VECTOR_XXX: &str = "Vector__XXX";
137pub(crate) const BA_DEF_DEF_REL_: &str = "BA_DEF_DEF_REL_";
138pub(crate) const BA_DEF_SGTYPE_: &str = "BA_DEF_SGTYPE_";
139pub(crate) const SIGTYPE_VALTYPE_: &str = "SIGTYPE_VALTYPE_";
140pub(crate) const ENVVAR_DATA_: &str = "ENVVAR_DATA_";
141pub(crate) const SIG_TYPE_REF_: &str = "SIG_TYPE_REF_";
142pub(crate) const NS_DESC_: &str = "NS_DESC_";
143pub(crate) const BA_DEF_REL_: &str = "BA_DEF_REL_";
144pub(crate) const BA_SGTYPE_: &str = "BA_SGTYPE_";
145pub(crate) const SGTYPE_VAL_: &str = "SGTYPE_VAL_";
146pub(crate) const BU_SG_REL_: &str = "BU_SG_REL_";
147pub(crate) const BU_EV_REL_: &str = "BU_EV_REL_";
148pub(crate) const BU_BO_REL_: &str = "BU_BO_REL_";
149pub(crate) const SG_MUL_VAL_: &str = "SG_MUL_VAL_";
150pub(crate) const BA_REL_: &str = "BA_REL_";
151pub(crate) const CAT_DEF_: &str = "CAT_DEF_";
152pub(crate) const EV_DATA_: &str = "EV_DATA_";
153pub(crate) const CAT_: &str = "CAT_";
154pub(crate) const FILTER: &str = "FILTER";
155
156#[cfg_attr(not(feature = "std"), allow(dead_code))]
157const DBC_KEYWORDS: &[&str] = &[
158    VECTOR_INDEPENDENT_SIG_MSG,
159    VECTOR_XXX,
160    BA_DEF_DEF_REL_,
161    BA_DEF_SGTYPE_,
162    SIGTYPE_VALTYPE_,
163    ENVVAR_DATA_,
164    SIG_TYPE_REF_,
165    NS_DESC_,
166    BA_DEF_REL_,
167    BA_SGTYPE_,
168    SGTYPE_VAL_,
169    VAL_TABLE_,
170    SIG_GROUP_,
171    SIG_VALTYPE_,
172    BO_TX_BU_,
173    BU_SG_REL_,
174    BU_EV_REL_,
175    BU_BO_REL_,
176    SG_MUL_VAL_,
177    BA_DEF_DEF_,
178    BA_DEF_,
179    BA_REL_,
180    CAT_DEF_,
181    EV_DATA_,
182    BA_,
183    VAL_,
184    CM_,
185    CAT_,
186    NS_,
187    BS_,
188    BU_,
189    BO_,
190    SG_,
191    EV_,
192    VERSION,
193    FILTER,
194];