Skip to main content

CallLayer

Trait CallLayer 

Source
pub trait CallLayer:
    Send
    + Sync
    + 'static {
    // Required method
    fn call<'a>(
        &'a self,
        ctx: &'a mut Ctx<'_>,
        next: Next<'a>,
    ) -> BoxFuture<'a, Result<Outcome>>;
}
Expand description

Around-call middleware for host calls.

A layer on the Registry wraps every host call of every run. The built-ins run inline and are never wrapped. A layer calls Next::run to go on, or answers without calling it. testing::call runs one call inside the layers it is given:

use dataflow_rs::function::{BoxFuture, CallLayer, Next, testing};
use dataflow_rs::{Ctx, Function, Outcome, Registry, Result};
use serde_json::json;

/// Turns any `5xx` into a `503`.
struct Unavailable;

impl CallLayer for Unavailable {
    fn call<'a>(
        &'a self,
        ctx: &'a mut Ctx<'_>,
        next: Next<'a>,
    ) -> BoxFuture<'a, Result<Outcome>> {
        Box::pin(async move {
            Ok(match next.run(ctx).await? {
                Outcome::Status(500..) => Outcome::Status(503),
                outcome => outcome,
            })
        })
    }
}

let mut registry = Registry::default();
registry.layer(Unavailable);

/// Fails every call with a `500`.
struct Down;

impl Function for Down {
    type Config = ();

    async fn call(&self, _ctx: &mut Ctx<'_>, _cfg: &()) -> Result<Outcome> {
        Ok(Outcome::Status(500))
    }
}

let called = testing::call(Down, json!({})).layer(Unavailable).await;
assert_eq!(called.outcome.unwrap(), Outcome::Status(503));

Required Methods§

Source

fn call<'a>( &'a self, ctx: &'a mut Ctx<'_>, next: Next<'a>, ) -> BoxFuture<'a, Result<Outcome>>

Wraps one call. next is the rest of the chain.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§