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
23 changes: 13 additions & 10 deletions engine/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ static A: System = System;
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use std::{borrow::Cow, clone::Clone, fmt::Debug, net::IpAddr};
use wirefilter::{
ExecutionContext, FilterAst, FunctionArgKind, FunctionArgs, GetType, LhsValue, Scheme,
ExecutionContext, FilterAst, FunctionArgKind, FunctionArgs, GetType, LhsValue, SchemeBuilder,
SimpleFunctionDefinition, SimpleFunctionImpl, SimpleFunctionParam, Type,
};

Expand Down Expand Up @@ -79,11 +79,12 @@ impl<T: 'static + Copy + Debug + Into<LhsValue<'static>>> FieldBench<'_, T> {
let mut group = c.benchmark_group("parsing");

group.bench_function(name, {
let mut scheme = Scheme::default();
scheme.add_field(field, ty).unwrap();
let mut builder = SchemeBuilder::default();
builder.add_field(field, ty).unwrap();
for (name, function) in functions {
scheme.add_function(name, function.clone()).unwrap();
builder.add_function(name, function.clone()).unwrap();
}
let scheme = builder.build();
move |b: &mut Bencher| {
b.iter(|| scheme.parse(filter).unwrap());
}
Expand All @@ -94,11 +95,12 @@ impl<T: 'static + Copy + Debug + Into<LhsValue<'static>>> FieldBench<'_, T> {
let mut group = c.benchmark_group("compilation");

group.bench_function(name, {
let mut scheme = Scheme::default();
scheme.add_field(field, ty).unwrap();
let mut builder = SchemeBuilder::default();
builder.add_field(field, ty).unwrap();
for (name, function) in functions {
scheme.add_function(name, function.clone()).unwrap();
builder.add_function(name, function.clone()).unwrap();
}
let scheme = builder.build();
move |b: &mut Bencher| {
let filter = scheme.parse(filter).unwrap();

Expand All @@ -111,11 +113,12 @@ impl<T: 'static + Copy + Debug + Into<LhsValue<'static>>> FieldBench<'_, T> {
let mut group = c.benchmark_group("execution");

group.bench_with_input(name, values, {
let mut scheme = Scheme::default();
scheme.add_field(field, ty).unwrap();
let mut builder = SchemeBuilder::default();
builder.add_field(field, ty).unwrap();
for (name, function) in functions {
scheme.add_function(name, function.clone()).unwrap();
builder.add_function(name, function.clone()).unwrap();
}
let scheme = builder.build();
move |b: &mut Bencher, values: &[T]| {
let filter = scheme.parse(filter).unwrap();

Expand Down
6 changes: 4 additions & 2 deletions engine/examples/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
.nth(1)
.expect("Expected an input as a command-line argument");

let mut scheme = Scheme! {
let mut builder = Scheme! {
ip: Ip,
str: Bytes,
int: Int,
Expand All @@ -23,7 +23,7 @@ fn main() {
bool_arr: Array(Bool),
};

scheme
builder
.add_function(
"panic",
SimpleFunctionDefinition {
Expand All @@ -41,6 +41,8 @@ fn main() {
)
.unwrap();

let scheme = builder.build();

match scheme.parse(&filter) {
Ok(res) => println!("{res:#?}"),
Err(err) => println!("{err}"),
Expand Down
18 changes: 9 additions & 9 deletions engine/src/ast/field_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ mod tests {
}

static SCHEME: LazyLock<Scheme> = LazyLock::new(|| {
let mut scheme: Scheme = Scheme! {
let mut builder = Scheme! {
http.cookies: Array(Bytes),
http.headers: Map(Bytes),
http.host: Bytes,
Expand All @@ -816,7 +816,7 @@ mod tests {
array.of.bool: Array(Bool),
http.parts: Array(Array(Bytes)),
};
scheme
builder
.add_function(
"any",
SimpleFunctionDefinition {
Expand All @@ -830,7 +830,7 @@ mod tests {
},
)
.unwrap();
scheme
builder
.add_function(
"echo",
SimpleFunctionDefinition {
Expand All @@ -844,7 +844,7 @@ mod tests {
},
)
.unwrap();
scheme
builder
.add_function(
"lowercase",
SimpleFunctionDefinition {
Expand All @@ -858,7 +858,7 @@ mod tests {
},
)
.unwrap();
scheme
builder
.add_function(
"concat",
SimpleFunctionDefinition {
Expand All @@ -878,10 +878,10 @@ mod tests {
},
)
.unwrap();
scheme
builder
.add_function("filter", FilterFunction::new())
.unwrap();
scheme
builder
.add_function(
"len",
SimpleFunctionDefinition {
Expand All @@ -895,10 +895,10 @@ mod tests {
},
)
.unwrap();
scheme
builder
.add_list(Type::Int, Box::new(NumMListDefinition {}))
.unwrap();
scheme
builder.build()
});

fn field(name: &'static str) -> Field<'static> {
Expand Down
14 changes: 7 additions & 7 deletions engine/src/ast/function_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ mod tests {
}

static SCHEME: LazyLock<Scheme> = LazyLock::new(|| {
let mut scheme = Scheme! {
let mut builder = Scheme! {
http.headers: Map(Bytes),
http.host: Bytes,
http.request.headers.names: Array(Bytes),
Expand All @@ -602,7 +602,7 @@ mod tests {
ssl: Bool,
tcp.port: Int,
};
scheme
builder
.add_function(
"any",
SimpleFunctionDefinition {
Expand All @@ -616,7 +616,7 @@ mod tests {
},
)
.unwrap();
scheme
builder
.add_function(
"echo",
SimpleFunctionDefinition {
Expand All @@ -639,7 +639,7 @@ mod tests {
},
)
.unwrap();
scheme
builder
.add_function(
"lower",
SimpleFunctionDefinition {
Expand All @@ -653,7 +653,7 @@ mod tests {
},
)
.unwrap();
scheme
builder
.add_function(
"regex_replace",
SimpleFunctionDefinition {
Expand All @@ -677,7 +677,7 @@ mod tests {
},
)
.unwrap();
scheme
builder
.add_function(
"len",
SimpleFunctionDefinition {
Expand All @@ -691,7 +691,7 @@ mod tests {
},
)
.unwrap();
scheme
builder.build()
});

#[test]
Expand Down
18 changes: 9 additions & 9 deletions engine/src/ast/index_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ mod tests {
use super::*;
use crate::{
ast::field_expr::IdentifierExpr, Array, FieldIndex, FilterParser, FunctionArgKind,
FunctionArgs, FunctionCallArgExpr, FunctionCallExpr, Scheme, SimpleFunctionDefinition,
SimpleFunctionImpl, SimpleFunctionParam,
FunctionArgs, FunctionCallArgExpr, FunctionCallExpr, Scheme, SchemeBuilder,
SimpleFunctionDefinition, SimpleFunctionImpl, SimpleFunctionParam,
};
use std::sync::LazyLock;

Expand All @@ -574,17 +574,17 @@ mod tests {
}

static SCHEME: LazyLock<Scheme> = LazyLock::new(|| {
let mut scheme = Scheme::new();
scheme
let mut builder = SchemeBuilder::new();
builder
.add_field("test", Type::Array(Type::Bytes.into()))
.unwrap();
scheme
builder
.add_field("test2", Type::Array(Type::Array(Type::Bytes.into()).into()))
.unwrap();
scheme
builder
.add_field("map", Type::Map(Type::Bytes.into()))
.unwrap();
scheme
builder
.add_function(
"array",
SimpleFunctionDefinition {
Expand All @@ -598,7 +598,7 @@ mod tests {
},
)
.unwrap();
scheme
builder
.add_function(
"array2",
SimpleFunctionDefinition {
Expand All @@ -612,7 +612,7 @@ mod tests {
},
)
.unwrap();
scheme
builder.build()
});

#[test]
Expand Down
3 changes: 2 additions & 1 deletion engine/src/ast/logical_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ fn test() {
at: Array(Bool),
af: Array(Bool),
aat: Array(Array(Bool)),
};
}
.build();

let ctx = &mut ExecutionContext::new(scheme);

Expand Down
8 changes: 4 additions & 4 deletions engine/src/ast/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ mod tests {
use std::sync::LazyLock;

static SCHEME: LazyLock<Scheme> = LazyLock::new(|| {
let mut scheme = Scheme! {
let mut builder = Scheme! {
http.headers: Map(Bytes),
http.request.headers.names: Array(Bytes),
http.request.headers.values: Array(Bytes),
Expand All @@ -234,7 +234,7 @@ mod tests {
ssl: Bool,
tcp.port: Int,
};
scheme
builder
.add_function(
"echo",
SimpleFunctionDefinition {
Expand All @@ -248,10 +248,10 @@ mod tests {
},
)
.unwrap();
scheme
builder
.add_list(Type::Bytes, Box::new(AlwaysList {}))
.unwrap();
scheme
builder.build()
});

#[test]
Expand Down
41 changes: 20 additions & 21 deletions engine/src/execution_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ impl Serialize for ExecutionContext<'_> {
fn test_field_value_type_mismatch() {
use crate::types::Type;

let scheme = Scheme! { foo: Int };
let scheme = Scheme! { foo: Int }.build();

let mut ctx = ExecutionContext::<()>::new(&scheme);

Expand All @@ -370,11 +370,11 @@ fn test_field_value_type_mismatch() {

#[test]
fn test_scheme_mismatch() {
let scheme = Scheme! { foo: Bool };
let scheme = Scheme! { foo: Bool }.build();

let mut ctx = ExecutionContext::<()>::new(&scheme);

let scheme2 = Scheme! { foo: Bool };
let scheme2 = Scheme! { foo: Bool }.build();

assert_eq!(
ctx.set_field_value(scheme2.get_field("foo").unwrap(), LhsValue::Bool(false)),
Expand All @@ -391,20 +391,18 @@ fn test_serde() {
use std::net::IpAddr;
use std::str::FromStr;

let mut scheme = Scheme::new();
scheme.add_field("bool", Type::Bool).unwrap();
scheme.add_field("ip", Type::Ip).unwrap();
scheme.add_field("str", Type::Bytes).unwrap();
scheme.add_field("bytes", Type::Bytes).unwrap();
scheme.add_field("num", Type::Int).unwrap();
scheme.add_field("min_num", Type::Int).unwrap();
scheme.add_field("max_num", Type::Int).unwrap();
scheme
.add_field("arr", Type::Array(Type::Bool.into()))
.unwrap();
scheme
.add_field("map", Type::Map(Type::Int.into()))
.unwrap();
let scheme = Scheme! {
bool: Bool,
ip: Ip,
str: Bytes,
bytes: Bytes,
num: Int,
min_num: Int,
max_num: Int,
arr: Array(Bool),
map: Map(Int),
}
.build();

let mut ctx = ExecutionContext::new(&scheme);

Expand Down Expand Up @@ -550,13 +548,14 @@ fn test_serde() {

#[test]
fn test_clear() {
use crate::types::Type;
use std::net::IpAddr;
use std::str::FromStr;

let mut scheme = Scheme::new();
scheme.add_field("bool", Type::Bool).unwrap();
scheme.add_field("ip", Type::Ip).unwrap();
let scheme = Scheme! {
bool: Bool,
ip: Ip,
}
.build();

let bool_field = scheme.get_field("bool").unwrap();
let ip_field = scheme.get_field("ip").unwrap();
Expand Down
4 changes: 2 additions & 2 deletions engine/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ mod tests {

#[test]
fn test_scheme_mismatch() {
let scheme1 = Scheme! { foo: Int };
let scheme2 = Scheme! { foo: Int, bar: Int };
let scheme1 = Scheme! { foo: Int }.build();
let scheme2 = Scheme! { foo: Int, bar: Int }.build();
let filter = scheme1.parse("foo == 42").unwrap().compile();
let ctx = ExecutionContext::new(&scheme2);

Expand Down
Loading
Loading