Coder Social home page Coder Social logo

rust-suggestions's Introduction

Minimal Rust library to provide clap-style "Did you mean?" suggestions

The only dependency is strsim.

The implementation is copied directly from clap (see here). It has just been extracted into a library.

Examples

let possible_vals = vec!["test", "possible", "values"];
let input = "tst";
let suggestions = suggestions::provide_suggestions(input, &possible_vals);
assert_eq!(suggestions, vec!["test"]);
// We have a convenience function to only pick only a single suggestion, giving `Some` or `None`
let single_suggestion = suggestions::provide_a_suggestion(input, &possible_vals);
assert_eq!(single_suggestion.unwrap(), "test");

Multiple matches

Sometimes, there may be multiple (good) suggestions.

Consider the following example:

let possible_vals = vec!["testing", "tempo"];
let input = "teso"; // Sems ambiguous. Maybe multiple suggestions?
let suggestions = suggestions::provide_suggestions(input, &possible_vals);
// The implementation trys to order matches from "best" to "wort"
assert_eq!(suggestions, vec!["testing", "tempo"]);

Asking for a single suggestion here (provide_a_suggestion) would attempt to return the "best" one. As you can immagine, that may not be what the user expects. Therefore, it is best to stick with provide_suggesetions.

No matches

If nothing is reasonably similar, asking for suggestions will return vec![] or None.

let possible_vals = vec!["testing", "things", "here"];
let input = "--something-completely_different";
assert_eq!(suggestions::provide_a_suggestion(&input, &possible_vals), None)

Binary

A binary is available as an example of how to use the library.

It has no additional dependencies. Desired targets are provided as arguments, and "possible strings" are read from standard input

Examples

$ echo "baz\nbar\nfood\nfoz" | suggestions fod
foz food
# Supports multiple targets
$ echo "baz\nbar\nfood\nfoz" | suggestions fod ba
foz food
baz bar
# No matches -> corresponding empty line
$ echo "baz\nbar\nfood\nfoz" | suggestions fod ba
foz food
baz bar

```

```
# Supports outputing as josn (for whatever that's worth)
# echo "baz\nbar\nfood\nfoz" | suggestions --json fod ba nothing-similar
{
  "fod":["foz","food"],
  "ba":["baz","bar"],
  "nothing-similar":[]
}

rust-suggestions's People

Contributors

techcable avatar

Stargazers

zongz avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

zong-zhe

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.