Wirefilter is an execution engine for Wireshark-like filters. The workflow consists of three main steps:
- Define a
Scheme: Create a map of possible filter fields and their types (e.g., Bytes, Int) using the Scheme! macro. - Parse and Compile: Use the
Scheme to parse a filter string into an Abstract Syntax Tree (AST), then call .compile() on the AST to produce an executable filter. - Execute: Create an
ExecutionContext from the Scheme, populate it with runtime field values using .set_field_value(), and run the filter using .execute(&ctx).
This allows you to test filter expressions against dynamic runtime data.
use wirefilter::{ExecutionContext, Scheme};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 1. Define the schema
let scheme = Scheme! {
http.method: Bytes,
http.ua: Bytes,
port: Int,
}
.build();
// 2. Parse and Compile
let ast = scheme.parse(
r"#""
http.method != "POST" &&
not http.ua matches "(googlebot|facebook)" &&
port in {80 443}
""#,
)?;
let filter = ast.compile();
// 3. Execute against context
let mut ctx = ExecutionContext::new(&scheme);
ctx.set_field_value(scheme.get_field("http.method").unwrap(), "GET")?;
ctx.set_field_value(
scheme.get_field("http.ua").unwrap(),
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0",
)?;
ctx.set_field_value(scheme.get_field("port").unwrap(), 443)?;
println!("Filter matches: {:?}", filter.execute(&ctx)?); // true
Ok(())
}