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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::fmt;
use crate::error::{Error, Result};
use crate::model::Path;
use crate::model::Scalar;
use crate::model::{ValueCow, ValueView};
use super::Expression;
use super::Runtime;
#[derive(Clone, Debug, PartialEq)]
pub struct Variable {
variable: Scalar,
indexes: Vec<Expression>,
}
impl Variable {
pub fn with_literal<S: Into<Scalar>>(value: S) -> Self {
Self {
variable: value.into(),
indexes: Default::default(),
}
}
pub fn push_literal<S: Into<Scalar>>(mut self, value: S) -> Self {
self.indexes.push(Expression::with_literal(value));
self
}
pub fn try_evaluate<'c>(&'c self, runtime: &'c dyn Runtime) -> Option<Path<'c>> {
let mut path = Path::with_index(self.variable.as_ref());
path.reserve(self.indexes.len());
for expr in &self.indexes {
let v = expr.try_evaluate(runtime)?;
let s = match v {
ValueCow::Owned(v) => v.into_scalar(),
ValueCow::Borrowed(v) => v.as_scalar(),
}?;
path.push(s);
}
Some(path)
}
pub fn evaluate<'c>(&'c self, runtime: &'c dyn Runtime) -> Result<Path<'c>> {
let mut path = Path::with_index(self.variable.as_ref());
path.reserve(self.indexes.len());
for expr in &self.indexes {
let v = expr.evaluate(runtime)?;
let s = match v {
ValueCow::Owned(v) => v.into_scalar(),
ValueCow::Borrowed(v) => v.as_scalar(),
}
.ok_or_else(|| {
let v = expr.evaluate(runtime).expect("lookup already verified");
let v = v.source();
let msg = format!("Expected scalar, found `{}`", v);
Error::with_msg(msg)
})?;
path.push(s);
}
Ok(path)
}
}
impl Extend<Scalar> for Variable {
fn extend<T: IntoIterator<Item = Scalar>>(&mut self, iter: T) {
let path = iter.into_iter().map(Expression::with_literal);
self.indexes.extend(path);
}
}
impl Extend<Expression> for Variable {
fn extend<T: IntoIterator<Item = Expression>>(&mut self, iter: T) {
let path = iter.into_iter();
self.indexes.extend(path);
}
}
impl fmt::Display for Variable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.variable.render())?;
for index in self.indexes.iter() {
write!(f, "[{}]", index)?;
}
Ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::model::Object;
use crate::model::ValueViewCmp;
use super::super::RuntimeBuilder;
use super::super::StackFrame;
#[test]
fn identifier_path_array_index() {
let globals: Object = serde_yaml::from_str(
r#"
test_a: ["test"]
"#,
)
.unwrap();
let mut var = Variable::with_literal("test_a");
let index = vec![Scalar::new(0)];
var.extend(index);
let runtime = RuntimeBuilder::new().build();
let runtime = StackFrame::new(&runtime, &globals);
let actual = var.evaluate(&runtime).unwrap();
let actual = runtime.get(&actual).unwrap();
assert_eq!(actual, ValueViewCmp::new(&"test"));
}
#[test]
fn identifier_path_array_index_negative() {
let globals: Object = serde_yaml::from_str(
r#"
test_a: ["test1", "test2"]
"#,
)
.unwrap();
let mut var = Variable::with_literal("test_a");
let index = vec![Scalar::new(-1)];
var.extend(index);
let runtime = RuntimeBuilder::new().build();
let runtime = StackFrame::new(&runtime, &globals);
let actual = var.evaluate(&runtime).unwrap();
let actual = runtime.get(&actual).unwrap();
assert_eq!(actual, ValueViewCmp::new(&"test2"));
}
#[test]
fn identifier_path_object() {
let globals: Object = serde_yaml::from_str(
r#"
test_a:
- test_h: 5
"#,
)
.unwrap();
let mut var = Variable::with_literal("test_a");
let index = vec![Scalar::new(0), Scalar::new("test_h")];
var.extend(index);
let runtime = RuntimeBuilder::new().build();
let runtime = StackFrame::new(&runtime, &globals);
let actual = var.evaluate(&runtime).unwrap();
let actual = runtime.get(&actual).unwrap();
assert_eq!(actual, ValueViewCmp::new(&5));
}
}