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