read_from_sources/
read_from_sources.rs

1use dbc_rs::Dbc;
2use std::io::Cursor;
3
4fn main() -> Result<(), dbc_rs::Error> {
5    let dbc_content = r#"VERSION "1.0"
6
7BU_: ECM TCM
8
9BO_ 256 Engine : 8 ECM
10 SG_ RPM : 0|16@1+ (0.25,0) [0|8000] "rpm"
11 SG_ Temp : 16|8@1- (1,-40) [-40|215] "°C"
12
13BO_ 512 Brake : 4 TCM
14 SG_ Pressure : 0|16@1+ (0.1,0) [0|1000] "bar"
15"#;
16
17    // Method 1: Parse from string slice (works in no_std)
18    println!("1. Parsing from &str:");
19    let dbc1 = Dbc::parse(dbc_content)?;
20    println!("   Parsed {} messages", dbc1.messages().len());
21
22    // Method 2: Parse from bytes (works in no_std)
23    println!("2. Parsing from &[u8]:");
24    let bytes = dbc_content.as_bytes();
25    let dbc2 = Dbc::parse_bytes(bytes)?;
26    println!("   Parsed {} messages", dbc2.messages().len());
27
28    // Method 3: Parse from String (works in no_std)
29    println!("3. Parsing from String:");
30    let string = String::from(dbc_content);
31    let dbc3 = Dbc::parse_from(string)?;
32    println!("   Parsed {} messages", dbc3.messages().len());
33
34    // Method 4: Parse from std::io::Read (requires std feature)
35    #[cfg(feature = "std")]
36    {
37        println!("4. Parsing from std::io::Read (Cursor):");
38        let cursor = Cursor::new(dbc_content.as_bytes());
39        let dbc4 = Dbc::from_reader(cursor)?;
40        println!("   Parsed {} messages", dbc4.messages().len());
41
42        // Method 5: Parse from File (requires std feature)
43        println!("5. Parsing from File:");
44        if let Ok(file) = std::fs::File::open("tests/data/simple.dbc") {
45            let dbc5 = Dbc::from_reader(file)?;
46            println!("   Parsed {} messages from file", dbc5.messages().len());
47        } else {
48            println!("   File not found (this is OK for the example)");
49        }
50    }
51
52    Ok(())
53}