Crates.io | streamdal-wasm-detective |
lib.rs | streamdal-wasm-detective |
version | 0.0.39 |
source | src |
created_at | 2023-10-21 01:37:50.364801 |
updated_at | 2024-01-05 15:09:37.611232 |
description | Data matcher library used by Streamdal WASM functions |
homepage | |
repository | https://github.com/streamdal/wasm-detective |
max_upload_size | |
id | 1009718 |
size | 125,307 |
Rust helper lib for performing value matching in streamdal/wasm
Wasm functions.
For available matchers, look at the enums listed in streamdal/protos.
cargo add streamdal-protos
cargo add streamdal-wasm-detective
fn main() {
let det = detective::Detective::new();
let sample_json = r#"{
"field1": {
"field2": "2"
}
}"#;
let request = Request {
match_type: DetectiveType::DETECTIVE_TYPE_HAS_FIELD,
data: &sample_json.as_bytes().to_vec(),
path: "field1".to_string(),
args: vec!["1".to_string()],
negate: false,
};
match det.matches(&request) {
Ok(value) => println!("Result: {:#?}", value),
Err(err) => println!("Error: {:#?}", err),
}
}
Regex-based matchers are currently slow because we have to compile the pattern on every call.
This will improve when we implement K/V functionality in SDK's.
The idea is that WASM funcs will be given the ability to GET/PUT items in cache, so detective
would be wired up to accept a param that is a trait that allows working with the cache funcs.
If K/V trait is provided to detective
- before compiling a regex pattern, it would first check if the cache already contains it. If yes, it'll use that, if not, it'll compile and put it in the cache.
~DS 06-29-2023
The library must be tested using Rust nightly (because we use #![feature(test)]
to enable the ability to bench).
To install nightly: rustup install nightly
To run tests using nightly: cargo +nightly test
To run benches using nightly: cargo +nightly bench
You can also set nightly as default using rustup default nightly
.
Any changes made under libs/wasm-detective will be automatically released to crates.io with a version bump
Valid as of 2023-11-14
test test_bench::bench_credit_card ... bench: 730 ns/iter (+/- 8)
test test_bench::bench_credit_card_payload ... bench: 7,821 ns/iter (+/- 90)
test test_bench::bench_email ... bench: 531 ns/iter (+/- 8)
test test_bench::bench_email_payload ... bench: 6,150 ns/iter (+/- 78)
test test_bench::bench_email_utf8 ... bench: 907 ns/iter (+/- 9)
test test_bench::bench_has_field ... bench: 386 ns/iter (+/- 11)
test test_bench::bench_hostname ... bench: 410 ns/iter (+/- 6)
test test_bench::bench_ipv4_address ... bench: 213 ns/iter (+/- 3)
test test_bench::bench_ipv6_address ... bench: 320 ns/iter (+/- 2)
test test_bench::bench_mac_address ... bench: 343 ns/iter (+/- 7)
test test_bench::bench_semver ... bench: 378 ns/iter (+/- 6)
test test_bench::bench_string_contains_all ... bench: 218 ns/iter (+/- 4)
test test_bench::bench_string_contains_any ... bench: 221 ns/iter (+/- 1)
test test_bench::bench_uuid ... bench: 402 ns/iter (+/- 6)