Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions engine/src/ast/field_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ impl Expr for ComparisonExpr {
mod tests {
use super::*;
use crate::{
BytesFormat, FieldRef, LhsValue, ParserSettings,
BytesFormat, FieldRef, LhsValue, ParserSettings, TypedMap,
ast::{
function_expr::{FunctionCallArgExpr, FunctionCallExpr},
logical_expr::LogicalExpr,
Expand Down Expand Up @@ -1586,18 +1586,18 @@ mod tests {
let expr = expr.compile();
let ctx = &mut ExecutionContext::new(&SCHEME);

let headers = LhsValue::Map({
let mut map = Map::new(Type::Bytes);
map.insert(b"host", "example.org").unwrap();
let headers = LhsValue::from({
let mut map = TypedMap::new();
map.insert(b"host".to_vec().into(), "example.org");
map
});

ctx.set_field_value(field("http.headers"), headers).unwrap();
assert_eq!(expr.execute_one(ctx), false);

let headers = LhsValue::Map({
let mut map = Map::new(Type::Bytes);
map.insert(b"host", "abc.net.au").unwrap();
let headers = LhsValue::from({
let mut map = TypedMap::new();
map.insert(b"host".to_vec().into(), "abc.net.au");
map
});

Expand Down Expand Up @@ -2186,11 +2186,11 @@ mod tests {
let expr = expr.compile();
let ctx = &mut ExecutionContext::new(&SCHEME);

let headers = LhsValue::Map({
let mut map = Map::new(Type::Bytes);
map.insert(b"0", "one").unwrap();
map.insert(b"1", "two").unwrap();
map.insert(b"2", "three").unwrap();
let headers = LhsValue::from({
let mut map = TypedMap::new();
map.insert(b"0".to_vec().into(), "one");
map.insert(b"1".to_vec().into(), "two");
map.insert(b"2".to_vec().into(), "three");
map
});
ctx.set_field_value(field("http.headers"), headers).unwrap();
Expand Down Expand Up @@ -2267,11 +2267,11 @@ mod tests {
let expr = expr.compile();
let ctx = &mut ExecutionContext::new(&SCHEME);

let headers = LhsValue::Map({
let mut map = Map::new(Type::Bytes);
map.insert(b"0", "one").unwrap();
map.insert(b"1", "two").unwrap();
map.insert(b"2", "three").unwrap();
let headers = LhsValue::from({
let mut map = TypedMap::new();
map.insert(b"0".to_vec().into(), "one");
map.insert(b"1".to_vec().into(), "two");
map.insert(b"2".to_vec().into(), "three");
map
});
ctx.set_field_value(field("http.headers"), headers).unwrap();
Expand Down
23 changes: 11 additions & 12 deletions engine/src/execution_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,7 @@ fn test_scheme_mismatch() {

#[test]
fn test_serde() {
use crate::lhs_types::{Array, Map};
use crate::types::Type;
use crate::lhs_types::{Array, TypedMap};
use std::net::IpAddr;
use std::str::FromStr;

Expand Down Expand Up @@ -491,9 +490,9 @@ fn test_serde() {

assert_eq!(
ctx.set_field_value(scheme.get_field("map").unwrap(), {
let mut map = Map::new(Type::Int);
map.insert(b"leet", 1337).unwrap();
map.insert(b"tabs", 25).unwrap();
let mut map = TypedMap::<i64>::new();
map.insert(b"leet".to_vec().into(), 1337);
map.insert(b"tabs".to_vec().into(), 25);
map
}),
Ok(None),
Expand Down Expand Up @@ -535,16 +534,16 @@ fn test_serde() {

assert_eq!(
ctx.set_field_value(scheme.get_field("map").unwrap(), {
let mut map = Map::new(Type::Int);
map.insert(b"leet", 1337).unwrap();
map.insert(b"tabs", 25).unwrap();
map.insert(b"a\xFF\xFFb", 17).unwrap();
let mut map = TypedMap::<i64>::new();
map.insert(b"leet".to_vec().into(), 1337);
map.insert(b"tabs".to_vec().into(), 25);
map.insert(b"a\xFF\xFFb".to_vec().into(), 17);
map
}),
Ok(Some({
let mut map = Map::new(Type::Int);
map.insert(b"leet", 1337).unwrap();
map.insert(b"tabs", 25).unwrap();
let mut map = TypedMap::<i64>::new();
map.insert(b"leet".to_vec().into(), 1337);
map.insert(b"tabs".to_vec().into(), 25);
map.into()
})),
);
Expand Down
100 changes: 5 additions & 95 deletions engine/src/lhs_types/array.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{
lhs_types::AsRefIterator,
types::{
CompoundType, GetType, IntoValue, LhsValue, LhsValueMut, LhsValueSeed, Type,
TypeMismatchError,
},
types::{CompoundType, GetType, IntoValue, LhsValue, LhsValueSeed, Type, TypeMismatchError},
};
use serde::{
Serialize, Serializer,
Expand Down Expand Up @@ -53,11 +50,6 @@ impl<'a> InnerArray<'a> {
self.as_vec().get_mut(idx)
}

#[inline]
fn insert(&mut self, idx: usize, value: LhsValue<'a>) {
self.as_vec().insert(idx, value)
}

#[inline]
fn push(&mut self, value: LhsValue<'a>) {
self.as_vec().push(value)
Expand Down Expand Up @@ -121,43 +113,6 @@ impl<'a> Array<'a> {
self.data.get(idx)
}

/// Get a mutable reference to an element if it exists
pub fn get_mut(&mut self, idx: usize) -> Option<LhsValueMut<'_, 'a>> {
self.data.get_mut(idx).map(LhsValueMut::from)
}

/// Inserts an element at index `idx`
pub fn insert(
&mut self,
idx: usize,
value: impl Into<LhsValue<'a>>,
) -> Result<(), TypeMismatchError> {
let value = value.into();
let value_type = value.get_type();
if value_type != self.val_type.into() {
return Err(TypeMismatchError {
expected: Type::from(self.val_type).into(),
actual: value_type,
});
}
self.data.insert(idx, value);
Ok(())
}

/// Push an element to the back of the array
pub fn push(&mut self, value: impl Into<LhsValue<'a>>) -> Result<(), TypeMismatchError> {
let value = value.into();
let value_type = value.get_type();
if value_type != self.val_type.into() {
return Err(TypeMismatchError {
expected: Type::from(self.val_type).into(),
actual: value_type,
});
}
self.data.push(value);
Ok(())
}

pub(crate) fn as_ref(&'a self) -> Array<'a> {
Array {
val_type: self.val_type,
Expand Down Expand Up @@ -445,51 +400,6 @@ impl<'de> DeserializeSeed<'de> for &mut Array<'de> {
}
}

/// Wrapper type around mutable `Array` to prevent
/// illegal operations like changing the type of
/// its values.
pub struct ArrayMut<'a, 'b>(&'a mut Array<'b>);

impl<'a, 'b> ArrayMut<'a, 'b> {
/// Push an element to the back of the array
#[inline]
pub fn push(&mut self, value: impl Into<LhsValue<'b>>) -> Result<(), TypeMismatchError> {
self.0.push(value)
}

/// Inserts an element at index `idx`
#[inline]
pub fn insert(
&mut self,
idx: usize,
value: impl Into<LhsValue<'b>>,
) -> Result<(), TypeMismatchError> {
self.0.insert(idx, value)
}

/// Get a mutable reference to an element if it exists
#[inline]
pub fn get_mut(&'a mut self, idx: usize) -> Option<LhsValueMut<'a, 'b>> {
self.0.get_mut(idx)
}
}

impl<'b> Deref for ArrayMut<'_, 'b> {
type Target = Array<'b>;

#[inline]
fn deref(&self) -> &Self::Target {
self.0
}
}

impl<'a, 'b> From<&'a mut Array<'b>> for ArrayMut<'a, 'b> {
#[inline]
fn from(arr: &'a mut Array<'b>) -> Self {
Self(arr)
}
}

/// Typed wrapper over an `Array` which provides
/// infaillible operations.
#[repr(transparent)]
Expand Down Expand Up @@ -703,11 +613,11 @@ mod tests {

#[test]
fn test_borrowed_eq_owned() {
let mut owned = Array::new(Type::Bytes);
let mut arr = TypedArray::new();

arr.push("borrowed");

owned
.push(LhsValue::Bytes("borrowed".as_bytes().into()))
.unwrap();
let owned = Array::from(arr);

let borrowed = owned.as_ref();

Expand Down
Loading
Loading