From df4e7078f92a9338bfe8386a4188de2e928a3605 Mon Sep 17 00:00:00 2001 From: Isaac Whitfield Date: Fri, 5 Jan 2024 19:40:48 +0000 Subject: [PATCH] Add examples of programmatic API --- Cargo.toml | 5 ++++- README.md | 4 ++++ examples/basic.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 examples/basic.rs diff --git a/Cargo.toml b/Cargo.toml index 738cfb0..4c70200 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,9 +17,9 @@ cli = ["bytelines", "bytesize", "clap", "cli-table", "format_num"] [dependencies] growable-bloom-filter = "2.1" identity-hash = "0.1" -xxhash-rust = { version = "0.8", features = ["xxh64"] } strum = "0.25" strum_macros = "0.25" +xxhash-rust = { version = "0.8", features = ["xxh64"] } # optional dependencies only use for CLI bytelines = { version = "2.4", optional = true } @@ -27,3 +27,6 @@ bytesize = { version = "1.3", optional = true } clap = { version = "4.4", optional = true, features = ["derive"] } cli-table = { version = "0.4", optional = true } format_num = { version = "0.1", optional = true } + +[dev-dependencies] +jen = "1.6" diff --git a/README.md b/README.md index 36a8bc8..7565a3b 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ You should disable the default features as it includes several dependencies whic ### Examples +Below are a few examples of using the Runiq CLI to filter duplicates out of input text. + ```shell $ cat << EOF >> input.txt this is a unique line @@ -47,6 +49,8 @@ this is a duplicate line this is another unique line ``` +For examples of the programmatic API, please see [the examples](./examples/basic.rs). + ### Filters Runiq comes with several "filters", which control exactly how uniqueness is verified. Each of these filters has different use cases, and excels in different ways. diff --git a/examples/basic.rs b/examples/basic.rs new file mode 100644 index 0000000..c3f6c86 --- /dev/null +++ b/examples/basic.rs @@ -0,0 +1,27 @@ +use jen::generator::Generator; +use runiq::{Filter, QuickFilter}; + +fn main() { + // First we create a filter to detect duplicates + let mut filter = QuickFilter::default(); + + // As well as a template to generate random inputs + let template = "{{ integer(start=1, end=6) }}"; + + // Then we generate some random input values + let inputs = Generator::from_string(template) + .unwrap() + .take(20) + .collect::>(); + + // And filter uniques + let outputs = inputs + .iter() + .filter(|v| filter.detect(v.as_bytes())) + .map(|v| v.to_owned()) + .collect::>(); + + // Before we print the before/after to the console + println!("Generate values: {}", inputs.join(", ")); + println!("Filtered values: {}", outputs.join(", ")); +}