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 byte_order;
40mod compat;
41mod dbc;
42mod error;
43mod extended_multiplexing;
44mod message;
45mod nodes;
46mod parser;
47mod receivers;
48mod signal;
49mod value_descriptions;
50mod version;
51
52// Builder infrastructure
53// Note: Macros are available in all configurations since they're compile-time only
54// and have no runtime dependencies. Builders themselves are std-only.
55mod macros;
56
57// High-performance wrapper (std-only)
58#[cfg(feature = "std")]
59mod fast_dbc;
60
61pub use byte_order::ByteOrder;
62pub use dbc::{Dbc, DecodedSignal};
63pub use error::{Error, Result};
64pub use extended_multiplexing::ExtendedMultiplexing;
65pub use message::{Message, Signals};
66pub use nodes::{Node, Nodes};
67pub use receivers::Receivers;
68pub use signal::Signal;
69pub use value_descriptions::ValueDescriptions;
70pub use version::Version;
71
72/// Builders
73#[cfg(feature = "std")]
74pub use dbc::DbcBuilder;
75#[cfg(feature = "std")]
76pub use extended_multiplexing::ExtendedMultiplexingBuilder;
77#[cfg(feature = "std")]
78pub use message::MessageBuilder;
79#[cfg(feature = "std")]
80pub use nodes::NodesBuilder;
81#[cfg(feature = "std")]
82pub use receivers::ReceiversBuilder;
83#[cfg(feature = "std")]
84pub use signal::SignalBuilder;
85#[cfg(feature = "std")]
86pub use value_descriptions::ValueDescriptionsBuilder;
87#[cfg(feature = "std")]
88pub use version::VersionBuilder;
89
90// High-performance wrapper
91#[cfg(feature = "std")]
92pub use fast_dbc::FastDbc;
93
94pub(crate) use parser::Parser;
95
96/// The version of this crate as specified in `Cargo.toml`.
97///
98/// This constant is only available when the `std` feature is enabled.
99#[cfg(feature = "std")]
100pub const PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
101
102// Maximum limits for two-pass parsing (no alloc)
103// Note: All MAX_* constants are now defined in limits.rs (generated by build.rs)
104// and can be overridden at build time via environment variables:
105// - DBC_MAX_MESSAGES (default: 8192, must be power of 2 for heapless)
106// - DBC_MAX_SIGNALS_PER_MESSAGE (default: 64)
107// - DBC_MAX_NODES (default: 256)
108// - DBC_MAX_VALUE_DESCRIPTIONS (default: 64)
109// - DBC_MAX_NAME_SIZE (default: 32, per DBC specification)
110include!(concat!(env!("OUT_DIR"), "/limits.rs"));
111
112// DBC file format keywords
113pub(crate) const VERSION: &str = "VERSION";
114pub(crate) const CM_: &str = "CM_";
115pub(crate) const NS_: &str = "NS_";
116pub(crate) const BS_: &str = "BS_";
117pub(crate) const BU_: &str = "BU_";
118pub(crate) const BO_: &str = "BO_";
119pub(crate) const SG_: &str = "SG_";
120pub(crate) const VAL_TABLE_: &str = "VAL_TABLE_";
121pub(crate) const BA_DEF_: &str = "BA_DEF_";
122pub(crate) const BA_DEF_DEF_: &str = "BA_DEF_DEF_";
123pub(crate) const BA_: &str = "BA_";
124pub(crate) const VAL_: &str = "VAL_";
125pub(crate) const SIG_GROUP_: &str = "SIG_GROUP_";
126pub(crate) const SIG_VALTYPE_: &str = "SIG_VALTYPE_";
127pub(crate) const EV_: &str = "EV_";
128pub(crate) const BO_TX_BU_: &str = "BO_TX_BU_";
129
130// Additional DBC keywords
131pub(crate) const VECTOR_INDEPENDENT_SIG_MSG: &str = "VECTOR__INDEPENDENT_SIG_MSG";
132pub(crate) const VECTOR_XXX: &str = "Vector__XXX";
133pub(crate) const BA_DEF_DEF_REL_: &str = "BA_DEF_DEF_REL_";
134pub(crate) const BA_DEF_SGTYPE_: &str = "BA_DEF_SGTYPE_";
135pub(crate) const SIGTYPE_VALTYPE_: &str = "SIGTYPE_VALTYPE_";
136pub(crate) const ENVVAR_DATA_: &str = "ENVVAR_DATA_";
137pub(crate) const SIG_TYPE_REF_: &str = "SIG_TYPE_REF_";
138pub(crate) const NS_DESC_: &str = "NS_DESC_";
139pub(crate) const BA_DEF_REL_: &str = "BA_DEF_REL_";
140pub(crate) const BA_SGTYPE_: &str = "BA_SGTYPE_";
141pub(crate) const SGTYPE_VAL_: &str = "SGTYPE_VAL_";
142pub(crate) const BU_SG_REL_: &str = "BU_SG_REL_";
143pub(crate) const BU_EV_REL_: &str = "BU_EV_REL_";
144pub(crate) const BU_BO_REL_: &str = "BU_BO_REL_";
145pub(crate) const SG_MUL_VAL_: &str = "SG_MUL_VAL_";
146pub(crate) const BA_REL_: &str = "BA_REL_";
147pub(crate) const CAT_DEF_: &str = "CAT_DEF_";
148pub(crate) const EV_DATA_: &str = "EV_DATA_";
149pub(crate) const CAT_: &str = "CAT_";
150pub(crate) const FILTER: &str = "FILTER";
151
152#[cfg_attr(not(feature = "std"), allow(dead_code))]
153const DBC_KEYWORDS: &[&str] = &[
154    VECTOR_INDEPENDENT_SIG_MSG,
155    VECTOR_XXX,
156    BA_DEF_DEF_REL_,
157    BA_DEF_SGTYPE_,
158    SIGTYPE_VALTYPE_,
159    ENVVAR_DATA_,
160    SIG_TYPE_REF_,
161    NS_DESC_,
162    BA_DEF_REL_,
163    BA_SGTYPE_,
164    SGTYPE_VAL_,
165    VAL_TABLE_,
166    SIG_GROUP_,
167    SIG_VALTYPE_,
168    BO_TX_BU_,
169    BU_SG_REL_,
170    BU_EV_REL_,
171    BU_BO_REL_,
172    SG_MUL_VAL_,
173    BA_DEF_DEF_,
174    BA_DEF_,
175    BA_REL_,
176    CAT_DEF_,
177    EV_DATA_,
178    BA_,
179    VAL_,
180    CM_,
181    CAT_,
182    NS_,
183    BS_,
184    BU_,
185    BO_,
186    SG_,
187    EV_,
188    VERSION,
189    FILTER,
190];