/
main.rs
42 lines (34 loc) · 1.04 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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() {
eprintln!("{:?}", err);
std::process::exit(1);
}
}
fn try_main() -> Result<()> {
color_eyre::install()?;
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(&opts.target, candidate));
let mut out = BufWriter::new(stdout());
for candidate in lines {
writeln!(out, "{}", candidate).context("could not write to stdout")?;
}
Ok(())
}