Rust

Bus

A synchronous in-process event bus: typed events, listeners with injected dependencies, failures isolated and recorded on the current span.
Rust 516 lines (206 code)split
PartCodeCommentsBlankTotalFiles
Rust source206270405161
Rust tests2502271

Comments include doc comments. Rust tests are tests/ and #[cfg(test)] modules.

Install#

The crate lives in the Appwrite repository; depend on it from Git.

Cargo.toml
[dependencies]
utopia-bus = { git = "https://github.com/appwrite/appwrite" }

Getting started#

Common tasks with Bus, each a complete program you can copy and run.

An event bus decouples what happens from what reacts to it: a deployment publishes Deployed, and the mailer, the audit log and the usage counter each subscribe to it without the deployment knowing about them. Listeners run synchronously, in subscription order, each with the dependencies it injects.

Publish an event to its listeners

Listeners subscribe to event types; dispatching an event calls those of its type only.

Rust
use std::sync::{Arc, Mutex};
use utopia_bus::{Bus, Event, Listener, Resource};

struct Deployed { id: String }
impl Event for Deployed {}
struct Cancelled;
impl Event for Cancelled {}

let log = Arc::new(Mutex::new(Vec::new()));
let mut bus = Bus::new();
bus.set_resolver(|name| Ok(Arc::new(name.to_owned()) as Resource));

let seen = log.clone();
bus.subscribe(Listener::new("audit").on::<Deployed>().callback(move |event, _| {
    if let Some(deployed) = event.downcast_ref::<Deployed>() {
        seen.lock().unwrap().push(format!("deployed {}", deployed.id));
    }
    Ok(())
}));
let seen = log.clone();
bus.subscribe(Listener::new("refunds").on::<Cancelled>().callback(move |_, _| {
    seen.lock().unwrap().push("cancelled".to_owned());
    Ok(())
}));

bus.dispatch(&Deployed { id: "d1".into() })?;
assert_eq!(*log.lock().unwrap(), ["deployed d1"]);

Inject dependencies

A listener names its dependencies; the bus's resolver builds each one on every dispatch, and the callback receives them in that order.

Rust
use std::sync::{Arc, Mutex};
use utopia_bus::{Bus, Event, Listener, Resource};

struct Deployed;
impl Event for Deployed {}

let sent = Arc::new(Mutex::new(Vec::new()));
let out = sent.clone();
let mut bus = Bus::new();
bus.set_resolver(|name| match name {
    "mailer" => Ok(Arc::new(String::from("smtp")) as Resource),
    other => Err(utopia_bus::Failure::new(format!("unknown dependency {other}"))),
});
bus.subscribe(Listener::new("notify").on::<Deployed>().inject("mailer").callback(move |_, deps| {
    out.lock().unwrap().push(deps[0].downcast_ref::<String>().cloned().unwrap_or_default());
    Ok(())
}));

bus.dispatch(&Deployed)?;
assert_eq!(*sent.lock().unwrap(), ["smtp"]);

Keep failures isolated

A failing listener does not stop the others; its failure is recorded on the current span.

Rust
use std::sync::Arc;
use utopia_bus::{Bus, Event, Failure, Listener, Resource};
use utopia_span::{Memory, Span, Value};

struct Deployed;
impl Event for Deployed {}

Span::set_storage(Some(Arc::new(Memory::new())));
let span = Span::init("deploy", None);

let mut bus = Bus::new();
bus.set_resolver(|name| Ok(Arc::new(name.to_owned()) as Resource));
bus.subscribe(Listener::new("mailer").on::<Deployed>().callback(|_, _| Err(Failure::new("SMTP is down").with_code(503))));
bus.subscribe(Listener::new("audit").on::<Deployed>().callback(|_, _| Ok(())));

bus.dispatch(&Deployed)?;
assert_eq!(span.get("listener.mailer.success"), Value::from(false));
assert_eq!(span.get("listener.mailer.error.message"), Value::from("SMTP is down"));
assert_eq!(span.get("listener.audit.success"), Value::from(true));

Reference#

A synchronous, in-process event bus. Code that does something publishes a typed Event; Listeners subscribed to that event type react to it, each with the dependencies it declares, resolved by name through the bus's resolver.

Bus::dispatch calls every listener of the event's type in subscription order. A listener that fails does not stop the others: its failure is recorded on the current span (listener.<name>.success, listener.<name>.error.*), next to listener.<name>.event.

Rust
use std::sync::{Arc, Mutex};
use utopia_bus::{Bus, Event, Listener, Resource};

struct Deployed;
impl Event for Deployed {}

let calls = Arc::new(Mutex::new(Vec::new()));
let seen = calls.clone();
let mut bus = Bus::new();
bus.set_resolver(|name| Ok(Arc::new(format!("{name}:resolved")) as Resource));
bus.subscribe(Listener::new("mailer").on::<Deployed>().inject("mailer").callback(move |_event, deps| {
    seen.lock().unwrap().push(deps[0].downcast_ref::<String>().cloned().unwrap_or_default());
    Ok(())
}));

bus.dispatch(&Deployed)?;
assert_eq!(*calls.lock().unwrap(), ["mailer:resolved"]);

API#

What utopia_bus exports at its root.