Skip to content

Commit

Permalink
Unify a library and cli into one crate
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui committed Mar 1, 2022
1 parent ee75208 commit b808588
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 164 deletions.
5 changes: 0 additions & 5 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,3 @@ updates:
directory: "/" # Location of package manifests
schedule:
interval: "daily"

- package-ecosystem: "cargo" # See documentation for possible values
directory: "/suggestion/" # Location of package manifests
schedule:
interval: "daily"
36 changes: 27 additions & 9 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

env:
CARGO_TERM_COLOR: always
Expand All @@ -20,17 +19,9 @@ jobs:

steps:
- uses: actions/checkout@v2

- name: Build
run: cargo build --verbose

- name: Run tests
run: cargo test --verbose

- name: Run tests on suggesion/
run: cargo test --verbose
working-directory: suggestion

run:
strategy:
fail-fast: false
Expand All @@ -44,3 +35,30 @@ jobs:
- run: cargo run instakk update install
- run: cargo run install update install && exit 1 || exit 0
- run: cargo run hoge update install && exit 1 || exit 0

format:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- run: cargo fmt --all -- --check

lint:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-targets --all-features -- -D clippy::all -D warnings

test:
runs-on: macos-latest

steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
27 changes: 10 additions & 17 deletions Cargo.lock

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

15 changes: 5 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
[package]
name = "suggestion-cli"
version = "0.3.1"
name = "suggestion"
version = "0.3.2"
edition = "2021"
authors = ["Ken Matsui <[email protected]>"]
description = "A CLI tool for similar name suggestions to provide helps like \"Did you mean?\""
description = "A minimal library & CLI tool to provide similar name suggestions like \"Did you mean?\""
license = "MIT"
readme = "README.md"
repository = "https://github.com/ken-matsui/suggestion/"
homepage = "https://github.com/ken-matsui/suggestion#readme"
documentation = "https://docs.rs/suggestion-cli"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[workspace]
members = ["suggestion"]
documentation = "https://docs.rs/suggestion"

[dependencies]
clap = { version = "3.0.13", features = ["derive"] }
suggestion = { path = "suggestion", version = "0.3.0" }
lev_distance = "0.1.1"

[[bin]]
name = "suggest"
Expand Down
103 changes: 94 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,106 @@
# suggestion-cli [![crates.io version](https://img.shields.io/crates/v/suggestion-cli.svg)](https://crates.io/crates/suggestion-cli) [![crates.io downloads](https://img.shields.io/crates/d/suggestion-cli.svg)](https://crates.io/crates/suggestion-cli)
# suggestion [![crates.io version](https://img.shields.io/crates/v/suggestion.svg)](https://crates.io/crates/suggestion) [![crates.io downloads](https://img.shields.io/crates/d/suggestion.svg)](https://crates.io/crates/suggestion)

A CLI tool for similar name suggestions to provide helps like "Did you mean?"
A minimal library & CLI tool to provide similar name suggestions like "Did you mean?"
This library provides suggestion traits for all collection types in the standard library.

The library version is placed [here](./suggestion).
## Examples

### Simple case

This example can be executed by the `cargo run --example simple` command.

```rust
use suggestion::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);
}
}
```

## Installation
```shell
$ cargo run
No command named `instakk` found.
Did you mean `install`?
```

### Specifying distance

```rust
use suggestion::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_with_dist(input, Some(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 `suggestion::SuggestKey` trait.

### Sets

* `BTreeSet`
* `HashSet`

### Misc

* `BinaryHeap`
* `[T; N]`: primitive array
* `[T]`: slices

## CLI

### Installation

```bash
cargo install suggestion-cli
cargo install suggestion
```

## Usage
### Usage

```bash
$ suggest --help
suggestion-cli 0.3.1
A CLI tool for similar name suggestions to provide helps like "Did you mean?"
suggestion 0.3.1
A minimal library & CLI tool to provide similar name suggestions like "Did you mean?"

USAGE:
suggest [OPTIONS] <INPUT> [VALUES]...
Expand All @@ -31,7 +116,7 @@ OPTIONS:
-V, --version Print version information
```
## Examples
### Examples
```bash
$ suggest instakk update install
Expand Down
File renamed without changes.
10 changes: 4 additions & 6 deletions suggestion/src/lib.rs → src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,21 @@ mod tests {
($t:ident, $f:ident) => {
#[test]
fn $f() {
use std::array::IntoIter;

let input = $t::<_, _>::from_iter(IntoIter::new([("aaab", 2), ("aaabc", 4)]));
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(IntoIter::new([(2, "aaab"), (4, "aaabc")]));
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(IntoIter::new([("poac", 2), ("poacpp", 4)]));
let input = $t::<_, _>::from_iter([("poac", 2), ("poacpp", 4)].into_iter());
assert_eq!(input.suggest_key("paoc"), None);
assert_eq!(input.suggest_key_with_dist("paoc", Some(1)), None);
assert_eq!(
input.suggest_key_with_dist("paoc", Some(2)),
Some("poac".to_string())
);

let input = $t::<_, _>::from_iter(IntoIter::new([(2, "poac"), (4, "poacpp")]));
let input = $t::<_, _>::from_iter([(2, "poac"), (4, "poacpp")].into_iter());
assert_eq!(input.suggest("paoc"), None);
assert_eq!(input.suggest_with_dist("paoc", Some(1)), None);
assert_eq!(
Expand Down
2 changes: 0 additions & 2 deletions suggestion/.gitignore

This file was deleted.

14 changes: 0 additions & 14 deletions suggestion/Cargo.toml

This file was deleted.

Loading

0 comments on commit b808588

Please sign in to comment.