mod common; use common::*; use std::io::BufWriter; use anyhow::Result; use mcap_rs as mcap; use memmap::Mmap; use tempfile::tempfile; #[test] fn smoke() -> Result<()> { let mapped = map_mcap("tests/references/OneMetadata.mcap")?; let metas = mcap::read::LinearReader::new(&mapped)? .filter_map(|record| match record.unwrap() { mcap::records::Record::Metadata(m) => Some(m), _ => None, }) .collect::>(); assert_eq!(metas.len(), 1); let expected = mcap::records::Metadata { name: String::from("myMetadata"), metadata: [(String::from("foo"), String::from("bar"))].into(), }; assert_eq!(metas[0], expected); Ok(()) } #[test] fn round_trip() -> Result<()> { let mapped = map_mcap("tests/references/OneMetadata.mcap")?; let metas = mcap::read::LinearReader::new(&mapped)?.filter_map(|record| match record.unwrap() { mcap::records::Record::Metadata(m) => Some(m), _ => None, }); let mut tmp = tempfile()?; let mut writer = mcap::Writer::new(BufWriter::new(&mut tmp))?; for m in metas { writer.write_metadata(&m)?; } drop(writer); let ours = unsafe { Mmap::map(&tmp) }?; let summary = mcap::Summary::read(&ours)?; let expected_summary = Some(mcap::Summary { stats: Some(mcap::records::Statistics { metadata_count: 1, ..Default::default() }), metadata_indexes: vec![mcap::records::MetadataIndex { offset: 38, // Finicky - depends on the length of the library version string length: 41, name: String::from("myMetadata"), }], ..Default::default() }); assert_eq!(summary, expected_summary); let expected = mcap::records::Metadata { name: String::from("myMetadata"), metadata: [(String::from("foo"), String::from("bar"))].into(), }; assert_eq!( mcap::read::metadata(&ours, &summary.unwrap().metadata_indexes[0])?, expected ); Ok(()) }