Skip to main content

mytheclipse_event/
lib.rs

1//! # mytheclipse-event
2//!
3//! A unified events & message bus abstraction so component-to-component (or
4//! service-to-service) communication doesn't get locked to one transport.
5//!
6//! - **In-memory dispatcher** ([`memory::InMemoryEventBus`], feature `mem`,
7//!   default): a `tokio::sync::broadcast`-backed pub/sub bus for
8//!   intra-process communication.
9//! - **Distributed broker backends**: [`amqp::AmqpEventBus`] (RabbitMQ,
10//!   feature `amqp`) and [`nats::NatsEventBus`] (NATS, feature `nats`) behind
11//!   the same [`EventBus`] trait, so swapping the backend does not change
12//!   handler code.
13//! - **Typed convenience**: [`typed::TypedEventBus`] layers JSON
14//!   (de)serialization on top of the byte-oriented [`EventBus`].
15//!
16//! ## Example
17//!
18//! The in-memory bus (`mem` feature, on by default) carries raw bytes; the
19//! `typed` feature adds JSON (de)serialization on top of it.
20//!
21//! ```no_run
22//! #[cfg(feature = "mem")]
23//! #[tokio::main]
24//! async fn main() {
25//!     use mytheclipse_event::{EventBus, InMemoryEventBus};
26//!     let bus = InMemoryEventBus::default();
27//!     let mut sub = bus.subscribe("orders").await.unwrap();
28//!     bus.publish("orders", b"{\"id\":42}".to_vec()).await.unwrap();
29//!     let payload = sub.recv().await.unwrap();
30//!     assert_eq!(payload, b"{\"id\":42}");
31//! }
32//!
33//! #[cfg(not(feature = "mem"))]
34//! fn main() {}
35//! ```
36
37#![deny(missing_docs)]
38pub mod traits;
39
40#[cfg(feature = "mem")]
41pub mod memory;
42
43#[cfg(feature = "typed")]
44pub mod typed;
45
46#[cfg(feature = "amqp")]
47pub mod amqp;
48
49#[cfg(feature = "nats")]
50pub mod nats;
51
52pub use traits::{EventBus, EventError, Subscription};
53
54#[cfg(feature = "mem")]
55pub use memory::InMemoryEventBus;
56
57#[cfg(feature = "typed")]
58pub use typed::{Event, TypedEventBus, TypedSubscription};
59
60#[cfg(feature = "amqp")]
61pub use amqp::AmqpEventBus;
62
63#[cfg(feature = "nats")]
64pub use nats::NatsEventBus;
65
66/// The common case, in one import.
67///
68/// ```
69/// use mytheclipse_event::prelude::*;
70/// ```
71///
72/// [`EventBus`] is the reason this exists: `subscribe` and `publish` are trait
73/// methods, so they are not callable until the trait is in scope.
74pub mod prelude {
75    pub use crate::{EventBus, EventError, Subscription};
76
77    #[cfg(feature = "amqp")]
78    pub use crate::AmqpEventBus;
79    #[cfg(feature = "mem")]
80    pub use crate::InMemoryEventBus;
81    #[cfg(feature = "nats")]
82    pub use crate::NatsEventBus;
83    #[cfg(feature = "typed")]
84    pub use crate::{Event, TypedEventBus, TypedSubscription};
85}