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

Allow actions to return ActionResult values to represent errors #76

Merged
merged 19 commits into from
Dec 2, 2023

Conversation

timburks
Copy link
Contributor

@timburks timburks commented Nov 29, 2023

Fixes #75

This PR contains a nonbreaking change that adds support for a new action field, action_with_result, that can be specified instead of action for Command and App structs. These fields take functions that return a new Result type that includes an error value that can be checked in the Seahorse calling code.

done (ready to verify):

  • added new ActionWithResult types and handling.
  • update examples in comments to describe the new capability.
  • restore support for the original action handling.
  • generate runtime errors if users specify both action and action_with_result.
  • original tests all pass.
  • update tests to verify new capability.

todo:

  • update README.

@timburks timburks marked this pull request as draft November 29, 2023 16:40
@timburks timburks marked this pull request as ready for review December 1, 2023 23:02
src/app.rs Outdated
self.help_text(),
)) {
Ok(_) => (),
Err(e) => panic!("{}", e.message),
Copy link
Contributor Author

@timburks timburks Dec 1, 2023

Choose a reason for hiding this comment

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

@ksk001100 Instead of calling panic! here, I think it would be better to return an error from this run function, which we might rename to run_with_result. To make this a nonbreaking change, we could add a new run function that has the same interface as the current one and which calls run_with_result and panics if run_with_result returns an error. What do you think about this? If it sounds good to you, I could make that change in this PR or a followup (please let me know your preference).

For background, my overall goal is to remove calls to panic! from command execution, generally following this discussion. Thanks!

Copy link
Owner

Choose a reason for hiding this comment

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

@timburks
I think that suggestion is good, please implement run_with_result.

Copy link
Owner

Choose a reason for hiding this comment

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

We would like to replace the run implementation with a run_with_result implementation in the future, as we want to prioritize simplicity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ksk001100 Thanks for your feedback! Yes, I'll proceed with that in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ksk001100 I think run_with_result is ready to review. I've added some top-level tests that demonstrate calling it and I've put a comment in #75 showing how it can be used to return Result values directly from main().

I agree with eventually fully replacing run with run_with_result (and just calling the new function run), and also agree with waiting for a major version bump to make that breaking change. I'm happy to make those changes whenever you'd like to do that.

Since this is a fairly large change, feel free to take your time to review and get feedback from anyone else before merging. Thank you!

@ksk001100
Copy link
Owner

@timburks
This looks great, thank you!!

@ksk001100 ksk001100 merged commit 2a0dcc8 into ksk001100:master Dec 2, 2023
6 checks passed
@timburks timburks deleted the action-result branch December 2, 2023 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow actions to return Result<(),Error>?
2 participants