Skip to content

Commit

Permalink
Try some error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Frits Sweijen committed Jan 11, 2024
1 parent 4f52b66 commit 14ee3a1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.79"
thiserror = "1.0.56"
hdf5 = "0.8.1"
ndarray = "0.15.6"
num-complex = "0.4.4"
Expand Down
36 changes: 29 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use hdf5::file;
use ndarray::{Array1, ArrayD};
use thiserror::Error;
use anyhow::bail;

pub struct H5parm {
pub name: String,
Expand Down Expand Up @@ -42,11 +44,11 @@ impl H5parm {

pub fn getSolSet(&self, ssname: String) -> &SolSet {
let index = self.solsets.iter().position(|r| r.name == ssname).unwrap();
&self.solsets[index]
return &self.solsets[index];
}

pub fn getSolSets(&self) -> &Vec<SolSet> {
&self.solsets
return &self.solsets;
}

pub fn has_solset(&self, ssname: &str) -> bool {
Expand All @@ -59,6 +61,10 @@ impl H5parm {
}
}

#[derive(Debug,Error)]
#[error("No soltab named {0} in h5parm!")]
struct MissingSoltabError(String);

#[derive(Debug)]
pub struct SolSet {
pub name: String,
Expand Down Expand Up @@ -111,19 +117,35 @@ impl SolSet {
soltablist.push(x);
}

SolSet {
return SolSet {
name: name,
soltabs: soltablist,
}
}

pub fn getSolTabs(&self) -> &Vec<SolTab> {
&self.soltabs
return &self.soltabs;
}

pub fn getSolTab(&self, st_name: String) -> Result<&SolTab, anyhow::Error> {
let index: i32 = if self.has_soltab(&st_name) {
self.soltabs.iter().position(|r| r.name == st_name).unwrap().try_into().unwrap()
} else {
-1
};
if index < 0 {
bail!(MissingSoltabError(st_name));
}
return Ok(&self.soltabs[index as usize]);
}

pub fn getSolTab(&self, st_name: String) -> &SolTab {
let index = self.soltabs.iter().position(|r| r.name == st_name).unwrap();
&self.soltabs[index]
pub fn has_soltab(&self, stname: &str) -> bool {
let result = &self.soltabs.iter()
.find(|s| s.name == stname);
return match result {
None => false,
_ => true,
};
}
}

Expand Down

0 comments on commit 14ee3a1

Please sign in to comment.