Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Option::unwrap() with Option::ok_or_else() with descriptive message #56

Merged
merged 4 commits into from
Oct 24, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions rust/fastsim-core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ 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))?
Copy link
Collaborator

@calbaker calbaker Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kylecarow , I think you could change

ok_or_else(|| anyhow!("Unable to parse file extension: {:?}", file))?

to something like

ok_or(|e| anyhow!("{e}\nUnable to parse file extension: {:?}", e, file))?

to preserve whatever the native error message is as context.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I don't know that the native error message really exists. The unwrap was on an Option, not a Result, so the error was just about unwrapping on a None

.to_str()
.unwrap()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kylecarow , I'd lean toward also getting rid of this unwrap with a separate message since it's not much additional effort.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@calbaker done, see latest commit

{
"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
Loading