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)
pub fn declare_var<I, T>(&mut self, name: I, value: T)
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)
pub fn declare_func<I>(&mut self, name: I, func: FuncDef)
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§
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> 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
Mutably borrows from an owned value. Read more