rosbags-rs

Crates.iorosbags-rs
lib.rsrosbags-rs
version0.3.4
created_at2025-06-07 20:37:06.991716+00
updated_at2025-06-07 20:40:22.155802+00
descriptionA high-performance Rust library for reading ROS2 bag files with full Python rosbags compatibility
homepagehttps://github.com/amin-abouee/rosbags-rs
repositoryhttps://github.com/amin-abouee/rosbags-rs
max_upload_size
id1704399
size688,533
Amin Abouee (amin-abouee)

documentation

https://docs.rs/rosbags-rs

README

rosbags-rs

CI Crates.io Documentation License

A high-performance Rust library for reading ROS2 bag files with full Python rosbags compatibility. This library provides comprehensive functionality to read ROS2 bag files in both SQLite3 and MCAP formats, with guaranteed byte-for-byte identical results compared to the Python rosbags library.

πŸš€ Features

  • βœ… Complete ROS2 bag reading - SQLite3 and MCAP formats
  • βœ… 94+ ROS2 message types - Full support across all major categories
  • βœ… Python rosbags compatibility - Byte-for-byte identical results
  • βœ… High performance - Zero-copy message reading where possible
  • βœ… Comprehensive CDR deserialization - All standard ROS2 message types
  • βœ… Advanced filtering - By topic, time range, and message type
  • βœ… Compression support - zstd compressed bags
  • βœ… Type-safe error handling - Comprehensive error types
  • βœ… Self-contained tests - No external dependencies required
  • βœ… Production ready - Extensive test coverage and CI/CD

🎯 Supported ROS2 Versions

  • ROS2 Jazzy Jalopy (LTS)
  • ROS2 Humble Hawksbill (LTS)
  • ROS2 Foxy Fitzroy (LTS)

πŸ“¦ Installation

Add this to your Cargo.toml:

[dependencies]
rosbags-rs = "0.2.0"

πŸš€ Quick Start

use rosbags_rs::{Reader, ReaderError};
use std::path::Path;

fn main() -> Result<(), ReaderError> {
    // Open a ROS2 bag
    let bag_path = Path::new("/path/to/rosbag2_directory");
    let mut reader = Reader::new(bag_path)?;
    reader.open()?;

    // Print basic information
    println!("Duration: {:.2} seconds", reader.duration() as f64 / 1_000_000_000.0);
    println!("Messages: {}", reader.message_count());

    // List topics with detailed information
    for topic in reader.topics() {
        println!("πŸ“‘ Topic: {}", topic.name);
        println!("   Type: {}", topic.message_type);
        println!("   Messages: {}", topic.message_count);
        println!("   Serialization: {}", topic.serialization_format);
    }

    // Read all messages
    for message_result in reader.messages()? {
        let message = message_result?;
        println!("πŸ“¨ Topic: {}, Timestamp: {}, Size: {} bytes",
                 message.topic, message.timestamp, message.data.len());
    }

    Ok(())
}

πŸ“š Examples

Basic Bag Information

cargo run --example bag_info /path/to/rosbag2_directory

List All Topics

cargo run --example list_topics /path/to/rosbag2_directory

For detailed topic information:

VERBOSE=1 cargo run --example list_topics /path/to/rosbag2_directory

Extract Specific Topic

cargo run --example extract_topic /path/to/rosbag2_directory /topic_name output.txt

Read Test Bags (Demo)

cargo run --example read_test_bags

πŸ—‚οΈ Supported ROS2 Bag Formats

Storage Formats

  • βœ… SQLite3 - Primary storage format for ROS2 bags
  • βœ… MCAP - Modern container format with high performance

Compression

  • βœ… None - Uncompressed bags
  • βœ… zstd - File-level and message-level compression
  • ❌ lz4 - Not currently supported

Bag Versions

  • βœ… Version 1-9 - All current ROS2 bag versions supported

πŸ—οΈ Architecture

The library is structured into several modules:

  • reader - Main Reader struct for opening and reading bags
  • metadata - Parsing and validation of metadata.yaml files
  • storage - Storage backend implementations (SQLite3, MCAP)
  • types - Core data structures (Connection, Message, TopicInfo, etc.)
  • error - Comprehensive error handling
  • cdr - CDR message deserialization
  • messages - ROS2 message type definitions

