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