Skip to main content

WalRouter

Struct WalRouter 

Source
pub struct WalRouter { /* private fields */ }
Expand description

Routes typed WAL change events to per-table async handlers.

Register handlers with on_insert, on_update, on_delete, and an optional on_default fallback, then drive an EventStream with run. Handlers are async and Send + 'static; capture shared state by cloning an Arc into the closure.

§Error behavior

The typed handlers deserialize the row before invoking your closure. A deserialization failure (schema mismatch, malformed data) — or any Err your handler returns — propagates out of run and terminates the stream. There is no per-event skip. If you need to log-and-continue, deserialize leniently inside an on_default handler (or drive the stream yourself with EventStream::for_each_event / next_event() and match event types by hand), returning Ok(()) to skip.

Implementations§

Source§

impl WalRouter

Source

pub fn new() -> Self

Create an empty router whose default handler is an async no-op.

Source

pub fn on_insert<T, F, Fut>( &mut self, table: impl Into<Arc<str>>, f: F, ) -> &mut Self
where T: DeserializeOwned + 'static, F: FnMut(T) -> Fut + Send + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Register an async handler for INSERT events on table, deserialized into T.

Source

pub fn on_update<T, F, Fut>( &mut self, table: impl Into<Arc<str>>, f: F, ) -> &mut Self
where T: DeserializeOwned + 'static, F: FnMut(Option<T>, T) -> Fut + Send + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Register an async handler for UPDATE events on table. The closure receives (old, new) where old is Some per the table’s replica identity.

Source

pub fn on_delete<T, F, Fut>( &mut self, table: impl Into<Arc<str>>, f: F, ) -> &mut Self
where T: DeserializeOwned + 'static, F: FnMut(T) -> Fut + Send + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Register an async handler for DELETE events on table, deserialized into T.

Source

pub fn on_default<F, Fut>(&mut self, f: F) -> &mut Self
where F: FnMut(&ChangeEvent) -> Fut + Send + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Set the fallback handler for events with no registered (table, kind) handler (including non-DML events like Begin/Commit/Relation).

The event is passed by reference — the router performs no clone, so a firehose of unhandled events costs nothing here. If your handler needs to own the event across an .await, clone it explicitly inside the closure (extract what you need synchronously first, otherwise the returned future would borrow the event and fail to satisfy the 'static bound).

Source

pub async fn run(&mut self, es: &mut EventStream) -> Result<()>

Drive an EventStream: dispatch each event, auto-advance applied LSN after each Ok, and exit gracefully on cancellation.

Source

pub fn on_insert_of<T, Fut>( &mut self, f: impl FnMut(T) -> Fut + Send + 'static, ) -> &mut Self
where T: WalTable + DeserializeOwned + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Register an INSERT handler for T, inferring the table from WalTable::TABLE. Level-A convenience over on_insert — the closure captures its own state.

The closure is an impl argument (not a named generic), so a turbofish only needs the row type and the future: on_insert_of::<User, _>(...), or annotate the closure param and omit it: on_insert_of(|u: User| ...).

Source

pub fn on_update_of<T, Fut>( &mut self, f: impl FnMut(Option<T>, T) -> Fut + Send + 'static, ) -> &mut Self
where T: WalTable + DeserializeOwned + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Register an UPDATE handler for T, inferring the table from T::TABLE.

Source

pub fn on_delete_of<T, Fut>( &mut self, f: impl FnMut(T) -> Fut + Send + 'static, ) -> &mut Self
where T: WalTable + DeserializeOwned + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Register a DELETE handler for T, inferring the table from T::TABLE.

Trait Implementations§

Source§

impl Default for WalRouter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more