proxy_sdk/
context.rs

1use std::any::Any;
2
3use crate::{http::HttpContext, stream::StreamContext};
4
5pub enum Context {
6    Http(Box<dyn HttpContext>),
7    Stream(Box<dyn StreamContext>),
8}
9
10pub trait BaseContext {
11    /// Called for access log WASM plugins. Not well supported in this crate. Unclear what context this gets called on.
12    fn on_log(&mut self) {}
13
14    /// Called when all processing is complete in the proxy for this context.
15    /// If returns true, the context is deleted immediately (i.e. dropped).
16    /// If returns false, then the drop is deferred
17    fn on_done(&mut self) -> bool {
18        true
19    }
20}
21
22#[allow(unused_variables)]
23pub trait RootContext: BaseContext + Any {
24    /// If returns true, VM startup is successful (and shall continue)
25    /// If returns false, VM startup is a failure and will be aborted.
26    fn on_vm_start(&mut self, configuration: Option<Vec<u8>>) -> bool {
27        true
28    }
29
30    /// If returns true, VM startup is successful (and shall continue)
31    /// If returns false, VM startup is a failure and will be aborted.
32    fn on_configure(&mut self, configuration: Option<Vec<u8>>) -> bool {
33        true
34    }
35
36    /// Called every tick period as set by [`crate::time::set_tick_period`]
37    fn on_tick(&mut self) {}
38
39    /// Called to initiate a new HTTP or Stream context.
40    fn create_context(&mut self) -> Context;
41}
42
43impl<R: RootContext> From<Box<R>> for Box<dyn RootContext> {
44    fn from(value: Box<R>) -> Self {
45        value
46    }
47}