pub struct Dbc<'a> { /* private fields */ }Expand description
Represents a complete DBC (CAN database) file.
A Dbc contains:
- An optional version string
- A list of nodes (ECUs)
- A collection of messages with their signals
§Examples
use dbc_rs::Dbc;
let dbc_content = r#"VERSION "1.0"
BU_: ECM TCM
BO_ 256 EngineData : 8 ECM
SG_ RPM : 0|16@0+ (0.25,0) [0|8000] "rpm" TCM
"#;
let dbc = Dbc::parse(dbc_content)?;
println!("Parsed {} messages", dbc.messages().len());Implementations§
Source§impl<'a> Dbc<'a>
impl<'a> Dbc<'a>
Sourcepub fn version(&self) -> Option<&Version<'a>>
pub fn version(&self) -> Option<&Version<'a>>
Get the version of the DBC file
§Examples
use dbc_rs::Dbc;
let dbc = Dbc::parse("VERSION \"1.0\"\n\nBU_: ECM\n\nBO_ 256 Engine : 8 ECM")?;
if let Some(version) = dbc.version() {
println!("Version: {}", version.to_string());
}Sourcepub fn nodes(&self) -> &Nodes<'a>
pub fn nodes(&self) -> &Nodes<'a>
Get a reference to the nodes collection
§Examples
use dbc_rs::Dbc;
let dbc = Dbc::parse("VERSION \"1.0\"\n\nBU_: ECM TCM\n\nBO_ 256 Engine : 8 ECM")?;
let nodes = dbc.nodes();
println!("Nodes: {}", nodes.to_string());
println!("Node count: {}", nodes.len());Sourcepub fn messages(&self) -> &Messages<'a>
pub fn messages(&self) -> &Messages<'a>
Get a reference to the messages collection
§Examples
use dbc_rs::Dbc;
let dbc = Dbc::parse("VERSION \"1.0\"\n\nBU_: ECM\n\nBO_ 256 Engine : 8 ECM")?;
let messages = dbc.messages();
println!("Message count: {}", messages.len());
for message in messages.iter() {
println!("Message: {} (ID: {})", message.name(), message.id());
}Sourcepub fn parse(data: &'a str) -> Result<Self, ParseError>
pub fn parse(data: &'a str) -> Result<Self, ParseError>
Parse a DBC file from a string slice
§Examples
use dbc_rs::Dbc;
let dbc_content = r#"VERSION "1.0"
BU_: ECM
BO_ 256 EngineData : 8 ECM
SG_ RPM : 0|16@0+ (0.25,0) [0|8000] "rpm""#;
let dbc = Dbc::parse(dbc_content)?;
println!("Parsed {} messages", dbc.messages().len());Sourcepub fn parse_with_options(
data: &'a str,
options: ParseOptions,
) -> Result<Self, ParseError>
pub fn parse_with_options( data: &'a str, options: ParseOptions, ) -> Result<Self, ParseError>
Parses a DBC file from a string with custom parsing options.
§Arguments
data- The DBC file content as a stringoptions- Parsing options to control validation behavior
§Examples
use dbc_rs::{Dbc, ParseOptions};
let dbc_content = r#"VERSION "1.0"
BU_: ECM
BO_ 256 Test : 8 ECM
SG_ Signal1 : 0|8@1+ (1,0) [0|255] ""
"#;
// Use lenient mode to allow signals that extend beyond message boundaries
let options = ParseOptions::lenient();
let dbc = Dbc::parse_with_options(dbc_content, options)?;Sourcepub fn parse_bytes(data: &[u8]) -> Result<Dbc<'static>>
pub fn parse_bytes(data: &[u8]) -> Result<Dbc<'static>>
Parse a DBC file from a byte slice
§Examples
use dbc_rs::Dbc;
let dbc_bytes = b"VERSION \"1.0\"\n\nBU_: ECM\n\nBO_ 256 Engine : 8 ECM";
let dbc = Dbc::parse_bytes(dbc_bytes)?;
println!("Parsed {} messages", dbc.messages().len());Sourcepub fn from_file<P: AsRef<Path>>(path: P) -> Result<Dbc<'static>>
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Dbc<'static>>
Parse a DBC file from a file path
§Examples
use dbc_rs::Dbc;
// Create a temporary file for the example
let dbc_content = r#"VERSION "1.0"
BU_: ECM
BO_ 256 Engine : 8 ECM
SG_ Signal1 : 0|8@1+ (1,0) [0|255] ""
"#;
std::fs::write("/tmp/example.dbc", dbc_content)?;
let dbc = Dbc::from_file("/tmp/example.dbc")?;
println!("Parsed {} messages", dbc.messages().len());Sourcepub fn from_reader<R: Read>(reader: R) -> Result<Dbc<'static>>
pub fn from_reader<R: Read>(reader: R) -> Result<Dbc<'static>>
Parse a DBC file from a reader
§Examples
use dbc_rs::Dbc;
use std::io::Cursor;
let data = b"VERSION \"1.0\"\n\nBU_: ECM\n\nBO_ 256 Engine : 8 ECM";
let reader = Cursor::new(data);
let dbc = Dbc::from_reader(reader)?;
println!("Parsed {} messages", dbc.messages().len());Sourcepub fn to_dbc_string(&self) -> String
pub fn to_dbc_string(&self) -> String
Serialize this DBC to a DBC format string
§Examples
use dbc_rs::Dbc;
let dbc = Dbc::parse("VERSION \"1.0\"\n\nBU_: ECM\n\nBO_ 256 Engine : 8 ECM")?;
let dbc_string = dbc.to_dbc_string();
// The string can be written to a file or used elsewhere
assert!(dbc_string.contains("VERSION"));Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Dbc<'a>
impl<'a> RefUnwindSafe for Dbc<'a>
impl<'a> Send for Dbc<'a>
impl<'a> Sync for Dbc<'a>
impl<'a> Unpin for Dbc<'a>
impl<'a> UnwindSafe for Dbc<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more