Skip to content

Commit

Permalink
Merge pull request #56 from NREL/to-file-unwrap-error
Browse files Browse the repository at this point in the history
Replace Option::unwrap() with Option::ok_or_else() with descriptive message
  • Loading branch information
calbaker authored Oct 24, 2023
2 parents 0559171 + 8a2b639 commit 1bef634
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions rust/fastsim-core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> {
/// # Returns:
///
/// A Rust Result
fn to_file(&self, filename: &str) -> Result<(), anyhow::Error> {
fn to_file(&self, filename: &str) -> anyhow::Result<()> {
let file = PathBuf::from(filename);
match file.extension().unwrap().to_str().unwrap() {
match file
.extension()
.ok_or_else(|| anyhow!("Unable to parse file extension: {:?}", file))?
.to_str()
.ok_or_else(|| {
anyhow!(
"Unable to convert file extension from `&OsStr` to `&str`: {:?}",
file
)
})? {
"json" => serde_json::to_writer(&File::create(file)?, self)?,
"yaml" => serde_yaml::to_writer(&File::create(file)?, self)?,
_ => serde_json::to_writer(&File::create(file)?, self)?,
Expand Down

0 comments on commit 1bef634

Please sign in to comment.