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//! - **Zero dependencies**: Pure Rust implementation
9//! - **Memory efficient**: Uses fixed-size arrays for `no_std` builds
10//! - **Type-safe**: Strong typing for all DBC elements
11//! - **Internationalized errors**: Support for multiple languages
12//!
13//! ## Kernel Support (Experimental)
14//!
15//! ⚠️ **EXPERIMENTAL**: The `kernel` feature is experimental and subject to change.
16//! It provides compatibility with Linux kernel's `kernel::alloc` API for use in kernel modules.
17//! This feature is still under development and may have breaking changes.
18//!
19//! - The `kernel` feature is mutually exclusive with `alloc` and `std`
20//! - Uses a mock `kernel::alloc` implementation for testing (not the real kernel alloc API)
21//! - Real kernel usage requires integration with rust-for-linux
22//! - API may change without notice
23//!
24//! ## Usage
25//!
26//! ```rust,no_run
27//! use dbc_rs::Dbc;
28//!
29//! let dbc_content = r#"VERSION "1.0"
30//!
31//! BU_: ECM TCM
32//!
33//! BO_ 256 EngineData : 8 ECM
34//!  SG_ RPM : 0|16@0+ (0.25,0) [0|8000] "rpm" TCM
35//! "#;
36//!
37//! let dbc = Dbc::parse(dbc_content)?;
38//! # Ok::<(), dbc_rs::Error>(())
39//! ```
40
41#![cfg_attr(not(feature = "std"), no_std)]
42
43#[cfg(any(feature = "alloc", feature = "kernel"))]
44extern crate alloc;
45
46// EXPERIMENTAL: Kernel feature support
47// This provides a mock kernel::alloc module for testing kernel compatibility
48// Note: This is experimental and subject to change
49// Real kernel usage requires integration with rust-for-linux and actual kernel::alloc
50#[cfg(feature = "kernel")]
51pub mod kernel_mock;
52
53#[cfg(feature = "kernel")]
54pub use kernel_mock as kernel;
55
56// Compatibility layer for alloc vs kernel::alloc
57#[cfg(any(feature = "alloc", feature = "kernel"))]
58mod compat;
59
60mod byte_order;
61mod dbc;
62mod error;
63mod message;
64mod nodes;
65mod parse_options;
66mod parser;
67mod receivers;
68mod signal;
69#[cfg(any(feature = "alloc", feature = "kernel"))]
70mod value_descriptions;
71mod version;
72
73pub use byte_order::ByteOrder;
74#[cfg(any(feature = "alloc", feature = "kernel"))]
75pub use dbc::ValueDescriptionsList;
76pub use dbc::{Dbc, MessageList};
77pub use error::{Error, Result};
78pub use message::{Message, Signals};
79pub use nodes::Nodes;
80pub use parse_options::ParseOptions;
81pub use receivers::Receivers;
82pub use signal::Signal;
83#[cfg(any(feature = "alloc", feature = "kernel"))]
84pub use value_descriptions::{ValueDescriptions, ValueDescriptionsBuilder};
85pub use version::Version;
86
87#[cfg(any(feature = "alloc", feature = "kernel"))]
88pub use dbc::DbcBuilder;
89#[cfg(any(feature = "alloc", feature = "kernel"))]
90pub use message::MessageBuilder;
91#[cfg(any(feature = "alloc", feature = "kernel"))]
92pub use nodes::NodesBuilder;
93#[cfg(any(feature = "alloc", feature = "kernel"))]
94pub use receivers::ReceiversBuilder;
95#[cfg(any(feature = "alloc", feature = "kernel"))]
96pub use signal::SignalBuilder;
97#[cfg(any(feature = "alloc", feature = "kernel"))]
98pub use version::VersionBuilder;
99
100pub(crate) use parser::Parser;
101
102/// The version of this crate as specified in `Cargo.toml`.
103///
104/// This constant is only available when the `std` feature is enabled.
105#[cfg(feature = "std")]
106pub const PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
107
108// Maximum limits for two-pass parsing (no alloc)
109// Note: MAX_MESSAGES and MAX_SIGNALS_PER_MESSAGE are now defined in limits.rs
110// (generated by build.rs) and can be overridden at build time.
111// Access them via Signals::max_capacity() and MessageList::max_capacity().
112pub(crate) const MAX_NODES: usize = 256;
113
114// DBC file format keywords
115pub(crate) const VERSION: &str = "VERSION";
116pub(crate) const CM_: &str = "CM_";
117pub(crate) const NS_: &str = "NS_";
118pub(crate) const BS_: &str = "BS_";
119pub(crate) const BU_: &str = "BU_";
120pub(crate) const BO_: &str = "BO_";
121pub(crate) const SG_: &str = "SG_";
122pub(crate) const VAL_TABLE_: &str = "VAL_TABLE_";
123pub(crate) const BA_DEF_: &str = "BA_DEF_";
124pub(crate) const BA_DEF_DEF_: &str = "BA_DEF_DEF_";
125pub(crate) const BA_: &str = "BA_";
126pub(crate) const VAL_: &str = "VAL_";
127pub(crate) const SIG_GROUP_: &str = "SIG_GROUP_";
128pub(crate) const SIG_VALTYPE_: &str = "SIG_VALTYPE_";
129pub(crate) const EV_: &str = "EV_";
130pub(crate) const BO_TX_BU_: &str = "BO_TX_BU_";
131
132// Additional DBC keywords
133pub(crate) const VECTOR__INDEPENDENT_SIG_MSG: &str = "VECTOR__INDEPENDENT_SIG_MSG";
134pub(crate) const VECTOR__XXX: &str = "VECTOR__XXX";
135pub(crate) const BA_DEF_DEF_REL_: &str = "BA_DEF_DEF_REL_";
136pub(crate) const BA_DEF_SGTYPE_: &str = "BA_DEF_SGTYPE_";
137pub(crate) const SIGTYPE_VALTYPE_: &str = "SIGTYPE_VALTYPE_";
138pub(crate) const ENVVAR_DATA_: &str = "ENVVAR_DATA_";
139pub(crate) const SIG_TYPE_REF_: &str = "SIG_TYPE_REF_";
140pub(crate) const NS_DESC_: &str = "NS_DESC_";
141pub(crate) const BA_DEF_REL_: &str = "BA_DEF_REL_";
142pub(crate) const BA_SGTYPE_: &str = "BA_SGTYPE_";
143pub(crate) const SGTYPE_VAL_: &str = "SGTYPE_VAL_";
144pub(crate) const BU_SG_REL_: &str = "BU_SG_REL_";
145pub(crate) const BU_EV_REL_: &str = "BU_EV_REL_";
146pub(crate) const BU_BO_REL_: &str = "BU_BO_REL_";
147pub(crate) const SG_MUL_VAL_: &str = "SG_MUL_VAL_";
148pub(crate) const BA_REL_: &str = "BA_REL_";
149pub(crate) const CAT_DEF_: &str = "CAT_DEF_";
150pub(crate) const EV_DATA_: &str = "EV_DATA_";
151pub(crate) const CAT_: &str = "CAT_";
152pub(crate) const FILTER: &str = "FILTER";
153
154#[cfg_attr(not(feature = "std"), allow(dead_code))]
155const DBC_KEYWORDS: &[&str] = &[
156    VECTOR__INDEPENDENT_SIG_MSG,
157    VECTOR__XXX,
158    BA_DEF_DEF_REL_,
159    BA_DEF_SGTYPE_,
160    SIGTYPE_VALTYPE_,
161    ENVVAR_DATA_,
162    SIG_TYPE_REF_,
163    NS_DESC_,
164    BA_DEF_REL_,
165    BA_SGTYPE_,
166    SGTYPE_VAL_,
167    VAL_TABLE_,
168    SIG_GROUP_,
169    SIG_VALTYPE_,
170    BO_TX_BU_,
171    BU_SG_REL_,
172    BU_EV_REL_,
173    BU_BO_REL_,
174    SG_MUL_VAL_,
175    BA_DEF_DEF_,
176    BA_DEF_,
177    BA_REL_,
178    CAT_DEF_,
179    EV_DATA_,
180    BA_,
181    VAL_,
182    CM_,
183    CAT_,
184    NS_,
185    BS_,
186    BU_,
187    BO_,
188    SG_,
189    EV_,
190    VERSION,
191    FILTER,
192];