Skip to content

Commit

Permalink
refactor: further separate CLI logic from the API related functionali…
Browse files Browse the repository at this point in the history
…ty (see #117) (#124)

* refactor: use `std::ops::Not::not` instead of custom `is_false` function

* refactor: further separate API logic from CLI, and create submodules for each subcommand

* fix: fmt

* fix: nightly `rustfmt` warning: the `version` option is deprecated. Use `style_edition` instead.

* fix: fmt

* refactor: `ProcessCommand` -> `process::Command`

* fix: remove unused imports

* refactor: make link more readable for CLI

* fix: `clippy` and `doc` warnings

* chore: bump minimum rust version

* fix: misc CI issues

* feat: use `enum_dispatch` to avoid needing to manually call `cmd.execute` for each variant of `Command`

* refactor: avoid cloning request input string

* refactor: use `Cow<'static, str>` instead of `String` for `check::Request.text`

* chore(lib): adding Cow

* refactor: use `Cow<'source, str>` when not compiling with the `cli` feature

Required cloning structs and methods from `api::check` to separate out
the `clap` functionality, as `clap` wouldn't support the lifetime
without it being `'static`

* fix: remove unnecessary clone of `split_pattern`

* refactor: use `Cow<'source, str>` for text referenced by `ResponseWithContext`

* Update CI.yml

* Update README.md

Co-authored-by: Jérome Eertmans <[email protected]>

* Update benches/benchmarks/check_texts.rs

Co-authored-by: Jérome Eertmans <[email protected]>

* Update benches/benchmarks/check_texts.rs

Co-authored-by: Jérome Eertmans <[email protected]>

* Update src/api/server.rs

Co-authored-by: Jérome Eertmans <[email protected]>

* Update tests/match_positions.rs

Co-authored-by: Jérome Eertmans <[email protected]>

* fix: formatting

* fix: remove unused imports

* fix: remove static lifetime from `with_text` and `with_data`

* fix: correct length addition and use `Cow::to_mut`

* fix: satisfy `clippy` pre-commit hook

---------

Co-authored-by: Jérome Eertmans <[email protected]>
  • Loading branch information
Rolv-Apneseth and jeertmans authored Nov 16, 2024
1 parent a7247e4 commit a067522
Show file tree
Hide file tree
Showing 20 changed files with 1,227 additions and 711 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ jobs:
toolchain: stable
override: true

- run: cargo publish --features full --token ${CRATES_TOKEN}
env:
CRATES_TOKEN: ${{ secrets.CRATES_TOKEN }}
- name: Publish on crates.io
run: cargo publish --features full --token ${{ secrets.CRATES_TOKEN }}
check-publish:
name: Check Publish
runs-on: ubuntu-latest
Expand All @@ -42,6 +41,5 @@ jobs:
toolchain: stable
override: true

- run: cargo publish --features cli --token ${CRATES_TOKEN} --dry-run -v
env:
CRATES_TOKEN: ${{ secrets.CRATES_TOKEN }}
- name: Check if can publish on crates.io
run: cargo publish --features cli --token ${{ secrets.CRATES_TOKEN }} --dry-run -v
73 changes: 64 additions & 9 deletions Cargo.lock

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

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ required-features = ["cli"]
annotate-snippets = {version = "^0.9.1", optional = true}
clap = {version = "^4.5.18", features = ["cargo", "derive", "env", "wrap_help"], optional = true}
clap_complete = {version = "^4.5.2", optional = true}
enum_dispatch = {version = "0.3.13", optional = true}
is-terminal = {version = "0.4.3", optional = true}
lifetime = {version = "0.1.0", features = ["macros"]}
pulldown-cmark = {version = "0.10.2", optional = true}
reqwest = {version = "^0.11", default-features = false, features = ["json"]}
serde = {version = "^1.0", features = ["derive"]}
Expand All @@ -33,7 +35,7 @@ tokio = {version = "^1.0", features = ["macros"]}

[features]
annotate = ["dep:annotate-snippets"]
cli = ["annotate", "color", "dep:clap", "dep:is-terminal", "multithreaded"]
cli = ["annotate", "color", "dep:clap", "dep:enum_dispatch", "dep:is-terminal", "multithreaded"]
cli-complete = ["cli", "clap_complete"]
color = ["annotate-snippets?/color", "dep:termcolor"]
default = ["cli", "native-tls"]
Expand Down Expand Up @@ -61,7 +63,7 @@ license = "MIT"
name = "languagetool-rust"
readme = "README.md"
repository = "https://github.com/jeertmans/languagetool-rust"
rust-version = "1.74.0"
rust-version = "1.75.0"
version = "2.1.4"

[package.metadata.docs.rs]
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,14 @@ languagetool-rust = "^2.1"
```rust
use languagetool_rust::api::{check, server::ServerClient};
use std::borrow::Cow;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ServerClient::from_env_or_default();
let req = check::Request::default()
.with_text("Some phrase with a smal mistake".to_string()); // # codespell:ignore smal
.with_text("Some phrase with a smal mistake"); // # codespell:ignore smal
println!(
"{}",
Expand Down
4 changes: 2 additions & 2 deletions benches/benchmarks/check_texts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static FILES: [(&str, &str); 3] = [
("large", include_str!("../large.txt")),
];

async fn request_until_success(req: &Request, client: &ServerClient) -> Response {
async fn request_until_success<'source>(req: &Request<'source>, client: &ServerClient) -> Response {

Check warning on line 17 in benches/benchmarks/check_texts.rs

View check run for this annotation

Codecov / codecov/patch

benches/benchmarks/check_texts.rs#L17

Added line #L17 was not covered by tests
loop {
match client.check(req).await {
Ok(resp) => return resp,
Expand All @@ -34,7 +34,7 @@ async fn check_text_basic(text: &str) -> Response {
"Please use a local server for benchmarking, and configure the environ variables to use \
it.",
);
let req = Request::default().with_text(text.to_string());
let req = Request::default().with_text(text);
request_until_success(&req, &client).await
}

Expand Down
2 changes: 1 addition & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
condense_wildcard_suffixes = true
edition = "2021"
# error_on_line_overflow = true
# error_on_unformatted = true
force_multiline_blocks = true
Expand All @@ -9,5 +10,4 @@ imports_granularity = "Crate"
match_block_trailing_comma = true
normalize_doc_attributes = true
unstable_features = true
version = "Two"
wrap_comments = true
Loading

0 comments on commit a067522

Please sign in to comment.