Skip to content

Commit

Permalink
add drop for writer
Browse files Browse the repository at this point in the history
  • Loading branch information
NerdToMars committed Dec 26, 2023
1 parent 3876292 commit e73c49a
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 53 deletions.
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Changelog

All notable changes to the ROS bag reader and writer project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.1] - 2023-12-26

### Added

- Initial implementation of the ROS bag writer in Rust, including:
- Basic structure setup for the `Writer` struct.
- Implementation of the `add_connection` and `write` methods.
- Initial setup of the ROS bag reader in Rust, including:
- Basic structure for the `Reader` struct.
- Implementation of the `open` method for the `Reader` struct to initialize storage based on metadata.

### Changed

- Refinement and updates to error handling mechanisms in both reader and writer components.
- Various optimizations and code cleanups to improve performance and readability.
- Add Drop for `Writer` to create metadata.yaml when Writer dropped.

## [0.1.0] - 2023-12-23

- Initial release of the project.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rosbag2-rs"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
description = "Rosbag2 writer and more..."
authors = ["CT <ctian.huang@gmail.com>"]
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Rosbag2 Rust

Rosbag2 Rust is a Rust crate designed to provide functionalities for handling ROS2 bag files. This crate aims to enable efficient reading, writing, and manipulation of ROS bag files, making it easier for developers working with ROS to manage and analyze their data.
Rosbag2 Rust (Rosbag2 rs) is a Rust crate designed to provide functionalities for handling ROS2 bag files. This crate aims to enable efficient reading, writing, and manipulation of ROS bag files, making it easier for developers working with ROS to manage and analyze their data.

[![CI][actions-badge]][actions-url]
[![Crates.io][crates-badge]][crates-url]
Expand All @@ -14,12 +14,12 @@ Rosbag2 Rust is a Rust crate designed to provide functionalities for handling RO

### Current Features

- [ ] Write ROS Bag Files (in progress, only support version 5 rosbag2 (humble))
- [ ] Read ROS Bag Files
- [x] Write ROS Bag Files (in progress, only support version 5 rosbag2 (humble))
- [x] Read ROS Bag Files

### Planned Features

- Read ROS Bag Files
- Support read from split db files
- Advanced message manipulation tools
- ...

Expand Down
43 changes: 43 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
///
/// # Usage
///
/// ```
/// use std::rc::Rc;
/// use std::cell::RefCell;
/// use rosbag2_rs::{Reader, Writer};
/// use anyhow::Result;
/// use tempfile::tempdir;

/// fn main() -> Result<()> {
/// let dir = tempdir()?;
/// let mut writer = Writer::new(dir.path());
/// writer.open()?;
///
/// let connection = writer.add_connection("topic1", "msgtype1", "cdr", "")?;
/// for i in 0..10 {
/// writer.write(&connection, i as i64, &[i * 2 + 1 as u8])?;
/// }
/// let connection = writer.add_connection("topic2", "msgtype2", "cdr", "")?;
/// for i in 0..10 {
/// writer.write(&connection, i as i64, &[i * 2 as u8])?;
/// }
/// writer.close()?;
///
/// let mut reader = Reader::new(dir.path())?;
/// let msg_data: Vec<(i64, i64, Vec<u8>)> = vec![];
/// let msg_data = Rc::new(RefCell::new(msg_data)); // Wrap the vector in Rc and RefCell
/// reader.handle_messages(
/// |(id, timestamp, data)| {
/// // Use `borrow_mut` to get a mutable reference to the vector
/// println!("processed message: {:?} {:?} {:?}", id, timestamp, data);
///
/// msg_data.borrow_mut().push((id, timestamp, data));
/// Ok(())
/// },
/// None,
/// None,
/// )?;
///
/// Ok(())
/// }
/// ```
pub mod metadata;
pub use metadata::*;

Expand Down
45 changes: 1 addition & 44 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,7 @@ pub struct Reader {
/// The `Reader` initializes with the path to a ROS bag directory and reads metadata
/// and message data from the storage. It supports filtering messages by time and handling
/// each message through a user-defined function.
///
/// # Usage
///
/// ```
/// use std::rc::Rc;
/// use std::cell::RefCell;
/// use rosbag2_rs::{Reader, Writer};
/// use anyhow::Result;
/// use tempfile::tempdir;

/// fn main() -> Result<()> {
/// let dir = tempdir()?;
/// # let mut writer = Writer::new(dir.path());
/// # writer.open()?;
/// # let connection = writer.add_connection("topic1", "msgtype1", "cdr", "")?;

/// # for i in 0..10 {
/// # writer.write(&connection, i as i64, &[i * 2 + 1 as u8])?;
/// # }
/// # let connection = writer.add_connection("topic2", "msgtype2", "cdr", "")?;

/// # for i in 0..10 {
/// # writer.write(&connection, i as i64, &[i * 2 as u8])?;
/// # }
/// # writer.close()?;

/// let mut reader = Reader::new(dir.path())?;
/// let msg_data: Vec<(i64, i64, Vec<u8>)> = vec![];
/// let msg_data = Rc::new(RefCell::new(msg_data)); // Wrap the vector in Rc and RefCell
/// reader.handle_messages(
/// |(id, timestamp, data)| {
/// // Use `borrow_mut` to get a mutable reference to the vector
/// println!("processed message: {:?} {:?} {:?}", id, timestamp, data);
///
/// msg_data.borrow_mut().push((id, timestamp, data));
/// Ok(())
/// },
/// None,
/// None,
/// )?;
///
/// Ok(())
/// }
/// ```

///
/// # Errors
///
Expand Down
10 changes: 8 additions & 2 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use std::collections::HashMap;
use std::fs::File;
use std::path::{Path, PathBuf};

/// This class implements writing of rosbag2 files in version 8. It should be
/// used as a contextmanager.
/// This class implements writing of rosbag2 files in version 5
pub struct Writer {
pub path: PathBuf,
pub metapath: PathBuf,
Expand Down Expand Up @@ -55,6 +54,7 @@ impl Writer {
}

std::fs::create_dir_all(&self.path)?;

let conn = Connection::open(&self.dbpath)?;

// TODO: add support for beyond humble ros2bag
Expand Down Expand Up @@ -265,3 +265,9 @@ impl Writer {
})
}
}

impl Drop for Writer {
fn drop(&mut self) {
let _ = self.close();
}
}
4 changes: 2 additions & 2 deletions tests/writer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn test_write_operations() -> Result<()> {
writer.close()?;

// Open the SQLite database to verify the data
let db_conn = Connection::open(writer.dbpath)?;
let db_conn = Connection::open(&writer.dbpath)?;
let mut stmt = db_conn.prepare("SELECT data FROM messages WHERE topic_id = ?")?;
let mut rows = stmt.query([connection.id.to_string().as_str()])?;

Expand All @@ -87,7 +87,7 @@ fn test_write_operations() -> Result<()> {

// also check metadata.yaml
// Read and verify the YAML metadata file
let metadata_contents = fs::read_to_string(writer.metapath)?;
let metadata_contents = fs::read_to_string(&writer.metapath)?;
let bag_info: BagFileInfo = serde_yaml::from_str(&metadata_contents)?;
let metadata = bag_info.rosbag2_bagfile_information;
assert_eq!(metadata.message_count, 10);
Expand Down

0 comments on commit e73c49a

Please sign in to comment.