use std::collections::HashSet;
use serde::{Deserialize, Serialize};
use crate::{Result, Topic, storage::Db3File};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Duration {
pub nanoseconds: i64,
}
impl Duration {
pub fn new(nanoseconds: i64) -> Self {
Self { nanoseconds }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct StartingTime {
pub nanoseconds_since_epoch: i64,
}
impl StartingTime {
pub fn new(nanoseconds_since_epoch: i64) -> Self {
Self {
nanoseconds_since_epoch,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TopicMetadata {
pub name: String,
#[serde(rename = "type")]
pub r#type: String,
pub serialization_format: String,
pub offered_qos_profiles: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TopicWithMessageCount {
pub topic_metadata: TopicMetadata,
pub message_count: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BagFile {
pub path: String,
pub starting_time: StartingTime,
pub duration: Duration,
pub message_count: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BagfileInformation {
pub version: u32,
pub storage_identifier: String,
pub duration: Duration,
pub starting_time: StartingTime,
pub message_count: u64,
pub topics_with_message_count: Vec<TopicWithMessageCount>,
pub compression_format: String,
pub compression_mode: String,
pub relative_file_paths: Vec<String>,
pub files: Vec<BagFile>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Metadata {
pub rosbag2_bagfile_information: BagfileInformation,
#[serde(skip)]
pub path: std::path::PathBuf,
}
impl Metadata {
pub fn create<'a, P: AsRef<std::path::Path>>(
topics: &HashSet<Topic>,
db3_files: impl Iterator<Item = &'a Db3File>,
path: P,
) -> Result<Self> {
todo!("Assemble BagfileInformation from topics and db3 files")
}
pub fn save<P: AsRef<std::path::Path>>(&self, path: P) -> Result<()> {
todo!("Serialize metadata to YAML and write it to the given path")
}
pub fn to_yaml(&self) -> Result<String> {
Ok(serde_yaml::to_string(self)?)
}
pub fn topics(&self) -> &[TopicWithMessageCount] {
&self.rosbag2_bagfile_information.topics_with_message_count
}
pub fn add_topic(&self, topic: &Topic, message_count: i64) -> Result<()> {
todo!("Append a TopicWithMessageCount entry to topics_with_message_count")
}
}
impl BagfileInformation {
pub fn find_topic(&self, name: &str) -> Option<&TopicMetadata> {
self.topics_with_message_count
.iter()
.find(|t| t.topic_metadata.name == name)
.map(|t| &t.topic_metadata)
}
}
pub const DEFAULT_OFFERED_QOS_PROFILES: &str = "\
- history: 3\n\
depth: 0\n\
reliability: 1\n\
durability: 2\n\
deadline:\n\
sec: 9223372036\n\
nsec: 854775807\n\
lifespan:\n\
sec: 9223372036\n\
nsec: 854775807\n\
liveliness: 1\n\
liveliness_lease_duration:\n\
sec: 9223372036\n\
nsec: 854775807\n\
avoid_ros_namespace_conventions: false";