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>
impl<'a> Context<'a>
sourcepub fn declare_var<I, T>(&mut self, name: I, value: T)where
I: Into<Identifier>,
T: Into<Value>,
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);
sourcepub fn declare_func<I>(&mut self, name: I, func: FuncDef)where
I: Into<Identifier>,
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);