hcl_edit/expr/
operation.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use crate::expr::Expression;
use crate::{Decor, Spanned};
use std::ops::Range;

// Re-exported for convenience.
#[doc(inline)]
pub use hcl_primitives::expr::{BinaryOperator, UnaryOperator};

/// An operation that applies an operator to one expression.
#[derive(Debug, Clone, Eq)]
pub struct UnaryOp {
    /// The unary operator to use on the expression.
    pub operator: Spanned<UnaryOperator>,
    /// An expression that supports evaluation with the unary operator.
    pub expr: Expression,

    decor: Decor,
    span: Option<Range<usize>>,
}

impl UnaryOp {
    /// Creates a new `UnaryOp` from an operator and an expression.
    pub fn new(
        operator: impl Into<Spanned<UnaryOperator>>,
        expr: impl Into<Expression>,
    ) -> UnaryOp {
        UnaryOp {
            operator: operator.into(),
            expr: expr.into(),
            decor: Decor::default(),
            span: None,
        }
    }

    pub(crate) fn despan(&mut self, input: &str) {
        self.decor.despan(input);
        self.expr.despan(input);
    }
}

impl PartialEq for UnaryOp {
    fn eq(&self, other: &Self) -> bool {
        self.operator == other.operator && self.expr == other.expr
    }
}

/// An operation that applies an operator to two expressions.
#[derive(Debug, Clone, Eq)]
pub struct BinaryOp {
    /// The expression on the left-hand-side of the operation.
    pub lhs_expr: Expression,
    /// The binary operator to use on the expressions.
    pub operator: Spanned<BinaryOperator>,
    /// The expression on the right-hand-side of the operation.
    pub rhs_expr: Expression,

    decor: Decor,
    span: Option<Range<usize>>,
}

impl BinaryOp {
    /// Creates a new `BinaryOp` from two expressions and an operator.
    pub fn new(
        lhs_expr: impl Into<Expression>,
        operator: impl Into<Spanned<BinaryOperator>>,
        rhs_expr: impl Into<Expression>,
    ) -> BinaryOp {
        BinaryOp {
            lhs_expr: lhs_expr.into(),
            operator: operator.into(),
            rhs_expr: rhs_expr.into(),
            decor: Decor::default(),
            span: None,
        }
    }

    pub(crate) fn despan(&mut self, input: &str) {
        self.decor.despan(input);
        self.lhs_expr.despan(input);
        self.rhs_expr.despan(input);
    }
}

impl PartialEq for BinaryOp {
    fn eq(&self, other: &Self) -> bool {
        self.lhs_expr == other.lhs_expr
            && self.operator == other.operator
            && self.rhs_expr == other.rhs_expr
    }
}

decorate_impl!(UnaryOp, BinaryOp);
span_impl!(UnaryOp, BinaryOp);