pub struct DbcBuilder { /* private fields */ }Expand description
Builder for constructing Dbc instances programmatically.
This builder allows you to create DBC files without parsing from a string.
It requires the alloc feature to be enabled.
§Examples
use dbc_rs::{DbcBuilder, NodesBuilder, MessageBuilder, SignalBuilder, VersionBuilder};
let nodes = NodesBuilder::new()
.add_node("ECM")
.add_node("TCM")
.build()?;
let signal = SignalBuilder::new()
.name("RPM")
.start_bit(0)
.length(16)
.build()?;
let message = MessageBuilder::new()
.id(256)
.name("EngineData")
.dlc(8)
.sender("ECM")
.add_signal(signal)
.build()?;
let dbc = DbcBuilder::new(None)
.version(VersionBuilder::new().version("1.0").build()?)
.nodes(nodes)
.add_message(message)
.build()?;Implementations§
Source§impl DbcBuilder
impl DbcBuilder
Sourcepub fn new(dbc: Option<&Dbc<'_>>) -> Self
pub fn new(dbc: Option<&Dbc<'_>>) -> Self
Creates a new DbcBuilder.
If a Dbc is provided, the builder is initialized with all data from it,
allowing you to modify an existing DBC file. If None is provided, an
empty builder is created.
§Arguments
dbc- Optional reference to an existingDbcto initialize from
§Examples
use dbc_rs::DbcBuilder;
// Create empty builder
let builder = DbcBuilder::new(None);use dbc_rs::{Dbc, DbcBuilder, MessageBuilder};
// Parse existing DBC
let dbc = Dbc::parse(r#"VERSION "1.0"
BU_: ECM
BO_ 256 Engine : 8 ECM
"#)?;
// Create builder from existing DBC
let modified = DbcBuilder::new(Some(&dbc))
.add_message(MessageBuilder::new()
.id(512)
.name("Brake")
.dlc(4)
.sender("ECM")
.build()?)
.build()?;
assert_eq!(modified.messages().len(), 2);Sourcepub fn version(self, version: Version<'static>) -> Self
pub fn version(self, version: Version<'static>) -> Self
Sets the version for the DBC file.
§Examples
use dbc_rs::{DbcBuilder, VersionBuilder};
let builder = DbcBuilder::new(None)
.version(VersionBuilder::new().version("1.0").build()?);Sourcepub fn nodes(self, nodes: Nodes<'static>) -> Self
pub fn nodes(self, nodes: Nodes<'static>) -> Self
Sets the nodes (ECUs) for the DBC file.
§Examples
use dbc_rs::{DbcBuilder, NodesBuilder};
let builder = DbcBuilder::new(None)
.nodes(NodesBuilder::new().add_node("ECM").build()?);Sourcepub fn add_message(self, message: Message<'static>) -> Self
pub fn add_message(self, message: Message<'static>) -> Self
Adds a message to the DBC file.
§Examples
use dbc_rs::{DbcBuilder, MessageBuilder};
let message = MessageBuilder::new()
.id(256)
.name("EngineData")
.dlc(8)
.sender("ECM")
.build()?;
let builder = DbcBuilder::new(None)
.add_message(message);Sourcepub fn add_messages(
self,
messages: impl IntoIterator<Item = Message<'static>>,
) -> Self
pub fn add_messages( self, messages: impl IntoIterator<Item = Message<'static>>, ) -> Self
Adds multiple messages to the DBC file.
§Examples
use dbc_rs::{DbcBuilder, MessageBuilder};
let msg1 = MessageBuilder::new().id(256).name("Msg1").dlc(8).sender("ECM").build()?;
let msg2 = MessageBuilder::new().id(512).name("Msg2").dlc(4).sender("TCM").build()?;
let builder = DbcBuilder::new(None)
.add_message(msg1)
.add_message(msg2);Sourcepub fn messages(self, messages: Vec<Message<'static>>) -> Self
pub fn messages(self, messages: Vec<Message<'static>>) -> Self
Sets all messages for the DBC file, replacing any existing messages.
§Examples
use dbc_rs::{DbcBuilder, MessageBuilder};
let msg = MessageBuilder::new().id(256).name("Msg1").dlc(8).sender("ECM").build()?;
let builder = DbcBuilder::new(None)
.add_message(msg);Sourcepub fn clear_messages(self) -> Self
pub fn clear_messages(self) -> Self
Clears all messages from the builder.
§Examples
use dbc_rs::DbcBuilder;
let builder = DbcBuilder::new(None)
.clear_messages();Sourcepub fn validate(self) -> Result<Self>
pub fn validate(self) -> Result<Self>
Validates the builder without constructing the Dbc.
This method performs all validation checks but returns the builder
instead of constructing the Dbc. Useful for checking if the builder
is valid before calling build().
§Examples
use dbc_rs::DbcBuilder;
let builder = DbcBuilder::new(None);
if builder.validate().is_err() {
// Handle validation error
}Sourcepub fn build(self) -> Result<Dbc<'static>>
pub fn build(self) -> Result<Dbc<'static>>
Builds the Dbc from the builder.
This method validates all fields and constructs the Dbc instance.
Returns an error if validation fails.
§Examples
use dbc_rs::{DbcBuilder, VersionBuilder, NodesBuilder};
let dbc = DbcBuilder::new(None)
.version(VersionBuilder::new().version("1.0").build()?)
.nodes(NodesBuilder::new().add_node("ECM").build()?)
.build()?;