Crate mcap

Source
Expand description

A library for manipulating Foxglove MCAP files, both reading:

use std::fs;

use anyhow::{Context, Result};
use camino::Utf8Path;
use memmap2::Mmap;

fn map_mcap<P: AsRef<Utf8Path>>(p: P) -> Result<Mmap> {
    let fd = fs::File::open(p.as_ref()).context("Couldn't open MCAP file")?;
    unsafe { Mmap::map(&fd) }.context("Couldn't map MCAP file")
}

fn read_it() -> Result<()> {
    let mapped = map_mcap("in.mcap")?;

    for message in mcap::MessageStream::new(&mapped)? {
        println!("{:?}", message?);
        // Or whatever else you'd like to do...
    }
    Ok(())
}

or writing:

use std::{collections::BTreeMap, fs, io::BufWriter};

use anyhow::Result;

use mcap::{Channel, records::MessageHeader, Writer};

fn write_it() -> Result<()> {
    // To set the profile or compression options, see mcap::WriteOptions.
    let mut out = Writer::new(
        BufWriter::new(fs::File::create("out.mcap")?)
    )?;

    // Channels and schemas are automatically assigned ID as they're serialized,
    // and automatically deduplicated with `Arc` when deserialized.
    let channel_id = out.add_channel(0, "cool stuff", "application/octet-stream", &BTreeMap::new())?;

    out.write_to_known_channel(
        &MessageHeader {
            channel_id,
            sequence: 25,
            log_time: 6,
            publish_time: 24
        },
        &[1, 2, 3]
    )?;
    out.write_to_known_channel(
        &MessageHeader {
            channel_id,
            sequence: 32,
            log_time: 23,
            publish_time: 25
        },
        &[3, 4, 5]
    )?;

    out.finish()?;

    Ok(())
}

Re-exports§

pub use read::parse_record;
pub use read::MessageStream;
pub use read::Summary;
pub use write::WriteOptions;
pub use write::Writer;

Modules§

read
Read MCAP data from a memory-mapped file.
records
Raw records parsed from an MCAP file
sans_io
Read MCAP files from any source of bytes
write
Write MCAP files

Structs§

Attachment
An attachment and its metadata in an MCAP file
Channel
Describes a channel which Messages are published to in an MCAP file
Message
An event in an MCAP file, published to a Channel
Schema
Describes a schema used by one or more Channels in an MCAP file

Enums§

Compression
Compression options for chunks of channels, schemas, and messages in an MCAP file
McapError

Constants§

MAGIC
Magic bytes for the MCAP format

Type Aliases§

McapResult