hcl/expr/
conditional.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use super::Expression;
use serde::Deserialize;

/// The conditional operator allows selecting from one of two expressions based on the outcome of a
/// boolean expression.
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Conditional {
    /// A condition expression that evaluates to a boolean value.
    pub cond_expr: Expression,
    /// The expression returned by the conditional if the condition evaluates to `true`.
    pub true_expr: Expression,
    /// The expression returned by the conditional if the condition evaluates to `false`.
    pub false_expr: Expression,
}

impl Conditional {
    /// Creates a new `Conditional` from a condition and two expressions for the branches of the
    /// conditional.
    pub fn new<C, T, F>(cond_expr: C, true_expr: T, false_expr: F) -> Conditional
    where
        C: Into<Expression>,
        T: Into<Expression>,
        F: Into<Expression>,
    {
        Conditional {
            cond_expr: cond_expr.into(),
            true_expr: true_expr.into(),
            false_expr: false_expr.into(),
        }
    }
}