Rust

Crate root

Module
What utopia_bus defines at its root.

Contents

use utopia_bus::Bus;
Rust
pub struct Bus

Delivers events to the listeners subscribed to their type.

Rust
use utopia_bus::{Bus, Error, Event};

struct Deployed;
impl Event for Deployed {}

let error = Bus::new().dispatch(&Deployed).unwrap_err();
assert!(matches!(error, Error::Logic(_)));
assert_eq!(error.to_string(), "Bus resolver must be set via setResolver() before dispatching events");
Also implements Debug.
use utopia_bus::Failure;
Rust
pub struct Failure

A listener's failure, as recorded on the span: a message, a code and where it was raised.

Rust
use utopia_bus::Failure;

let failure = Failure::new("Mailer is down").with_code(503);
assert_eq!(failure.message(), "Mailer is down");
assert_eq!(failure.code(), 503);
assert!(failure.file().ends_with(".rs"));
Also implements Display, From.
use utopia_bus::Listener;
Rust
pub struct Listener

Reacts to events of the types it is subscribed to, with the dependencies it injects.

Rust
use utopia_bus::{Event, Listener};

struct Deployed;
impl Event for Deployed {}

let listener = Listener::new("clock").desc("Records deploy times").on::<Deployed>().inject("clock");
assert_eq!(listener.name(), "clock");
assert_eq!(listener.injections(), ["clock"]);
assert!(listener.get_callback().is_err());
Also implements Debug.
use utopia_bus::Error;
Rust
pub enum Error

Why a dispatch failed: listeners' own failures never fail it.

use utopia_bus::Event;
Rust
pub trait Event: Any + Send + Sync

Something that happened, published on a Bus. Listeners subscribe to event types.

Rust
use utopia_bus::Event;

struct UserCreated { id: String }
impl Event for UserCreated {
    fn name(&self) -> &str { "users.create" }
}

let event = UserCreated { id: "u1".into() };
let event: &dyn Event = &event;
assert_eq!(event.name(), "users.create");
assert_eq!(event.downcast_ref::<UserCreated>().map(|e| e.id.as_str()), Some("u1"));
Rust
pub type Callback = Arc<dyn Fn(&dyn Event, &[Resource]) -> Result<(), Failure> + Send + Sync>

A listener's callback: the event and the listener's dependencies, resolved in the order it injected them.

Rust
pub type Resolver = Arc<dyn Fn(&str) -> Result<Resource, Failure> + Send + Sync>

Resolves a dependency by name.

Rust
pub type Resource = Arc<dyn Any + Send + Sync>

A resolved dependency a listener receives.