bus-contract 0.1.0

A contract for an in-process hook bus: libraries declare typed hook points and listen with named order (after, before, last), owners and deadlines; the application supplies the implementation and decides on one bus or several. No dependencies.
Documentation

bus-contract

Latest version Documentation Build Status MSRV 1.95 no unsafe no dependencies MIT Apache

Traits and types for an in-process bus of hook points (requests, events, alter, collect) with ordered listeners.

A library declares its points and listeners against the Bus trait. The application creates the bus and passes it to each library at construction. There is no global.

Contents

  • Points are types (Point): an id (doc.close), the input every listener reads, the value a listener edits, and a declared kind (request, event, alter, collect).
  • Bus, an object-safe trait (Arc<dyn Bus>): declare a point; listen under an id and an owner, ordered by after / before / last, then priority; unlisten; forget an owner; alter and collect emits that return every listener's failure (error, panic, missed deadline); failure sinks; introspection.
  • Typed calls on dyn Bus (declare::<P>, listen::<P>, alter::<P>, collect::<P>, on_failure), written once over the erased methods: an implementation writes only those.
  • A contract table (PointDecl): a library's points as a const, readable before anything is built, declared with declare_all.
  • Tools on the contract: order_dump ("who runs when", for reviewed files and debug views) and missing_targets (an ordering constraint naming a listener that is not there).
  • No dependencies, no unsafe, no async runtime.

This crate has no implementation. One is Kernel in egui-pop-hooks. The guarantees an implementation must keep are documented on Bus.

Example

use std::sync::Arc;
use bus_contract::{Bus, ListenOpts, Point, PointDecl, PointKind, declare_all};

/// The library's request: a document closes.
pub struct DocClose;
impl Point for DocClose {
    const ID: &'static str = "doc.close";
    const KIND: PointKind = PointKind::Request;
    type Input = String;
    type Value = Vec<String>;
}

/// The library's points, readable before anything is built.
pub const POINTS: &[PointDecl] = &[PointDecl::of::<DocClose>("A document closes.")];

/// The library takes the bus; it never constructs one.
pub struct Documents(Arc<dyn Bus>);

impl Documents {
    pub fn new(bus: Arc<dyn Bus>) -> Documents {
        declare_all(&*bus, POINTS);
        let opts = ListenOpts::new().owner("docs");
        let _ = bus.listen::<DocClose>("docs.autosave", opts, |doc, did| {
            did.push(format!("saved {doc}"));
            Ok(())
        });
        Documents(bus)
    }

    pub fn close(&self, doc: &str) -> Vec<String> {
        self.0.alter::<DocClose>(doc.to_owned(), Vec::new()).value
    }
}

The application creates the bus and passes it to each library:

let bus: Arc<dyn Bus> = Arc::new(egui_pop_hooks::Kernel::new()); // or its own
let docs = Documents::new(bus.clone());
let search = Search::new(bus.clone());
print!("{}", bus_contract::order_dump(&*bus, &["doc.close"]));

Not included

  • A journal (events with a revision, catch-up from a revision).
  • Transport across processes.

License

MIT OR Apache-2.0, at your option.