Skip to content

Commit 1720eb0

Browse files
committed
Replace calls to LhsValue::get_nested by LhsValue::extract_nested
This will allow further experimention with making the execution context an interface to make it agnostic about how the underlying data is stored.
1 parent 3363b07 commit 1720eb0

File tree

2 files changed

+63
-73
lines changed

2 files changed

+63
-73
lines changed

engine/src/ast/index_expr.rs

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,6 @@ pub struct IndexExpr {
3131
pub indexes: Vec<FieldIndex>,
3232
}
3333

34-
#[allow(clippy::manual_ok_err)]
35-
#[inline]
36-
pub fn ok_ref<T, E>(result: &Result<T, E>) -> Option<&T> {
37-
match result {
38-
Ok(x) => Some(x),
39-
Err(_) => None,
40-
}
41-
}
42-
4334
impl ValueExpr for IndexExpr {
4435
#[inline]
4536
fn walk<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) {
@@ -64,7 +55,7 @@ impl ValueExpr for IndexExpr {
6455
let map_each_count = self.map_each_count();
6556
let Self {
6657
identifier,
67-
indexes,
58+
mut indexes,
6859
} = self;
6960

7061
let last = match map_each_count {
@@ -86,20 +77,20 @@ impl ValueExpr for IndexExpr {
8677
IdentifierExpr::FunctionCallExpr(call) => compiler.compile_function_call_expr(call),
8778
}
8879
} else if let Some(last) = last {
80+
indexes.truncate(last);
8981
// Average path
9082
match identifier {
9183
IdentifierExpr::Field(f) => CompiledValueExpr::new(move |ctx| {
9284
ctx.get_field_value_unchecked(&f)
93-
.and_then(|value| value.get_nested(&indexes[..last]))
94-
.map(LhsValue::as_ref)
85+
.and_then(|value| value.as_ref().extract_nested(&indexes))
9586
.ok_or(ty)
9687
}),
9788
IdentifierExpr::FunctionCallExpr(call) => {
9889
let call = compiler.compile_function_call_expr(call);
9990
CompiledValueExpr::new(move |ctx| {
10091
call.execute(ctx)
10192
.ok()
102-
.and_then(|val| val.extract_nested(&indexes[..last]))
93+
.and_then(|val| val.extract_nested(&indexes))
10394
.ok_or(ty)
10495
})
10596
}
@@ -168,12 +159,13 @@ impl IndexExpr {
168159
})
169160
} else {
170161
CompiledOneExpr::new(move |ctx| {
171-
ok_ref(&call.execute(ctx))
172-
.and_then(|val| val.get_nested(&indexes))
162+
call.execute(ctx)
163+
.ok()
164+
.and_then(|val| val.extract_nested(&indexes))
173165
.map_or(
174166
default,
175167
#[inline]
176-
|val| comp.compare(val, ctx),
168+
|val| comp.compare(&val, ctx),
177169
)
178170
})
179171
}
@@ -188,11 +180,11 @@ impl IndexExpr {
188180
} else {
189181
CompiledOneExpr::new(move |ctx| {
190182
ctx.get_field_value_unchecked(&f)
191-
.and_then(|value| value.get_nested(&indexes))
183+
.and_then(|value| value.as_ref().extract_nested(&indexes))
192184
.map_or(
193185
default,
194186
#[inline]
195-
|val| comp.compare(val, ctx),
187+
|val| comp.compare(&val, ctx),
196188
)
197189
})
198190
}
@@ -213,35 +205,68 @@ impl IndexExpr {
213205
match identifier {
214206
IdentifierExpr::FunctionCallExpr(call) => {
215207
let call = compiler.compile_function_call_expr(call);
216-
CompiledVecExpr::new(move |ctx| {
217-
let comp = &comp;
218-
ok_ref(&call.execute(ctx))
219-
.and_then(|val| val.get_nested(&indexes))
220-
.map_or(
208+
if indexes.is_empty() {
209+
CompiledVecExpr::new(move |ctx| {
210+
let comp = &comp;
211+
call.execute(ctx).map_or(
221212
BOOL_ARRAY,
222213
#[inline]
223-
|val: &LhsValue<'_>| {
214+
|val: LhsValue<'_>| {
224215
TypedArray::from_iter(
225216
val.iter().unwrap().map(|item| comp.compare(item, ctx)),
226217
)
227218
},
228219
)
229-
})
220+
})
221+
} else {
222+
CompiledVecExpr::new(move |ctx| {
223+
let comp = &comp;
224+
call.execute(ctx)
225+
.ok()
226+
.and_then(|val| val.extract_nested(&indexes))
227+
.map_or(
228+
BOOL_ARRAY,
229+
#[inline]
230+
|val: LhsValue<'_>| {
231+
TypedArray::from_iter(
232+
val.iter().unwrap().map(|item| comp.compare(item, ctx)),
233+
)
234+
},
235+
)
236+
})
237+
}
230238
}
231-
IdentifierExpr::Field(f) => CompiledVecExpr::new(move |ctx| {
232-
let comp = &comp;
233-
ctx.get_field_value_unchecked(&f)
234-
.and_then(|value| value.get_nested(&indexes))
235-
.map_or(
236-
BOOL_ARRAY,
237-
#[inline]
238-
|val: &LhsValue<'_>| {
239-
TypedArray::from_iter(
240-
val.iter().unwrap().map(|item| comp.compare(item, ctx)),
239+
IdentifierExpr::Field(f) => {
240+
if indexes.is_empty() {
241+
CompiledVecExpr::new(move |ctx| {
242+
let comp = &comp;
243+
ctx.get_field_value_unchecked(&f).map_or(
244+
BOOL_ARRAY,
245+
#[inline]
246+
|val: &LhsValue<'_>| {
247+
TypedArray::from_iter(
248+
val.iter().unwrap().map(|item| comp.compare(item, ctx)),
249+
)
250+
},
251+
)
252+
})
253+
} else {
254+
CompiledVecExpr::new(move |ctx| {
255+
let comp = &comp;
256+
ctx.get_field_value_unchecked(&f)
257+
.and_then(|value| value.as_ref().extract_nested(&indexes))
258+
.map_or(
259+
BOOL_ARRAY,
260+
#[inline]
261+
|val: LhsValue<'_>| {
262+
TypedArray::from_iter(
263+
val.iter().unwrap().map(|item| comp.compare(item, ctx)),
264+
)
265+
},
241266
)
242-
},
243-
)
244-
}),
267+
})
268+
}
269+
}
245270
}
246271
}
247272

