pub trait ContextFactory: Send + Sync {
// Required method
fn build(&self, actor_json: &Value) -> Result<Box<dyn ScriptContext>>;
}Expand description
Builds a ScriptContext from JSON captured at enqueue time on the run.
Install on ChrononBuilder::context_factory at boot. The executor/worker call Self::build
for every dispatched run using the run’s snapshotted actor_json.
| Implementation | When to use |
|---|---|
JsonScriptContextFactory | Examples / handlers that only need label + actor JSON |
NoOpContextFactory | Tests and benches |
| Custom | Production identity, sessions, permissions — prefer fail closed on invalid payloads |
§Examples
Custom factory sketch:
use chronon_core::{ContextFactory, ChrononError, Result, ScriptContext};
use serde_json::Value;
struct AppCtx { label: String, actor_json: Value }
impl ScriptContext for AppCtx {
fn label(&self) -> &str { &self.label }
fn actor_json(&self) -> &Value { &self.actor_json }
}
struct AppFactory;
impl ContextFactory for AppFactory {
fn build(&self, actor_json: &Value) -> Result<Box<dyn ScriptContext>> {
let Some(user) = actor_json.get("user").and_then(|v| v.as_str()) else {
return Err(ChrononError::Identity("missing user".into()));
};
Ok(Box::new(AppCtx {
label: user.into(),
actor_json: actor_json.clone(),
}))
}
}
let ctx = AppFactory.build(&serde_json::json!({"user": "bob"})).unwrap();
assert_eq!(ctx.label(), "bob");
assert!(AppFactory.build(&serde_json::json!({})).is_err());Required Methods§
Sourcefn build(&self, actor_json: &Value) -> Result<Box<dyn ScriptContext>>
fn build(&self, actor_json: &Value) -> Result<Box<dyn ScriptContext>>
Reconstruct handler context from actor JSON snapshotted on the run at enqueue.
§Errors
Returns IdentityError (mapped to ChrononError::Identity) when the payload
cannot be decoded into application identity. Hosts should fail closed on untrusted
or incomplete payloads.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".