Skip to main content

TemplateContext

Struct TemplateContext 

Source
pub struct TemplateContext {
    pub collection: Arc<Collection>,
    pub selected_profile: Option<ProfileId>,
    pub http_provider: Box<dyn HttpProvider>,
    pub overrides: IndexMap<String, ValueTemplate>,
    pub prompter: Box<dyn Prompter>,
    pub show_sensitive: bool,
    pub root_dir: PathBuf,
    pub state: RenderGroupState,
}
Expand description

A little container struct for all the data that the user can access via templating. Unfortunately this has to own all data so templating can be deferred into a task (tokio requires 'static for spawned tasks). If this becomes a bottleneck, we can Arc some stuff.

One instance of this context applies to all renders in a group (a render group is all the renders for a single request). It’s important to use the same context for an entire request so common profile fields can be cached between components of the request.

This has two different implementations of Context: one for Value and one for ValueStream. The former is used in most cases, where values are eagerly evaluated. The latter is used when streams are supported.

Fields§

§collection: Arc<Collection>

Entire request collection

§selected_profile: Option<ProfileId>

ID of the profile whose data should be used for rendering. Generally the caller should check the ID is valid before passing it, to provide a better error to the user if not.

§http_provider: Box<dyn HttpProvider>

An interface to allow accessing and sending HTTP chained requests

§overrides: IndexMap<String, ValueTemplate>

Additional profile key=value overrides passed directly from the user. These will be applied to both the root and triggered requests, which is why they are part of the context instead of BuildOptions.

§prompter: Box<dyn Prompter>

A conduit to ask the user questions

§show_sensitive: bool

Should sensitive values be shown normally or masked? Enabled for request renders, disabled for previews

§root_dir: PathBuf

Directory in which to run file system operations. Should be the directory containing the collection file.

§state: RenderGroupState

State that should be shared across all renders that use this context. This is meant to be opaque; just use Default::default to initialize.

Trait Implementations§

Source§

impl Context<Value> for TemplateContext

Source§

async fn get_field(&self, field: &Identifier) -> Result<Value, RenderError>

Get the value of a field from the context. The implementor can decide where fields are derived from. Fields can also be computed dynamically and be async. For example, fields can be loaded from a map of nested templates, in which case the nested template would need to be rendered before this can be returned.
Source§

async fn call( &self, function_name: &Identifier, arguments: Arguments<'_, Self>, ) -> Result<Value, RenderError>

Call a function by name
Source§

impl Context<ValueStream> for TemplateContext

Source§

async fn get_field( &self, field: &Identifier, ) -> Result<ValueStream, RenderError>

Get the value of a field from the context. The implementor can decide where fields are derived from. Fields can also be computed dynamically and be async. For example, fields can be loaded from a map of nested templates, in which case the nested template would need to be rendered before this can be returned.
Source§

async fn call( &self, function_name: &Identifier, arguments: Arguments<'_, Self>, ) -> Result<ValueStream, RenderError>

Call a function by name
Source§

impl Debug for TemplateContext

Source§

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

Formats the value using the given formatter. Read more

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more