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
use std::fmt;
use error::Result;
use value::Scalar;
use value::Value;
use super::Context;
use variable::Variable;
#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
Variable(Variable),
Literal(Value),
}
impl Expression {
pub fn with_literal<S: Into<Scalar>>(literal: S) -> Self {
Expression::Literal(Value::scalar(literal))
}
pub fn evaluate(&self, context: &Context) -> Result<Value> {
let val = match *self {
Expression::Literal(ref x) => x.clone(),
Expression::Variable(ref x) => {
let path = x.evaluate(context)?;
context.stack().get(&path)?.clone()
}
};
Ok(val)
}
}
impl fmt::Display for Expression {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Expression::Literal(ref x) => write!(f, "{}", x),
Expression::Variable(ref x) => write!(f, "{}", x),
}
}
}