use std::path::PathBuf;
use std::time::Duration;
use ros2kit::{Env, RecordConfig, Recorder, TopicRecording};
use tokio::sync::mpsc;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let output_path = std::env::args()
.nth(1)
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("output.mcap"));
let env = Env::from_env()?;
let (tx, rx) = mpsc::channel::<Vec<u8>>(64);
let config = RecordConfig {
output_path: output_path.clone(),
topics: vec![TopicRecording {
topic: "/chatter".to_string(),
type_name: "std_msgs/String".to_string(),
rx,
}],
};
let mut recorder = Recorder::new();
let result = recorder.start(config, &env)?;
if !result.skipped_topics.is_empty() {
eprintln!("Skipped topics (unresolvable): {:?}", result.skipped_topics);
}
let mut stats_rx = result.stats_rx;
let producer = tokio::spawn(async move {
for i in 0..10 {
let msg = format!("hello {i}");
let data = msg.into_bytes();
if tx.send(data).await.is_err() {
break;
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
});
producer.await?;
recorder.stop();
if let Ok(true) = stats_rx.has_changed() {
let stats = stats_rx.borrow_and_update();
eprintln!(
"Recorded {} messages ({} bytes) in {:.1?}",
stats.message_count, stats.bytes_written, stats.duration
);
}
eprintln!("Written to {}", output_path.display());
Ok(())
}