Replace ValidationOutcome with Result#18541
Conversation
| // combining the results into a single `ValidationOutcome`. | ||
| ValidationOutcome::Valid$(.combine($param::validate_param($param, system_meta, world)))* | ||
| $( | ||
| $param::validate_param($param, system_meta, world)?; |
There was a problem hiding this comment.
This is the tricky macro code that I was struggling to make short-circuit before.
alice-i-cecile
left a comment
There was a problem hiding this comment.
Semantically this makes sense to me: parma-validation is very Result-like. I also like cutting down on the code needed, and you managed to get the macro short-circuiting properly!
Good cleanup; thanks for chewing on this :)
ElliottjPierce
left a comment
There was a problem hiding this comment.
A few suggestions but nothing blocking approval.
There's other ways to do the short circuiting in the macro, but I agree that Result is semantically valuable here. Might be worth a type alias to provide documentation for what the result means more directly, but that's not a huge deal.
| } | ||
|
|
||
| /// Constructs a `SystemParamValidationError` that skips the system. | ||
| pub fn skipped() -> Self { |
There was a problem hiding this comment.
This is a small thing, but could we make these constants instead? Or make them const functions with inline?(Though they're probably inlined already)
There was a problem hiding this comment.
I don't want to make them actual constants because I'm anticipating we'll want to add parameters (#18515) and it should be a smoother transition if they're already functions.
Making them const fn is a good idea, though! I'll do that.
| pub struct SystemParamValidationError; | ||
| pub struct SystemParamValidationError { | ||
| /// Whether the system should be skipped. | ||
| pub skipped: bool, |
There was a problem hiding this comment.
Do we need this to be public? If we're already exposing is_skipped, etc, I don't see the need for this. Making it private would make this easier to change in the future, and it forces users to use the better documented functions.
| pub skipped: bool, | |
| skipped: bool, |
There was a problem hiding this comment.
I'd prefer to keep this pub and kill the helper method if we're deduplicating.
There was a problem hiding this comment.
Whoops, I had started with it pub and failed to remove it after I added the method. Good catch!
But it sounds like we want it pub, so I'll go remove the helper methods instead. (I guess I still have some old C# habits.)
| /// and handled using the unified error handling mechanisms defined in [`bevy_ecs::error`]. | ||
| #[derive(Debug, PartialEq, Eq, Clone, Display, Error)] | ||
| pub struct SystemParamValidationError; | ||
| pub struct SystemParamValidationError { |
There was a problem hiding this comment.
This is a thiserror derive, but I don't see the #[error(msg)] attribute. Instead Display is derived directly. Nothing wrong with that, but is this intentional?
There was a problem hiding this comment.
No clue, sorry. It was like that when I got here, and I'm not familliar with thiserror.
There was a problem hiding this comment.
That's fine. We can revisit this later if we need to.
| ); | ||
| Ok(()) => (), | ||
| Err(e) => { | ||
| if !e.is_skipped() { |
There was a problem hiding this comment.
This is repeated in a bunch of places. It was before too, but it might be worth pulling out into an associated function on the error type. Definitely not a problem, but worth mentioning.
There was a problem hiding this comment.
Yup, I said the same thing on the original PR :) #18504 (comment) It sounded like it was tricky to solve at the moment, so I didn't try.
There was a problem hiding this comment.
Well, it's only a little duplication, and it may be subject to change anyway. So we can come back to it if it's final form still has duplication and it becomes boiler plate.
|
I'll give you a couple days to respond to feedback here, but I'm going to merge this before the next release candidate regardless <3 |
# Objective Make it easier to short-circuit system parameter validation. Simplify the API surface by combining `ValidationOutcome` with `SystemParamValidationError`. ## Solution Replace `ValidationOutcome` with `Result<(), SystemParamValidationError>`. Move the docs from `ValidationOutcome` to `SystemParamValidationError`. Add a `skipped` field to `SystemParamValidationError` to distinguish the `Skipped` and `Invalid` variants. Use the `?` operator to short-circuit validation in tuples of system params.
…stemError (#18666) # Objective Provide more useful errors when `World::run_system` and related methods fail parameter validation. Let callers determine whether the validation failure would have skipped or failed the system. Follow-up to #18541. ## Solution Add a `SystemParamValidationError` value to the `RunSystemError::InvalidParams` and `RegisteredSystemError::InvalidParams` variants. That includes the complete context of the parameter validation error, including the `skipped` flag.
…stemError (#18666) # Objective Provide more useful errors when `World::run_system` and related methods fail parameter validation. Let callers determine whether the validation failure would have skipped or failed the system. Follow-up to #18541. ## Solution Add a `SystemParamValidationError` value to the `RunSystemError::InvalidParams` and `RegisteredSystemError::InvalidParams` variants. That includes the complete context of the parameter validation error, including the `skipped` flag.
Objective
Make it easier to short-circuit system parameter validation.
Simplify the API surface by combining
ValidationOutcomewithSystemParamValidationError.Solution
Replace
ValidationOutcomewithResult<(), SystemParamValidationError>. Move the docs fromValidationOutcometoSystemParamValidationError.Add a
skippedfield toSystemParamValidationErrorto distinguish theSkippedandInvalidvariants.Use the
?operator to short-circuit validation in tuples of system params.