use std::sync::Arc;
use wirerust::*;
fn main() -> Result<(), WirerustError> {
let schema = FilterSchemaBuilder::new()
.field("http.method", FieldType::Bytes)
.field("port", FieldType::Int)
.field("tags", FieldType::Array(Box::new(FieldType::Bytes)))
.build();
let mut functions = FunctionRegistry::new();
register_builtins(&mut functions);
let filter_str = r#"http.method == "GET" && port in {80 443} && len(tags) == 2"#;
let expr = FilterParser::parse(filter_str, &schema)?;
println!("Parsed AST: {:#?}", expr);
let filter = CompiledFilter::new(expr, Arc::new(schema.clone()), Arc::new(functions));
let mut ctx = FilterContext::new();
ctx.set(
"http.method",
LiteralValue::Bytes(Arc::new(b"GET".to_vec()).into()),
&schema,
)?;
ctx.set("port", LiteralValue::Int(80), &schema)?;
ctx.set(
"tags",
LiteralValue::Array(
Arc::new(vec![
LiteralValue::Bytes(Arc::new(b"foo".to_vec()).into()),
LiteralValue::Bytes(Arc::new(b"bar".to_vec()).into()),
])
.into(),
),
&schema,
)?;
let result = filter.execute(&ctx);
match result {
Ok(val) => println!("Filter matches: {}", val),
Err(e) => println!("Filter error: {}", e),
}
Ok(())
}