| Crates.io | rosbags-rs |
| lib.rs | rosbags-rs |
| version | 0.3.4 |
| created_at | 2025-06-07 20:37:06.991716+00 |
| updated_at | 2025-06-07 20:40:22.155802+00 |
| description | A high-performance Rust library for reading ROS2 bag files with full Python rosbags compatibility |
| homepage | https://github.com/amin-abouee/rosbags-rs |
| repository | https://github.com/amin-abouee/rosbags-rs |
| max_upload_size | |
| id | 1704399 |
| size | 688,533 |
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.
Add this to your Cargo.toml:
[dependencies]
rosbags-rs = "0.2.0"
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(())
}
cargo run --example bag_info /path/to/rosbag2_directory
cargo run --example list_topics /path/to/rosbag2_directory
For detailed topic information:
VERBOSE=1 cargo run --example list_topics /path/to/rosbag2_directory
cargo run --example extract_topic /path/to/rosbag2_directory /topic_name output.txt
cargo run --example read_test_bags
The library is structured into several modules:
reader - Main Reader struct for opening and reading bagsmetadata - Parsing and validation of metadata.yaml filesstorage - Storage backend implementations (SQLite3, MCAP)types - Core data structures (Connection, Message, TopicInfo, etc.)error - Comprehensive error handlingcdr - CDR message deserializationmessages - ROS2 message type definitionsThe 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);
}
}
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 (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);
}
}
This library supports 94+ ROS2 message types across all major categories:
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 |
The library includes a comprehensive, self-contained test suite:
cargo test
# 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
The test suite validates:
All tests are self-contained and run without external dependencies or Python installations.
Contributions are welcome! Please feel free to submit issues and pull requests.
Clone the repository
git clone https://github.com/amin-abouee/rosbags-rs.git
cd rosbags-rs
Install Rust (1.70+ required)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Run tests
cargo test
Run examples
cargo run --example bag_info /path/to/bag
Check formatting and linting
cargo fmt --check
cargo clippy -- -D warnings
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.