Dbc

Struct Dbc 

Source
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>

Source

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() {
    // Version is available
    let _ = version.as_str();
}
Source

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();
assert_eq!(nodes.len(), 2);
// Iterate over nodes
let mut iter = nodes.iter();
assert_eq!(iter.next(), Some("ECM"));
assert_eq!(iter.next(), Some("TCM"));
assert_eq!(iter.next(), None);
Source

pub fn messages(&self) -> &MessageList<'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();
assert_eq!(messages.len(), 1);
let message = messages.at(0).unwrap();
assert_eq!(message.name(), "Engine");
assert_eq!(message.id(), 256);
Source

pub fn value_descriptions(&self) -> &ValueDescriptionsList<'a>

Get value descriptions for a specific signal

Value descriptions map numeric signal values to human-readable text. Returns None if the signal has no value descriptions.

Global Value Descriptions: According to the Vector DBC specification, a message_id of -1 (0xFFFFFFFF) in a VAL_ statement means the value descriptions apply to all signals with that name in ANY message. This method will first check for a message-specific entry, then fall back to a global entry if one exists.

§Examples
if let Some(value_descriptions) = dbc.value_descriptions_for_signal(100, "Gear") {
    if let Some(desc) = value_descriptions.get(0) {
        println!("Value 0 means: {}", desc);
    }
}

Get a reference to the value descriptions list

§Examples
use dbc_rs::Dbc;

let dbc = Dbc::parse(r#"VERSION "1.0"

BU_: ECM

BO_ 100 Engine : 8 ECM
 SG_ Gear : 0|8@1+ (1,0) [0|5] "" *

VAL_ 100 Gear 0 "Park" 1 "Drive" ;"#)?;
let value_descriptions_list = dbc.value_descriptions();
assert_eq!(value_descriptions_list.len(), 1);
Source

pub fn value_descriptions_for_signal( &self, message_id: u32, signal_name: &str, ) -> Option<&ValueDescriptions<'a>>

Source

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)?;
assert_eq!(dbc.messages().len(), 1);
Source

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 string
  • options - 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)?;
Source

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());
Source

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());
Source

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());
Source

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§

Source§

impl<'a> Debug for Dbc<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Display for Dbc<'a>

Available on crate feature alloc only.
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.