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§

Creates an empty Context.

Declare a variable from a name and a value.

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

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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.