A high-performance Rust library for reading and writing ROS2 bag files with full Python rosbags compatibility. This library provides comprehensive functionality to read and write ROS2 bag files in both SQLite3 and MCAP formats, with guaranteed byte-for-byte identical results compared to the Python rosbags library.
- โ Complete ROS2 bag reading and writing - 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
- ROS2 Jazzy Jalopy (LTS)
- ROS2 Humble Hawksbill (LTS)
- ROS2 Foxy Fitzroy (LTS)
Add this to your Cargo.toml:
[dependencies]
rosbags-rs = "0.2.0"This library includes several command-line utilities for working with ROS2 bag files:
High-performance bag copying with topic and time filtering:
# Copy entire bag
cargo run --bin bag_filter -- /path/to/input_bag /path/to/output_bag
# Copy specific topics only
cargo run --bin bag_filter -- /path/to/input_bag /path/to/output_bag --topics /imu/data,/camera/image_raw
# Copy with time filtering
cargo run --bin bag_filter -- /path/to/input_bag /path/to/output_bag --start 1000000000 --end 2000000000
# List available topics
cargo run --bin bag_filter -- /path/to/input_bag /path/to/output_bag --list-topics
# Use verbose output
cargo run --bin bag_filter -- /path/to/input_bag /path/to/output_bag --verboseShow metadata and statistics about bag files:
cargo run --bin bag_info -- /path/to/rosbag2_directoryExtract specific topic data and save to appropriate file formats:
cargo run --bin extract_topic_data -- /path/to/bag /topic_name /output/directoryGenerate test bag files with sample data for testing:
cargo run --bin write_dummy_bag -- /path/to/output_bag- โ SQLite3 - Primary storage format for ROS2 bags
- โ MCAP - Modern container format with high performance
- โ None - Uncompressed bags
- โ zstd - File-level and message-level compression
- โ lz4 - Not currently supported
- โ Version 1-9 - All current ROS2 bag versions supported
The library is structured into several modules:
reader- MainReaderstruct for opening and reading bagswriter- MainWriterstruct for creating and writing bagsmetadata- Parsing and validation ofmetadata.yamlfilesstorage- Storage backend implementations (SQLite3, MCAP)types- Core data structures (Connection, Message, TopicInfo, etc.)error- Comprehensive error handlingcdr- CDR message deserializationmessages- ROS2 message type definitions
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);
}
}This library supports 94+ ROS2 message types across all major 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
This Rust implementation provides 100% compatibility with the Python rosbags library:
| Feature | Python rosbags | rosbags-rs |
|---|---|---|
| SQLite3 reading | โ | โ |
| MCAP reading | โ | โ |
| SQLite3 writing | โ | โ |
| MCAP writing | โ | โ |
| CDR deserialization | โ | โ |
| Message filtering | โ | โ |
| Compression support | โ | โ |
| Type safety | โ | โ |
| Memory safety | โ | โ |
| Performance | Good | Excellent |
| Cross-validation | N/A | Byte-for-byte identical |
- Zero-copy message reading where possible
- Optimized SQL queries for SQLite3 backend
- SIMD-accelerated parsing for MCAP backend (future work)
- Lazy-loading of message data - only read what you need
- Minimal memory allocations - focus on performance and efficiency
- Bulk operations - Batch reading and writing for maximum throughput
The library is designed for high-throughput applications where performance is critical. The bag_filter tool uses optimized raw copying by default, similar to ros2 bag convert, for maximum speed.
This library includes a comprehensive test suite that validates correctness against the Python rosbags library. All tests are self-contained and do not require an external ROS2 installation.
cargo test -- --nocaptureThe tests cover:
- Unit tests for individual modules
- Integration tests for reading complete bag files
- Compatibility tests to ensure byte-for-byte identical results with Python
rosbags - Fuzz testing to uncover edge cases and potential panics
The test bags are generated using the generate_test_bags.py script and are included in the repository.
Contributions are welcome! Please feel free to submit a pull request or open an issue.
- Clone the repository
- Install Rust:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh - Build the project:
cargo build - Run tests:
cargo test
This project uses rustfmt for code formatting and clippy for linting. Please ensure your contributions are formatted and free of warnings.
cargo fmt
cargo clippy -- -D warningsThis project is licensed under the Apache 2.0 License - see the LICENSE file for details.