Struct hcl::eval::FuncDef

source ·
pub struct FuncDef { /* private fields */ }
Expand description

The definition of a function that can be called in HCL expressions.

It defines the function to call, and number and types of parameters that the function accepts. The parameter information is used to validate function arguments prior to calling it.

The signature of a function is defined by the Func type alias. For available parameter types see the documentation of ParamType.

Function call evaluation

When a [FuncCall][crate::structure::FuncCall] is evaluated (via its evaluate method), the arguments are validated against the defined function parameters before calling the function. The evaluation will stop with an error if too few or too many arguments are provided, of if their types do not match the expected parameter types.

Because all arguments are validated before calling the function, unnecessary length and type checks on the function arguments can be avoided in the function body.

Examples

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

fn add(args: FuncArgs) -> Result<Value, String> {
    let a = args[0].as_number().unwrap();
    let b = args[1].as_number().unwrap();
    Ok(Value::Number(*a + *b))
}

let params = [ParamType::Number, ParamType::Number];

let func_def = FuncDef::new(add, params);

let mut ctx = Context::new();

// Declare the function in the context to make it available
// during expression evaluation.
ctx.declare_func("add", func_def);

// Use the context to evaluate an expression.
// ...

Alternatively, the FuncDefBuilder can be used to construct the FuncDef:

let func_def = FuncDef::builder()
    .param(ParamType::Number)
    .param(ParamType::Number)
    .build(add);

See the documentation of the FuncDefBuilder for all available methods.

Implementations§

Creates a new FuncDef from a function and its parameters.

Note: if you want to define a FuncDef with a variadic parameter, use the .builder() method. It provides a FuncDefBuilder which also lets you define variadic parameters.

See the type-level documentation of FuncDef for usage examples.

Creates a FuncDefBuilder.

See the type-level documentation of FuncDef for usage examples.

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

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.