engine/src/types.rs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -698,41 +698,6 @@ impl<'a> LhsValue<'a> {
698698
}
699699
}
700700

701-
/// Retrieve an element from an LhsValue given a path item and a specified
702-
/// type.
703-
/// Returns a TypeMismatchError error if current type does not support it
704-
/// nested element.
705-
///
706-
/// Both LhsValue::Array and LhsValue::Map support nested elements.
707-
pub(crate) fn get(
708-
&'a self,
709-
item: &FieldIndex,
710-
) -> Result<Option<&'a LhsValue<'a>>, IndexAccessError> {
711-
match (self, item) {
712-
(LhsValue::Array(arr), FieldIndex::ArrayIndex(idx)) => Ok(arr.get(*idx as usize)),
713-
(_, FieldIndex::ArrayIndex(_)) => Err(IndexAccessError {
714-
index: item.clone(),
715-
actual: self.get_type(),
716-
}),
717-
(LhsValue::Map(map), FieldIndex::MapKey(key)) => Ok(map.get(key.as_bytes())),
718-
(_, FieldIndex::MapKey(_)) => Err(IndexAccessError {
719-
index: item.clone(),
720-
actual: self.get_type(),
721-
}),
722-
(_, FieldIndex::MapEach) => Err(IndexAccessError {
723-
index: item.clone(),
724-
actual: self.get_type(),
725-
}),
726-
}
727-
}
728-
729-
#[inline]
730-
pub(crate) fn get_nested(&'a self, indexes: &[FieldIndex]) -> Option<&'a LhsValue<'a>> {
731-
indexes
732-
.iter()
733-
.try_fold(self, |value, idx| value.get(idx).unwrap())
734-
}
735-
736701
pub(crate) fn extract(
737702
self,
738703
item: &FieldIndex,

0 commit comments

Comments
 (0)