Skip to content

Commit

Permalink
use structopt instead of clap directly
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Aug 24, 2021
1 parent dafad04 commit 79949ff
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 19 deletions.
14 changes: 14 additions & 0 deletions BENCHMARKING.md
Expand Up @@ -71,3 +71,17 @@ Benchmark #1: ./target/release/similar-sort benchmark < /usr/share/dict/words
```

So, no to that too!

## Removing arg parsing overhead?

What if it's creating that big Clap struct that's causing problems?
Let's give structopt a try (and then move to deriving from Clap once 3.0.0 is finally released.)

```
$ hyperfine './target/release/similar-sort benchmark < /usr/share/dict/words'
Benchmark #1: ./target/release/similar-sort benchmark < /usr/share/dict/words
Time (mean ± σ): 667.5 ms ± 7.1 ms [User: 4.158 s, System: 0.031 s]
Range (min … max): 658.3 ms … 678.2 ms 10 runs
```

Ok, seems fine!
71 changes: 70 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -7,7 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = "2.33.3"
structopt = "0.3.22"
color-eyre = "0.5.7"
rayon = "1.5.1"
strsim = "0.10.0"
30 changes: 13 additions & 17 deletions src/main.rs
@@ -1,8 +1,17 @@
use clap::{crate_authors, crate_version, App, Arg};
use color_eyre::eyre::{ContextCompat, Result, WrapErr};
use color_eyre::eyre::{Result, WrapErr};
use rayon::prelude::*;
use std::io::{self, stdin, stdout, BufRead, BufWriter, Write};
use strsim::levenshtein;
use structopt::StructOpt;

// works like `sort`, but sorts according to Levenshtein distance instead of
// alphanumerically.
#[derive(StructOpt)]
#[structopt(name = "similar-sort")]
struct Opts {
/// sort according to distance from this string
target: String,
}

fn main() {
if let Err(err) = try_main() {
Expand All @@ -14,28 +23,15 @@ fn main() {
fn try_main() -> Result<()> {
color_eyre::install()?;

let matches = App::new("similar-sort")
.version(crate_version!())
.author(crate_authors!())
.about("works like `sort`, but sorts according to Levenshtein distance instead of alphanumerically")
.arg(
Arg::with_name("target")
.value_name("TARGET")
.help("sort according to distance from this string")
.required(true)
).get_matches();

let target = matches.value_of("target").context(
"could not get the target value. This is an internal error and should be reported.",
)?;
let opts = Opts::from_args();

let mut lines: Vec<String> = stdin()
.lock()
.lines()
.collect::<io::Result<Vec<String>>>()
.context("could not read lines from stdin")?;

lines.par_sort_by_key(|candidate| levenshtein(target, candidate));
lines.par_sort_by_key(|candidate| levenshtein(&opts.target, candidate));

let mut out = BufWriter::new(stdout());
for candidate in lines {
Expand Down

0 comments on commit 79949ff

Please sign in to comment.