From 8ab6016d3b2e3dcb1211bf84c4ff2e31e270a0ea Mon Sep 17 00:00:00 2001 From: Kyle Carow Date: Tue, 10 Oct 2023 11:00:28 -0600 Subject: [PATCH 1/4] replace Option::unwrap() with Option::ok_or_else() w descriptive message --- rust/fastsim-core/src/traits.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rust/fastsim-core/src/traits.rs b/rust/fastsim-core/src/traits.rs index 0507a66b..bbd30ba3 100644 --- a/rust/fastsim-core/src/traits.rs +++ b/rust/fastsim-core/src/traits.rs @@ -21,7 +21,12 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> { /// A Rust Result fn to_file(&self, filename: &str) -> Result<(), anyhow::Error> { let file = PathBuf::from(filename); - match file.extension().unwrap().to_str().unwrap() { + match file + .extension() + .ok_or_else(|| anyhow!("File extension not specified"))? + .to_str() + .unwrap() + { "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)?, From e27026c79b96bea01cffdc865496c36af324f710 Mon Sep 17 00:00:00 2001 From: Kyle Carow Date: Tue, 10 Oct 2023 11:10:36 -0600 Subject: [PATCH 2/4] update error message --- rust/fastsim-core/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/fastsim-core/src/traits.rs b/rust/fastsim-core/src/traits.rs index bbd30ba3..193ca9bd 100644 --- a/rust/fastsim-core/src/traits.rs +++ b/rust/fastsim-core/src/traits.rs @@ -23,7 +23,7 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> { let file = PathBuf::from(filename); match file .extension() - .ok_or_else(|| anyhow!("File extension not specified"))? + .ok_or_else(|| anyhow!("Unable to parse file extension: {:?}", file))? .to_str() .unwrap() { From b58f89fe243e817546fcc1e54c6c0039a340110d Mon Sep 17 00:00:00 2001 From: Kyle Carow Date: Tue, 10 Oct 2023 11:15:21 -0600 Subject: [PATCH 3/4] clean up function signature return --- rust/fastsim-core/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/fastsim-core/src/traits.rs b/rust/fastsim-core/src/traits.rs index 193ca9bd..b8305886 100644 --- a/rust/fastsim-core/src/traits.rs +++ b/rust/fastsim-core/src/traits.rs @@ -19,7 +19,7 @@ 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() From 8a2b6391e69f09e6c01f578266d6027de777a884 Mon Sep 17 00:00:00 2001 From: Kyle Carow Date: Tue, 10 Oct 2023 12:16:44 -0600 Subject: [PATCH 4/4] add error message for &OsStr to &str conversion failure, doubt this will error occur --- rust/fastsim-core/src/traits.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/rust/fastsim-core/src/traits.rs b/rust/fastsim-core/src/traits.rs index b8305886..94c8a783 100644 --- a/rust/fastsim-core/src/traits.rs +++ b/rust/fastsim-core/src/traits.rs @@ -25,8 +25,12 @@ pub trait SerdeAPI: Serialize + for<'a> Deserialize<'a> { .extension() .ok_or_else(|| anyhow!("Unable to parse file extension: {:?}", file))? .to_str() - .unwrap() - { + .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)?,