Struct hcl::eval::Context

source ·
pub struct Context<'a> { /* private fields */ }
Expand description

A type holding the evaluation context.

The Context is used to declare variables and functions that are evaluated when evaluating a template or expression.

Implementations§

source§

impl<'a> Context<'a>

source

pub fn new() -> Self

Creates an empty Context.

source

pub fn declare_var<I, T>(&mut self, name: I, value: T)
where I: Into<Identifier>, T: Into<Value>,

Declare a variable from a name and a value.

§Example
let mut ctx = Context::new();
ctx.declare_var("some_number", 42);
source

pub fn declare_func<I>(&mut self, name: I, func: FuncDef)
where I: Into<Identifier>,

Declare a function from a name and a function definition.

See the documentation of the FuncDef type to learn about all available options for constructing a function definition.

§Example
use hcl::Value;
use hcl::eval::{FuncArgs, FuncDef, ParamType};

fn strlen(args: FuncArgs) -> Result<Value, String> {
    // The arguments are already validated against the function
    // definition's parameters, so we know that there is exactly
    // one arg of type string.
    Ok(Value::from(args[0].as_str().unwrap().len()))
}

let func_def = FuncDef::builder()
    .param(ParamType::String)
    .build(strlen);

let mut ctx = Context::new();
ctx.declare_func("strlen", func_def);

Trait Implementations§

source§

impl<'a> Clone for Context<'a>

source§

fn clone(&self) -> Context<'a>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for Context<'a>

source§

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

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

impl Default for Context<'_>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Context<'a>

§

impl<'a> RefUnwindSafe for Context<'a>

§

impl<'a> Send for Context<'a>

§

impl<'a> Sync for Context<'a>

§

impl<'a> Unpin for Context<'a>

§

impl<'a> UnwindSafe for Context<'a>

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, 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,

§

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>,

§

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>,

§

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.