Crate bevy_anyhow_alert

Source
Expand description

bevy_anyhow_alert provides an extension trait enabling Bevy systems that return Result types to opt-in to a simple Alert-based error UI.

The main benefit: your systems can return Result<T, E> (or even Result<T, Vec<E>>) with one chain call: system.anyhow_alert() (or its counterpart, system.anyhow_alerts).

§Examples

This example shows a system that returns some Result:

use bevy::prelude::*;
use bevy_anyhow_alert::{AlertsPlugin, AnyhowAlertExt, anyhow::Result};

fn main() {
    let mut app = App::new();
    app.add_plugins(MinimalPlugins);
    app.add_plugins(AlertsPlugin::new());
    app.add_systems(Update, fallible_system.anyhow_alert());
    // app.run();
}

#[derive(Component)]
struct MyComponent;

fn fallible_system(my_query: Query<&MyComponent>) -> Result<()> {
    for my_value in my_query.iter() {
        // we can use the `?` operator!
        get_result()?;
    }
    Ok(())
}

fn get_result() -> Result<()> {
    Ok(())
}

Alternatively, the system can collect errors without interrupting the iteration and return a vector of Results:

use bevy::prelude::*;
use bevy_anyhow_alert::{AnyhowAlertExt, ResultVec};
use bevy_anyhow_alert::anyhow::{Error, Result};

#[derive(Component)]
struct MyComponent;

fn fallible_system(my_query: Query<&MyComponent>) -> ResultVec<(), Error> {
    let mut errors = vec![];
    for my_value in my_query.iter() {
        if let Err(error) = get_result() {
           errors.push(error);
        }
    }
    if errors.is_empty() {
        Ok(())
    } else {
        Err(errors)
    }
}

fn get_result() -> Result<()> {
    Ok(())
}

The resulting UI is somewhat restylable but may not fit every application.

Furthermore, this does not allow for any actual error maangement beyond displaying them. For errors that should be handled in more meaningful ways, consider using system.pipe directly or using .pipe_err from the bevy_try_mod_system crate.

Re-exports§

pub use anyhow;

Structs§

AlertsPlugin
A Bevy plugin that must be attached in order to spawn alert UIs.

Traits§

AnyhowAlertExt
Defines the anyhow_alert method which pipes system output to an Alert UI if the output is an error.
AnyhowAlertsExt
Defines the anyhow_alert method which pipes system output to an Alert UI if the output Vec<MyError> is non-empty.

Functions§

anyhow_alert_system
The inner PipeableSystem used by AnyhowAlertExt.
anyhow_alerts_system
The inner PipeableSystem used by AnyhowAlertsExt.

Type Aliases§

ResultVec