|
| 1 | +use core::{any::type_name, fmt}; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + entity::Entity, |
| 5 | + system::{entity_command::EntityCommandError, Command, EntityCommand}, |
| 6 | + world::{error::EntityMutableFetchError, World}, |
| 7 | +}; |
| 8 | + |
| 9 | +use super::{default_error_handler, BevyError, ErrorContext}; |
| 10 | + |
| 11 | +/// Takes a [`Command`] that returns a Result and uses a given error handler function to convert it into |
| 12 | +/// a [`Command`] that internally handles an error if it occurs and returns `()`. |
| 13 | +pub trait HandleError<Out = ()> { |
| 14 | + /// Takes a [`Command`] that returns a Result and uses a given error handler function to convert it into |
| 15 | + /// a [`Command`] that internally handles an error if it occurs and returns `()`. |
| 16 | + fn handle_error_with(self, error_handler: fn(BevyError, ErrorContext)) -> impl Command; |
| 17 | + /// Takes a [`Command`] that returns a Result and uses the default error handler function to convert it into |
| 18 | + /// a [`Command`] that internally handles an error if it occurs and returns `()`. |
| 19 | + fn handle_error(self) -> impl Command |
| 20 | + where |
| 21 | + Self: Sized, |
| 22 | + { |
| 23 | + self.handle_error_with(default_error_handler()) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl<C, T, E> HandleError<Result<T, E>> for C |
| 28 | +where |
| 29 | + C: Command<Result<T, E>>, |
| 30 | + E: Into<BevyError>, |
| 31 | +{ |
| 32 | + fn handle_error_with(self, error_handler: fn(BevyError, ErrorContext)) -> impl Command { |
| 33 | + move |world: &mut World| match self.apply(world) { |
| 34 | + Ok(_) => {} |
| 35 | + Err(err) => (error_handler)( |
| 36 | + err.into(), |
| 37 | + ErrorContext::Command { |
| 38 | + name: type_name::<C>().into(), |
| 39 | + }, |
| 40 | + ), |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl<C> HandleError for C |
| 46 | +where |
| 47 | + C: Command, |
| 48 | +{ |
| 49 | + #[inline] |
| 50 | + fn handle_error_with(self, _error_handler: fn(BevyError, ErrorContext)) -> impl Command { |
| 51 | + self |
| 52 | + } |
| 53 | + #[inline] |
| 54 | + fn handle_error(self) -> impl Command |
| 55 | + where |
| 56 | + Self: Sized, |
| 57 | + { |
| 58 | + self |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/// Passes in a specific entity to an [`EntityCommand`], resulting in a [`Command`] that |
| 63 | +/// internally runs the [`EntityCommand`] on that entity. |
| 64 | +/// |
| 65 | +// NOTE: This is a separate trait from `EntityCommand` because "result-returning entity commands" and |
| 66 | +// "non-result returning entity commands" require different implementations, so they cannot be automatically |
| 67 | +// implemented. And this isn't the type of implementation that we want to thrust on people implementing |
| 68 | +// EntityCommand. |
| 69 | +pub trait CommandWithEntity<Out> { |
| 70 | + /// Passes in a specific entity to an [`EntityCommand`], resulting in a [`Command`] that |
| 71 | + /// internally runs the [`EntityCommand`] on that entity. |
| 72 | + fn with_entity(self, entity: Entity) -> impl Command<Out> + HandleError<Out>; |
| 73 | +} |
| 74 | + |
| 75 | +impl<C> CommandWithEntity<Result<(), EntityMutableFetchError>> for C |
| 76 | +where |
| 77 | + C: EntityCommand, |
| 78 | +{ |
| 79 | + fn with_entity( |
| 80 | + self, |
| 81 | + entity: Entity, |
| 82 | + ) -> impl Command<Result<(), EntityMutableFetchError>> |
| 83 | + + HandleError<Result<(), EntityMutableFetchError>> { |
| 84 | + move |world: &mut World| -> Result<(), EntityMutableFetchError> { |
| 85 | + let entity = world.get_entity_mut(entity)?; |
| 86 | + self.apply(entity); |
| 87 | + Ok(()) |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl<C, T, Err> CommandWithEntity<Result<T, EntityCommandError<Err>>> for C |
| 93 | +where |
| 94 | + C: EntityCommand<Result<T, Err>>, |
| 95 | + Err: fmt::Debug + fmt::Display + Send + Sync + 'static, |
| 96 | +{ |
| 97 | + fn with_entity( |
| 98 | + self, |
| 99 | + entity: Entity, |
| 100 | + ) -> impl Command<Result<T, EntityCommandError<Err>>> + HandleError<Result<T, EntityCommandError<Err>>> |
| 101 | + { |
| 102 | + move |world: &mut World| { |
| 103 | + let entity = world.get_entity_mut(entity)?; |
| 104 | + self.apply(entity) |
| 105 | + .map_err(EntityCommandError::CommandFailed) |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments