Skip to content

Commit 5e20fb3

Browse files
Copilotjbrodovsky
andcommitted
Address code review feedback - improve error handling and empty dataset handling
Co-authored-by: jbrodovsky <57160841+jbrodovsky@users.noreply.github.com>
1 parent 87d4469 commit 5e20fb3

1 file changed

Lines changed: 54 additions & 12 deletions

File tree

core/src/sim.rs

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,24 @@ impl TestDataRecord {
325325
let file = File::create(path)?;
326326
let n = records.len();
327327

328+
// Handle empty datasets
329+
if n == 0 {
330+
// Create group to indicate structure even for empty datasets
331+
let _group = file.create_group("test_data")?;
332+
return Ok(());
333+
}
334+
328335
// Create a group for test data records
329336
let group = file.create_group("test_data")?;
330337

331338
// Write timestamps as strings
332-
let timestamps: Vec<hdf5::types::VarLenAscii> = records.iter()
333-
.map(|r| hdf5::types::VarLenAscii::from_ascii(&r.time.to_rfc3339()).unwrap())
339+
let timestamps: Result<Vec<hdf5::types::VarLenAscii>> = records.iter()
340+
.map(|r| {
341+
hdf5::types::VarLenAscii::from_ascii(&r.time.to_rfc3339())
342+
.map_err(|e| anyhow::anyhow!("Failed to encode timestamp as ASCII: {}", e))
343+
})
334344
.collect();
345+
let timestamps = timestamps?;
335346
let ds_time = group.new_dataset::<hdf5::types::VarLenAscii>()
336347
.shape([n])
337348
.create("time")?;
@@ -403,10 +414,16 @@ impl TestDataRecord {
403414
let file = File::open(path)?;
404415
let group = file.group("test_data")?;
405416

406-
// Read timestamps
407-
let ds_time = group.dataset("time")?;
408-
let timestamps: Vec<hdf5::types::VarLenAscii> = ds_time.read_raw()?;
409-
let n = timestamps.len();
417+
// Check if time dataset exists (might be empty dataset)
418+
if let Ok(ds_time) = group.dataset("time") {
419+
// Read timestamps
420+
let timestamps: Vec<hdf5::types::VarLenAscii> = ds_time.read_raw()?;
421+
let n = timestamps.len();
422+
423+
// Handle empty dataset
424+
if n == 0 {
425+
return Ok(Vec::new());
426+
}
410427

411428
// Helper macro to read f64 arrays
412429
macro_rules! read_f64_field {
@@ -490,6 +507,10 @@ impl TestDataRecord {
490507
}
491508

492509
Ok(records)
510+
} else {
511+
// No time dataset means empty file
512+
Ok(Vec::new())
513+
}
493514
}
494515
}
495516
impl Display for TestDataRecord {
@@ -700,13 +721,24 @@ impl NavigationResult {
700721
let file = File::create(path)?;
701722
let n = records.len();
702723

724+
// Handle empty datasets
725+
if n == 0 {
726+
// Create group to indicate structure even for empty datasets
727+
let _group = file.create_group("navigation_results")?;
728+
return Ok(());
729+
}
730+
703731
// Create a group for navigation results
704732
let group = file.create_group("navigation_results")?;
705733

706734
// Write timestamps as strings
707-
let timestamps: Vec<hdf5::types::VarLenAscii> = records.iter()
708-
.map(|r| hdf5::types::VarLenAscii::from_ascii(&r.timestamp.to_rfc3339()).unwrap())
735+
let timestamps: Result<Vec<hdf5::types::VarLenAscii>> = records.iter()
736+
.map(|r| {
737+
hdf5::types::VarLenAscii::from_ascii(&r.timestamp.to_rfc3339())
738+
.map_err(|e| anyhow::anyhow!("Failed to encode timestamp as ASCII: {}", e))
739+
})
709740
.collect();
741+
let timestamps = timestamps?;
710742
let ds_time = group.new_dataset::<hdf5::types::VarLenAscii>()
711743
.shape([n])
712744
.create("timestamp")?;
@@ -781,10 +813,16 @@ impl NavigationResult {
781813
let file = File::open(path)?;
782814
let group = file.group("navigation_results")?;
783815

784-
// Read timestamps
785-
let ds_time = group.dataset("timestamp")?;
786-
let timestamps: Vec<hdf5::types::VarLenAscii> = ds_time.read_raw()?;
787-
let n = timestamps.len();
816+
// Check if timestamp dataset exists (might be empty dataset)
817+
if let Ok(ds_time) = group.dataset("timestamp") {
818+
// Read timestamps
819+
let timestamps: Vec<hdf5::types::VarLenAscii> = ds_time.read_raw()?;
820+
let n = timestamps.len();
821+
822+
// Handle empty dataset
823+
if n == 0 {
824+
return Ok(Vec::new());
825+
}
788826

789827
// Helper macro to read f64 arrays
790828
macro_rules! read_f64_field {
@@ -871,6 +909,10 @@ impl NavigationResult {
871909
}
872910

873911
Ok(records)
912+
} else {
913+
// No timestamp dataset means empty file
914+
Ok(Vec::new())
915+
}
874916
}
875917
}
876918
/// Convert DVectors containing the navigation state mean and covariance into a NavigationResult

0 commit comments

Comments
 (0)