Expand description
§mdf4-rs
A Rust library for reading and writing ASAM MDF 4 (Measurement Data Format) files.
MDF4 is a binary file format standardized by ASAM for storing measurement data, commonly used in automotive and industrial applications for recording sensor data, CAN bus messages, and other time-series measurements.
§Features
- 100% safe Rust -
#![forbid(unsafe_code)] - no_std support - Works on embedded targets with
alloc - Reading (std only): Parse MDF4 files and access channel data
- Writing: Create new MDF4 files (works with
alloconly) - Indexing (std only): Generate lightweight JSON indexes
- Cutting (std only): Extract time-based segments from recordings
- Merging (std only): Combine multiple MDF files
§Feature Flags
| Feature | Default | Description |
|---|---|---|
std | Yes | Full std library support. Enables file I/O, indexing, merging. |
alloc | Yes | Heap allocation. Required for all functionality. |
§no_std Usage
For embedded targets, disable default features and enable alloc:
[dependencies]
mdf4-rs = { version = "0.1", default-features = false, features = ["alloc"] }With alloc only, you can:
- Create MDF files in memory using
MdfWriter::from_writer() - Serialize blocks to byte vectors
- Use all block types and data encoding
§Quick Start
§Reading an MDF file
use mdf4_rs::{MDF, Result};
fn main() -> Result<()> {
let mdf = MDF::from_file("recording.mf4")?;
for group in mdf.channel_groups() {
println!("Group: {:?}", group.name()?);
for channel in group.channels() {
let name = channel.name()?.unwrap_or_default();
let values = channel.values()?;
let valid_count = values.iter().filter(|v| v.is_some()).count();
println!(" {}: {} valid samples", name, valid_count);
}
}
Ok(())
}§Writing an MDF file
ⓘ
use mdf4_rs::{MdfWriter, DataType, DecodedValue, Result};
fn main() -> Result<()> {
let mut writer = MdfWriter::new("output.mf4")?;
writer.init_mdf_file()?;
// Create a channel group
let cg = writer.add_channel_group(None, |_| {})?;
// Add a temperature channel
writer.add_channel(&cg, None, |ch| {
ch.data_type = DataType::FloatLE;
ch.name = Some("Temperature".into());
ch.bit_count = 64;
})?;
// Write data records
writer.start_data_block_for_cg(&cg, 0)?;
for temp in [20.5, 21.0, 21.5, 22.0] {
writer.write_record(&cg, &[DecodedValue::Float(temp)])?;
}
writer.finish_data_block(&cg)?;
writer.finalize()?;
Ok(())
}§Using the Index for Efficient Access
use mdf4_rs::{MdfIndex, FileRangeReader, Result};
fn main() -> Result<()> {
// Create an index from a file
let index = MdfIndex::from_file("recording.mf4")?;
// Save index for later use
index.save_to_file("recording.mdf4.index")?;
// Load index and read specific channel
let index = MdfIndex::load_from_file("recording.mdf4.index")?;
let mut reader = FileRangeReader::new("recording.mf4")?;
let values = index.read_channel_values_by_name("Temperature", &mut reader)?;
println!("Read {} values", values.len());
Ok(())
}§Module Overview
| Module | Description | Requires |
|---|---|---|
blocks | Low-level MDF block structures | alloc |
writer | MDF file creation | alloc |
parsing | File parsing utilities | std |
index | File indexing | std |
cut | Time-based segment extraction | std |
merge | File merging utilities | std |
error | Error types and Result alias | alloc |
§Error Handling
All fallible operations return Result<T>, which is an alias for
core::result::Result<T, Error>. The Error enum covers I/O errors,
parsing failures, and invalid file structures.
Re-exports§
pub use blocks::DataType;pub use error::Error;pub use error::Result;pub use writer::MdfWriter;pub use writer::FlushPolicy;pub use writer::StreamingConfig;pub use cut::cut_mdf_by_time;pub use index::BufferedRangeReader;pub use index::ByteRangeReader;pub use index::FileRangeReader;pub use index::MdfIndex;pub use merge::merge_files;
Modules§
- blocks
- can
- CAN bus integration for MDF4 files.
- cut
- error
- Error types for MDF4 operations.
- index
- MDF File Indexing System
- merge
- parsing
- writer
- MDF4 file writer module.
Structs§
- Channel
- High level handle for a single channel within a group.
- Channel
Group - High level wrapper for a channel group.
- MDF
- High level representation of an MDF file.
Enums§
- Decoded
Value - An enum representing the decoded value of a channel sample.