suggest-0.5.1/.cargo_vcs_info.json0000644000000001360000000000100125200ustar { "git": { "sha1": "6fa465a7862e902b65b5bcf487f2605fc5ddcce0" }, "path_in_vcs": "" }suggest-0.5.1/.github/dependabot.yml000064400000000000000000000012451046102023000155020ustar 00000000000000# To get started with Dependabot version updates, you'll need to specify which # package ecosystems to update and where the package manifests are located. # Please see the documentation for all configuration options: # https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates version: 2 updates: - package-ecosystem: "github-actions" # See documentation for possible values directory: "/" # Location of package manifests schedule: interval: "monthly" - package-ecosystem: "cargo" # See documentation for possible values directory: "/" # Location of package manifests schedule: interval: "monthly" suggest-0.5.1/.github/workflows/auto-release.yml000064400000000000000000000005421046102023000200170ustar 00000000000000name: Release on: push: tags: - 'v*' jobs: auto-release: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Release uses: softprops/action-gh-release@v1 with: generate_release_notes: true draft: false env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} suggest-0.5.1/.github/workflows/rust.yml000064400000000000000000000015301046102023000164240ustar 00000000000000name: Rust on: push: branches: [ main ] pull_request: env: CARGO_TERM_COLOR: always jobs: build: strategy: fail-fast: false matrix: os: [ ubuntu-latest, macos-latest ] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - name: Build run: cargo build --verbose format: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - run: cargo fmt --all -- --check lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions-rs/clippy-check@v1 with: token: ${{ secrets.GITHUB_TOKEN }} args: --all-targets --all-features -- -D clippy::all -D warnings test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Run tests run: cargo test --verbose suggest-0.5.1/.gitignore000064400000000000000000000000101046102023000132670ustar 00000000000000/target suggest-0.5.1/Cargo.lock0000644000000142110000000000100104720ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "anstream" version = "0.6.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5" dependencies = [ "anstyle", "anstyle-parse", "anstyle-query", "anstyle-wincon", "colorchoice", "utf8parse", ] [[package]] name = "anstyle" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41ed9a86bf92ae6580e0a31281f65a1b1d867c0cc68d5346e2ae128dddfa6a7d" [[package]] name = "anstyle-parse" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e765fd216e48e067936442276d1d57399e37bce53c264d6fefbe298080cb57ee" dependencies = [ "utf8parse", ] [[package]] name = "anstyle-query" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" dependencies = [ "windows-sys", ] [[package]] name = "anstyle-wincon" version = "3.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628" dependencies = [ "anstyle", "windows-sys", ] [[package]] name = "clap" version = "4.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e578d6ec4194633722ccf9544794b71b1385c3c027efe0c55db226fc880865c" dependencies = [ "clap_builder", "clap_derive", ] [[package]] name = "clap_builder" version = "4.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4df4df40ec50c46000231c914968278b1eb05098cf8f1b3a518a95030e71d1c7" dependencies = [ "anstream", "anstyle", "clap_lex", "strsim", ] [[package]] name = "clap_derive" version = "4.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" dependencies = [ "heck", "proc-macro2", "quote", "syn", ] [[package]] name = "clap_lex" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" [[package]] name = "colorchoice" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" [[package]] name = "heck" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" [[package]] name = "lev_distance" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72d9d1bd215936bc8647ad92986bb56f3f216550b53c44ab785e3217ae33625e" [[package]] name = "proc-macro2" version = "1.0.54" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e472a104799c74b514a57226160104aa483546de37e839ec50e3c2e41dd87534" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" dependencies = [ "proc-macro2", ] [[package]] name = "strsim" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "suggest" version = "0.5.1" dependencies = [ "clap", "lev_distance", ] [[package]] name = "syn" version = "2.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79d9531f94112cfc3e4c8f5f02cb2b58f72c97b7efd85f70203cc6d8efda5927" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "unicode-ident" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd" [[package]] name = "utf8parse" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" [[package]] name = "windows-sys" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ "windows-targets", ] [[package]] name = "windows-targets" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" [[package]] name = "windows_aarch64_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" [[package]] name = "windows_i686_gnu" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" [[package]] name = "windows_i686_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" [[package]] name = "windows_x86_64_gnu" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" [[package]] name = "windows_x86_64_gnullvm" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" [[package]] name = "windows_x86_64_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" suggest-0.5.1/Cargo.toml0000644000000017430000000000100105230ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" name = "suggest" version = "0.5.1" authors = ["Ken Matsui <26405363+ken-matsui@users.noreply.github.com>"] description = "A minimal library to provide similar name suggestions like \"Did you mean?\"" homepage = "https://github.com/ken-matsui/suggest#readme" documentation = "https://docs.rs/suggest" readme = "README.md" license = "MIT" repository = "https://github.com/ken-matsui/suggest/" [dependencies.lev_distance] version = "0.1.1" [dev-dependencies.clap] version = "4.4" features = ["derive"] suggest-0.5.1/Cargo.toml.orig000064400000000000000000000010151046102023000141740ustar 00000000000000[package] authors = ["Ken Matsui <26405363+ken-matsui@users.noreply.github.com>"] description = "A minimal library to provide similar name suggestions like \"Did you mean?\"" documentation = "https://docs.rs/suggest" edition = "2021" homepage = "https://github.com/ken-matsui/suggest#readme" license = "MIT" name = "suggest" readme = "README.md" repository = "https://github.com/ken-matsui/suggest/" version = "0.5.1" [dependencies] lev_distance = "0.1.1" [dev-dependencies] clap = {version = "4.4", features = ["derive"]} suggest-0.5.1/LICENSE000064400000000000000000000020531046102023000123150ustar 00000000000000MIT License Copyright (c) 2021 Ken Matsui Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. suggest-0.5.1/README.md000064400000000000000000000045721046102023000125770ustar 00000000000000# suggest [![crates.io version](https://img.shields.io/crates/v/suggest.svg)](https://crates.io/crates/suggest) [![crates.io downloads](https://img.shields.io/crates/d/suggest.svg)](https://crates.io/crates/suggest) A minimal library to provide similar name suggestions like "Did you mean?" This library provides suggestion traits for all collection types in the standard library. This library is intended to suggest a candidate from a list of unknown suggestions until runtime, in addition to the suggestion feature already available in [`clap`](https://github.com/clap-rs/clap#default-features). ## Installation Add the following to your `Cargo.toml`: ```toml [dependencies] suggest = "0.5" ``` ## Examples ### Simple case This example can be executed by the `cargo run --example simple` command. ```rust use suggest::Suggest; fn main() { let input = "instakk"; let list_commands = vec!["update", "install"]; if list_commands.contains(&input) { return; } if let Some(sugg) = list_commands.suggest(input) { println!("No command named `{}` found.", input); println!("Did you mean `{}`?", sugg); } } ``` ```shell $ cargo run No command named `instakk` found. Did you mean `install`? ``` ### Specifying distance ```rust use suggest::Suggest; fn main() { let input = "paoc"; let list_commands = vec!["poac", "poacpp"]; if list_commands.contains(&input) { return; } if let Some(sugg) = list_commands.suggest_by(input, 2) { println!("No command named `{}` found.", input); println!("Did you mean `{}`?", sugg); } } ``` ```shell $ cargo run No command named `paoc` found. Did you mean `poac`? ``` ## Supported types Please let me know if anything is left out through issues or pull requests. ### Sequences * `LinkedList` * `VecDeque` * `Vec` ### Maps * `HashMap` * `BTreeMap` To suggest keys, use `suggest::SuggestKey` trait. ### Sets * `BTreeSet` * `HashSet` ### Misc * `BinaryHeap` * `[T; N]`: primitive array * `[T]`: slices ## Contribution Contributions, including issues and pull requests, are very welcome. ### Build ```bash $ cargo build ``` ### Test ```bash $ cargo build $ cargo test ``` ### Publish #### [GitHub Releases](https://github.com/ken-matsui/suggest/tags) ```bash $ git tag v0.1.0 $ git push origin v0.1.0 ``` #### [crates.io](https://crates.io/) ```bash $ cargo publish ``` suggest-0.5.1/examples/full.rs000064400000000000000000000024621046102023000144420ustar 00000000000000use std::process::exit; use clap::Parser; use suggest::Suggest; #[derive(Parser, Debug)] #[clap(version, about, long_about = None)] struct Args { /// Input to check if similar name exists input: String, /// Values of similar names values: Vec, /// Disable console outputs #[clap(short, long)] quiet: bool, /// Levenshtein Distance #[clap(short, long)] distance: Option, } const SUCCESS: i32 = 0; const FAILURE: i32 = 1; fn main() { let args = Args::parse(); if args.values.contains(&args.input) { if !args.quiet { eprintln!("The same value with the `{}` input exists.", args.input); }; exit(FAILURE); } else if let Some(dist) = args.distance { if let Some(sugg) = args.values.suggest_by(&args.input, dist) { if !args.quiet { println!("The `{}` input is similar to `{}`.", args.input, sugg); }; exit(SUCCESS); } } else if let Some(sugg) = args.values.suggest(&args.input) { if !args.quiet { println!("The `{}` input is similar to `{}`.", args.input, sugg); }; exit(SUCCESS); }; if !args.quiet { println!("No similar value for the `{}` input was found.", args.input); }; exit(FAILURE); } suggest-0.5.1/examples/simple.rs000064400000000000000000000005271046102023000147710ustar 00000000000000use suggest::Suggest; fn main() { let input = "instakk"; let list_commands = vec!["update", "install"]; if list_commands.contains(&input) { return; } if let Some(sugg) = list_commands.suggest(input) { println!("No command named `{}` found.", input); println!("Did you mean `{}`?", sugg); } } suggest-0.5.1/src/lib.rs000064400000000000000000000137041046102023000132200ustar 00000000000000//! This library provides suggestion traits for all collection types in the standard library. //! //! # Example //! ``` //! use suggest::Suggest; //! //! let input = "instakk"; //! //! let list_commands = vec!["update", "install"]; //! if list_commands.contains(&input) { //! return; //! } //! //! if let Some(sugg) = list_commands.suggest(input) { //! println!("No command named `{}` found.", input); //! println!("Did you mean `{}`?", sugg); //! } //! ``` //! //! ```console //! $ cargo run //! No command named `instakk` found. //! Did you mean `install`? //! ``` use lev_distance::find_best_match_for_name; // Sequences use std::collections::LinkedList; use std::collections::VecDeque; use std::vec::Vec; // Maps use std::collections::hash_map::HashMap; use std::collections::BTreeMap; // Sets use std::collections::BTreeSet; use std::collections::HashSet; // Misc use std::collections::BinaryHeap; pub trait Suggest { /// Find similar name in values for all collections fn suggest(&self, query: &str) -> Option; /// Find similar name with dist in values for all collections fn suggest_by(&self, query: &str, dist: usize) -> Option; } pub trait SuggestKey { /// Find similar name in keys for Map collections fn suggest_key(&self, query: &str) -> Option; /// Find similar name with dist in keys for Map collections fn suggest_key_by(&self, query: &str, dist: usize) -> Option; } macro_rules! impl_suggest_fns { ($f:ident) => { fn suggest(&self, query: &str) -> Option { find_best_match_for_name(self.$f(), query, None) } fn suggest_by(&self, query: &str, dist: usize) -> Option { find_best_match_for_name(self.$f(), query, Some(dist)) } }; } macro_rules! impl_suggest { ($t:ident) => { impl> Suggest for $t { impl_suggest_fns!(iter); } }; } macro_rules! impl_suggest_value { ($t:ident) => { impl> Suggest for $t { impl_suggest_fns!(values); } }; } macro_rules! impl_suggest_key { ($t:ident) => { impl, U> SuggestKey for $t { fn suggest_key(&self, query: &str) -> Option { find_best_match_for_name(self.keys(), query, None) } fn suggest_key_by(&self, query: &str, dist: usize) -> Option { find_best_match_for_name(self.keys(), query, Some(dist)) } } }; } macro_rules! impl_suggest_map { ($t:ident) => { impl_suggest_key!($t); impl_suggest_value!($t); }; } // Primitive Array Type impl, const N: usize> Suggest for [T; N] { impl_suggest_fns!(iter); } // Slices impl> Suggest for [T] { impl_suggest_fns!(iter); } // Sequences impl_suggest!(LinkedList); impl_suggest!(VecDeque); impl_suggest!(Vec); // Maps impl_suggest_map!(HashMap); impl_suggest_map!(BTreeMap); // Sets impl_suggest!(BTreeSet); impl_suggest!(HashSet); // Misc impl_suggest!(BinaryHeap); #[cfg(test)] mod tests { use super::*; macro_rules! test_suggest_primitive { ($t:ty, $f:ident) => { #[test] fn $f() { let tmp = ["aaab", "aaabc"]; let input: $t = &tmp; assert_eq!(input.suggest("aaaa"), Some("aaab".to_string())); let tmp = ["poac", "poacpp"]; let input: $t = &tmp; assert_eq!(input.suggest("paoc"), None); assert_eq!(input.suggest_by("paoc", 1), None); assert_eq!(input.suggest_by("paoc", 2), Some("poac".to_string())); } }; } macro_rules! test_suggest { ($t:ident, $f:ident) => { #[test] fn $f() { let input: $t<_> = vec!["aaab", "aaabc"].into_iter().collect(); assert_eq!(input.suggest("aaaa"), Some("aaab".to_string())); let input: $t<_> = vec!["poac", "poacpp"].into_iter().collect(); assert_eq!(input.suggest("paoc"), None); assert_eq!(input.suggest_by("paoc", 1), None); assert_eq!(input.suggest_by("paoc", 2), Some("poac".to_string())); } }; } macro_rules! test_suggest_map { ($t:ident, $f:ident) => { #[test] fn $f() { let input = $t::<_, _>::from_iter([("aaab", 2), ("aaabc", 4)].into_iter()); assert_eq!(input.suggest_key("aaaa"), Some("aaab".to_string())); let input = $t::<_, _>::from_iter([(2, "aaab"), (4, "aaabc")].into_iter()); assert_eq!(input.suggest("aaaa"), Some("aaab".to_string())); let input = $t::<_, _>::from_iter([("poac", 2), ("poacpp", 4)].into_iter()); assert_eq!(input.suggest_key("paoc"), None); assert_eq!(input.suggest_key_by("paoc", 1), None); assert_eq!(input.suggest_key_by("paoc", 2), Some("poac".to_string())); let input = $t::<_, _>::from_iter([(2, "poac"), (4, "poacpp")].into_iter()); assert_eq!(input.suggest("paoc"), None); assert_eq!(input.suggest_by("paoc", 1), None); assert_eq!(input.suggest_by("paoc", 2), Some("poac".to_string())); } }; } // Primitive Array Type test_suggest_primitive!(&[&str; 2], test_array); // Slices test_suggest_primitive!(&[&str], test_slices); // Sequences test_suggest!(LinkedList, test_suggest_linked_list); test_suggest!(VecDeque, test_suggest_vec_deque); test_suggest!(Vec, test_suggest_vec); // Maps test_suggest_map!(HashMap, test_suggest_hash_map); test_suggest_map!(BTreeMap, test_suggest_b_tree_map); // Sets test_suggest!(HashSet, test_suggest_b_tree_set); test_suggest!(HashSet, test_suggest_hash_set); // Misc test_suggest!(BinaryHeap, test_suggest_binary_heap); }