Skip to main content

Template

Struct Template 

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

A config field whose authored JSON is a JSONLogic expression.

Deserializes from any JSON value and keeps it verbatim; the expression is compiled once at engine construction (see crate::AsyncFunctionHandler::compile_input) and evaluated per message on the worker thread’s pooled arena — unless it folded to a constant, in which case the value is computed once at construction and handed back directly.

§Literals and the $ escape

A JSON scalar or array authored here is a literal: "data.out" resolves to the string data.out, 30000 to the number. An object is where care is needed, because the engine evaluates in templating mode: a single-key object whose key matches an operator name is that operator. {"cat": ["a", "b"]} resolves to "ab", not to the object.

Prefix the key with Engine::template_key_escape ($) to force the literal reading: {"$cat": ["a", "b"]} resolves to the object {"cat": ["a", "b"]}. One prefix is stripped from every template key, so a genuinely $-prefixed key doubles up — {"$$oid": …} emits $oid.

Before that escape existed a literal object with a colliding key was inexpressible, which is why this type used to be documented as opt-in per field. It no longer is: any config field may be a Template.

Implementations§

Source§

impl Template

Source

pub fn compile(&mut self, c: &TemplateCompiler, label: &str) -> Result<()>

Compile the expression. Called once at engine construction via crate::AsyncFunctionHandler::compile_input or its receiver-taking twin crate::AsyncFunctionHandler::compile_input_with. label is used only in the error message, matching LogicCompiler’s "<what> for task <id> in workflow <id>" convention.

§Errors

DataflowError::LogicEvaluation if the expression fails to compile, with label prefixed onto the message.

Source

pub fn is_constant(&self) -> bool

Whether the expression folded to a compile-time constant, so every resolve_* call returns a cached value instead of evaluating.

True for the static spelling of any parameter — a scalar, an array, or an object template with no var in it. False once anything reads the message. Callers that precompute a derived form (a split write path, for instance) branch on this.

Meaningless before Self::compile: an uncompiled Template reports false because nothing has been folded yet, not because the expression is dynamic.

Source

pub fn constant_string(&self) -> Option<String>

The folded constant coerced to a plain string, when the expression folded to one.

Lets a caller do at compile time what Self::resolve_string would otherwise defer to the first message — which is how PathTemplate precomputes a static write path.

Source

pub fn resolve(&self, ctx: &TaskContext<'_>) -> Result<OwnedDataValue>

The parameter’s value for this message: the cached constant when the expression folded, otherwise a fresh evaluation.

This is the sanctioned read for a config parameter. Self::eval is the same thing without the constant cache, kept for handlers that hold a Template they compiled themselves.

§Errors

As Self::eval.

Source

pub fn resolve_string(&self, ctx: &TaskContext<'_>) -> Result<String>

As Self::resolve, coerced to a plain string — a string result yields its contents, anything else its compact JSON form. Use this wherever the value becomes a URL path, a header value, a topic name or a write path, where JSON quoting would be wrong.

§Errors

As Self::eval.

Source

pub fn resolve_u64(&self, ctx: &TaskContext<'_>, label: &str) -> Result<u64>

As Self::resolve, as a u64 — for parameters like timeout_ms.

§Errors

As Self::eval, plus DataflowError::Validation when the result is not a number that fits a u64. A timeout that evaluated to null because its path was missing is a configuration error worth reporting, not something to silently default.

Source

pub fn eval(&self, ctx: &TaskContext<'_>) -> Result<OwnedDataValue>

Evaluate against the message context, on the worker thread’s pooled bump arena.

§Errors

DataflowError::LogicEvaluation if Self::compile was never called — naming the field is the caller’s job via label, since this type has no field name of its own to report — or if evaluation itself fails.

Source

pub fn eval_into<T: DeserializeOwned>(&self, ctx: &TaskContext<'_>) -> Result<T>

As Self::eval, deserialized into T.

Routes through serde_json::ValueTaskContext::eval_json then serde_json::from_value — so it costs one extra walk and rebuild past Self::eval. Prefer eval when T is OwnedDataValue or when you only need to inspect the result, not deserialize it into a caller type.

§Errors

As Self::eval, plus a deserialization error if the evaluated JSON does not fit T.

Source

pub fn eval_to_plain_string(&self, ctx: &TaskContext<'_>) -> Result<String>

As Self::eval, coerced to a plain string via TaskContext::eval_to_plain_string — a JSON string result yields its contents, anything else its compact JSON form. Use this when the result is going into a URL path or a message key, where JSON quoting would be wrong.

§Errors

As Self::eval.

Source

pub fn as_json(&self) -> &Value

The authored JSON, unchanged. For handlers that need to report or re-serialize their own config.

Source

pub fn is_compiled(&self) -> bool

Whether Self::compile has run. Mainly for tests and for callers that want to assert the build pass reached them.

Trait Implementations§

Source§

impl Clone for Template

Source§

fn clone(&self) -> Template

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Template

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Template

Source§

fn default() -> Self

A Template over JSON null.

Exists so config structs that derive Default still can. It is a placeholder, not a usable parameter — every config carrying one names the field as required, so a Default-built config is a base to fill in.

Source§

impl<'de> Deserialize<'de> for Template

Source§

fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<Value> for Template

Source§

fn from(raw: Value) -> Self

An uncompiled Template over raw. For hosts and tests building a config struct directly rather than deserializing one; LogicCompiler still has to compile it before any resolve_* call will succeed.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.