πŸ›‘οΈ Error Handling

The library uses the thiserror crate for structured error handling:

use rosbags_rs::{Reader, ReaderError};

match Reader::new("/path/to/bag") {
    Ok(reader) => { /* success */ },
    Err(ReaderError::BagNotFound { path }) => {
        eprintln!("Bag not found: {}", path.display());
    },
    Err(ReaderError::UnsupportedVersion { version }) => {
        eprintln!("Unsupported bag version: {}", version);
    },
    Err(e) => {
        eprintln!("Other error: {}", e);
    }
}

πŸ” Advanced Usage

Filter Messages by Topic

use rosbags_rs::Reader;

let mut reader = Reader::new("/path/to/bag")?;
reader.open()?;

// Filter messages for specific topics
let target_topics = vec!["/camera/image_raw", "/imu/data"];
for message_result in reader.messages()? {
    let message = message_result?;
    if target_topics.contains(&message.topic.as_str()) {
        println!("Found message on topic: {}", message.topic);
    }
}

Filter by Time Range

// Filter by time range (nanoseconds since epoch)
let start_time = 1234567890000000000;
let end_time = 1234567891000000000;

for message_result in reader.messages()? {
    let message = message_result?;
    if message.timestamp >= start_time && message.timestamp <= end_time {
        println!("Message in time range: {}", message.timestamp);
    }
}

πŸ“Š Supported ROS2 Message Types

This library supports 94+ ROS2 message types across all major categories:

Core Message Categories

  • πŸ“‘ std_msgs - Standard message types (String, Header, etc.)
  • πŸ“ geometry_msgs - Geometric primitives (Point, Pose, Transform, etc.)
  • πŸ€– sensor_msgs - Sensor data (Image, PointCloud2, Imu, NavSatFix, etc.)
  • πŸ—ΊοΈ nav_msgs - Navigation messages (Odometry, Path, etc.)
  • πŸ”§ diagnostic_msgs - System diagnostics
  • ⏰ builtin_interfaces - Time and duration types

Cross-Compatibility Guarantee

This Rust implementation provides 100% compatibility with the Python rosbags library:

Feature Python rosbags rosbags-rs
SQLite3 reading βœ… βœ…
MCAP reading βœ… βœ…
CDR deserialization βœ… βœ…
Message filtering βœ… βœ…
Compression support βœ… βœ…
Type safety ❌ βœ…
Memory safety ❌ βœ…
Performance Good Excellent
Cross-validation N/A Byte-for-byte identical

πŸš€ Performance

  • Zero-copy message reading where possible
  • Efficient memory usage with Rust's ownership system
  • Fast SQLite3 and MCAP parsing with optimized queries
  • Comprehensive benchmarks included in test suite

πŸ§ͺ Testing

The library includes a comprehensive, self-contained test suite:

Run All Tests

cargo test

Run Specific Test Categories

# Integration tests with cross-validation
cargo test --test integration_tests

# Performance tests
cargo test --release

# Feature-specific tests
cargo test --features sqlite
cargo test --features mcap

Test Coverage

The test suite validates:

  • βœ… All 94 message types across 6 categories
  • βœ… 188 test messages (2 per topic) in both SQLite3 and MCAP formats
  • βœ… Byte-for-byte compatibility with Python rosbags library
  • βœ… Message filtering by topic and timestamp
  • βœ… Metadata consistency between formats
  • βœ… Error handling for edge cases

All tests are self-contained and run without external dependencies or Python installations.

🀝 Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

Development Setup

  1. Clone the repository

    git clone https://github.com/amin-abouee/rosbags-rs.git
    cd rosbags-rs
    
  2. Install Rust (1.70+ required)

    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    
  3. Run tests

    cargo test
    
  4. Run examples

    cargo run --example bag_info /path/to/bag
    
  5. Check formatting and linting

    cargo fmt --check
    cargo clippy -- -D warnings
    

Contribution Guidelines

  • Follow Rust best practices and idioms
  • Add tests for new functionality
  • Update documentation for API changes
  • Ensure CI passes on all platforms

πŸ“„ License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Inspired by the excellent rosbags Python library by Ternaris
  • Built for the ROS2 robotics ecosystem
  • Thanks to all contributors and the Rust community
Commit count: 25

cargo fmt