diff --git a/Cargo.lock b/Cargo.lock index e006877..4e486bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,15 +15,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" -[[package]] -name = "ansi_term" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" -dependencies = [ - "winapi 0.3.9", -] - [[package]] name = "atty" version = "0.2.14" @@ -87,19 +78,36 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "2.33.3" +version = "3.0.0-beta.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +checksum = "4bd1061998a501ee7d4b6d449020df3266ca3124b941ec56cf2005c3779ca142" dependencies = [ - "ansi_term", "atty", "bitflags", - "strsim 0.8.0", + "clap_derive", + "indexmap", + "lazy_static", + "os_str_bytes", + "strsim", + "termcolor", "textwrap", "unicode-width", "vec_map", ] +[[package]] +name = "clap_derive" +version = "3.0.0-beta.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b5bb0d655624a0b8770d1c178fb8ffcb1f91cc722cb08f451e3dc72465421ac" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "color-eyre" version = "0.5.11" @@ -193,6 +201,12 @@ version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0a01e0497841a3b2db4f8afa483cce65f7e96a3498bd6c541734792aeac8fe7" +[[package]] +name = "hashbrown" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" + [[package]] name = "heck" version = "0.3.3" @@ -217,6 +231,16 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" +[[package]] +name = "indexmap" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -289,6 +313,12 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" +[[package]] +name = "os_str_bytes" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85" + [[package]] name = "owo-colors" version = "1.3.0" @@ -394,48 +424,18 @@ name = "similar-sort" version = "2.0.0" dependencies = [ "bump_alloc", + "clap", "color-eyre", "rayon", - "strsim 0.10.0", - "structopt", + "strsim", ] -[[package]] -name = "strsim" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" - [[package]] name = "strsim" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" -[[package]] -name = "structopt" -version = "0.3.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69b041cdcb67226aca307e6e7be44c8806423d83e018bd662360a93dabce4d71" -dependencies = [ - "clap", - "lazy_static", - "structopt-derive", -] - -[[package]] -name = "structopt-derive" -version = "0.4.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7813934aecf5f51a54775e00068c237de98489463968231a51746bbbc03f9c10" -dependencies = [ - "heck", - "proc-macro-error", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "syn" version = "1.0.75" @@ -448,10 +448,19 @@ dependencies = [ ] [[package]] -name = "textwrap" -version = "0.11.0" +name = "termcolor" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "203008d98caf094106cfaba70acfed15e18ed3ddb7d94e49baec153a2b462789" dependencies = [ "unicode-width", ] @@ -576,6 +585,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi 0.3.9", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 2a3e86a..01189a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ edition = "2018" [dependencies] bump_alloc = "0.1.0" +clap = "=3.0.0-beta.2" color-eyre = "0.5.7" rayon = "1.5.1" strsim = "0.10.0" -structopt = "0.3.22" diff --git a/src/main.rs b/src/main.rs index 20e103c..058c3d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,12 @@ -use color_eyre::eyre::{Result, WrapErr}; +use clap::{crate_authors, crate_version, App, Arg}; +use color_eyre::eyre::{ContextCompat, Result, WrapErr}; use rayon::prelude::*; use std::io::{self, stdin, stdout, BufRead, BufWriter, Write}; use strsim::levenshtein; -use structopt::StructOpt; #[global_allocator] static A: bump_alloc::BumpAlloc = bump_alloc::BumpAlloc::new(); -// 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); @@ -26,7 +17,18 @@ fn main() { fn try_main() -> Result<()> { color_eyre::install()?; - let opts = Opts::from_args(); + let matches = App::new("similar-sort") + .version(crate_version!()) + .author(crate_authors!()) + .about( + "works like `sort`, but sorts according to edit distance instead of alphanumerically.", + ) + .arg(Arg::new("target").about("sort according to distance from this string")) + .get_matches(); + + let target = matches + .value_of("target") + .context("could not retrieve target from args. Internal error; please report!")?; let lines: Vec = stdin() .lock() @@ -36,7 +38,7 @@ fn try_main() -> Result<()> { let mut distances: Vec<(usize, &String)> = lines .iter() - .map(|candidate| (levenshtein(&opts.target, candidate), candidate)) + .map(|candidate| (levenshtein(target, candidate), candidate)) .collect(); distances.par_sort_unstable_by_key(|x| x.0);