pub struct Template {
segments: Vec<Segment>,
has_refs: bool,
}Expand description
A parsed template, ready to render repeatedly against different contexts.
Adjacent literal segments are merged at parse time, so a template with no
references at all collapses to zero-or-one Literal segments, enabling
the Template::render fast path to borrow directly from it.
Fields§
§segments: Vec<Segment>§has_refs: boolImplementations§
Source§impl Template
impl Template
Sourcepub fn parse(s: &str) -> (Template, Vec<String>)
pub fn parse(s: &str) -> (Template, Vec<String>)
Parses s, resolving env.* references from the process environment.
Returns the compiled template plus any warnings (well-formed-but-unknown references, unset env vars) discovered along the way.
Sourcepub fn parse_with_env(
s: &str,
env: &dyn Fn(&str) -> Option<String>,
) -> (Template, Vec<String>)
pub fn parse_with_env( s: &str, env: &dyn Fn(&str) -> Option<String>, ) -> (Template, Vec<String>)
Parses s, resolving env.* references via the injected env lookup
(used by tests to avoid depending on real process environment state).
Sourcepub fn render(&self, ctx: &Context) -> Cow<'_, str>
pub fn render(&self, ctx: &Context) -> Cow<'_, str>
Renders the template against ctx. Never touches $var syntax.
Literal-only templates return Cow::Borrowed without allocating.
Sourcepub fn render_with_legacy(&self, ctx: &Context) -> String
pub fn render_with_legacy(&self, ctx: &Context) -> String
Applies the legacy $var/${var} pass to the template’s literal
segments only; rendered {{...}} output is appended inert and is
never $-processed. Used only by fields that historically supported
$ interpolation.
This composition is deliberate, not an optimization: $-processing
the fully rendered string would let runtime data (a header, cookie,
query param, …) that happens to contain $var/${var} syntax act
as a read primitive against the context (e.g. a client-supplied
header value of $http_authorization would leak the Authorization
header) and would corrupt values like pa$sword4 or {"$ref":1}
flowing through a ref — exactly the classes this engine exists to
prevent. For a template with no refs (a single literal segment, or
none), this is byte-identical to the old whole-string pass.
Sourcefn literal_str(&self) -> &str
fn literal_str(&self) -> &str
Borrows the single literal chunk of a literal-only template (or “”
when the source was empty). Only meaningful when !self.has_refs.
Sourcepub fn references_response_body(&self) -> bool
pub fn references_response_body(&self) -> bool
True when the template has no {{...}} references at all (render is
a pure borrow with no per-request work).
Whether rendering this template reads context.response.body.
Covers both spellings a config can use: the parsed {{response.body}}
reference, and the legacy $resp_body, which render_with_legacy
resolves at render time and so never becomes a segment – a
segment-only scan would miss it entirely.
The legacy check is textual, so a literal containing $resp_body in a
field rendered by Template::render (which never interpolates
$var) reports true as well. That over-reports in a rare case and
buffers a response that could have streamed, which is the safe
direction: streaming when a node needed the body corrupts the
response, while buffering only forgoes an optimization.
pub fn is_literal(&self) -> bool
Sourcepub fn source_warnings_only(s: &str) -> Vec<String>
pub fn source_warnings_only(s: &str) -> Vec<String>
Parses s and returns only the warnings, discarding the template.
Used by the compile-time walk that surfaces warnings without
duplicating render behavior.
Trait Implementations§
impl Eq for Template
impl StructuralPartialEq for Template
Auto Trait Implementations§
impl Freeze for Template
impl RefUnwindSafe for Template
impl Send for Template
impl Sync for Template
impl Unpin for Template
impl UnsafeUnpin for Template
impl UnwindSafe for Template
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more