Skip to content

Commit eaaef2d

Browse files
authored
Merge pull request #28 from jeertmans/feature-refactoring
chore(lib): refactoring feature usage
2 parents c07a671 + 7e619de commit eaaef2d

File tree

8 files changed

+78
-74
lines changed

8 files changed

+78
-74
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Created founding link. [#19](https://github.com/jeertmans/languagetool-rust/pull/19)
1414
- Added two related projets. [#21](https://github.com/jeertmans/languagetool-rust/pull/21)
1515
- Added release dates. [#31](https://github.com/jeertmans/languagetool-rust/pull/31)
16-
- Added `#[must_use]` flag to most structures, to please clippy pendantic. [#29](https://github.com/jeertmans/languagetool-rust/pull/21)
16+
- Added `#[must_use]` flag to most structures, to please clippy pendantic. [#29](https://github.com/jeertmans/languagetool-rust/pull/29)
17+
- Changed conditional compilation flags to directly point to dependency, e.g., `"clap"` instead of `"cli"`. [#28](https://github.com/jeertmans/languagetool-rust/pull/28)
1718

1819
### Added
1920

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ annotate = ["annotate-snippets"]
3838
cli = ["atty", "clap", "lazy_static", "regex", "tokio"]
3939
cli-complete = ["cli", "clap_complete"]
4040
docker = []
41-
full = ["annotate", "cli", "docker", "unstable", "cli-complete"]
41+
full = ["annotate", "cli-complete", "docker", "unstable"]
4242

4343
native-tls = ["reqwest/native-tls"]
4444
native-tls-vendored = ["reqwest/native-tls-vendored"]

src/bin.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use clap::{CommandFactory, FromArgMatches};
2-
#[cfg(feature = "cli-complete")]
2+
#[cfg(feature = "clap_complete")]
33
use clap_complete::{generate, shells};
44
use languagetool_rust::error::Result;
55
use languagetool_rust::*;
66
use std::io::{BufRead, Write};
77

8-
#[cfg(feature = "cli-complete")]
8+
#[cfg(feature = "clap_complete")]
99
pub(crate) static COMPLETIONS_HELP: &str = r"DISCUSSION:
1010
Enable tab completion for Bash, Fish, Zsh, or PowerShell
1111
Elvish shell completion is currently supported, but not documented below.
@@ -157,7 +157,7 @@ fn build_cli() -> clap::Command<'static> {
157157
.author(clap::crate_authors!()),
158158
);
159159

160-
#[cfg(feature = "cli-complete")]
160+
#[cfg(feature = "clap_complete")]
161161
let command = command.subcommand(
162162
clap::Command::new("completions")
163163
.author(clap::crate_authors!())
@@ -237,7 +237,7 @@ async fn try_main() -> Result<()> {
237237

238238
let mut resp = client.check(&req).await?;
239239

240-
#[cfg(feature = "cli")]
240+
#[cfg(feature = "clap")]
241241
if req.more_context {
242242
use crate::check::CheckResponseWithContext;
243243
let text = req.get_text();
@@ -295,7 +295,7 @@ async fn try_main() -> Result<()> {
295295
Some(("docker", sub_matches)) => Docker::from_arg_matches(sub_matches)?
296296
.run_action()
297297
.map(|_| ())?,
298-
#[cfg(feature = "cli-complete")]
298+
#[cfg(feature = "clap_complete")]
299299
Some(("completions", sub_matches)) => match sub_matches.value_of("shell").unwrap() {
300300
"bash" => generate(
301301
shells::Bash,

src/lib/check.rs

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Structures for `check` requests and responses.
22
3-
use crate::error::Error;
4-
#[cfg(feature = "cli")]
3+
#[cfg(feature = "clap")]
54
use clap::Parser;
65
#[cfg(feature = "lazy_static")]
76
use lazy_static::lazy_static;
@@ -61,7 +60,7 @@ pub fn is_language_code(v: &str) -> crate::error::Result<()> {
6160
if v == "auto" || RE.is_match(v) {
6261
Ok(())
6362
} else {
64-
Err(Error::InvalidValue {
63+
Err(crate::error::Error::InvalidValue {
6564
body: format!(
6665
"The value should be `auto` or match regex pattern: {}",
6766
RE.as_str()
@@ -157,7 +156,7 @@ impl Serialize for Data {
157156
}
158157
}
159158

160-
#[cfg(feature = "cli")]
159+
#[cfg(feature = "clap")]
161160
impl std::str::FromStr for Data {
162161
type Err = clap::Error;
163162

@@ -205,7 +204,7 @@ impl Level {
205204
}
206205
}
207206

208-
#[cfg(feature = "cli")]
207+
#[cfg(feature = "clap")]
209208
impl std::str::FromStr for Level {
210209
type Err = clap::Error;
211210

@@ -221,7 +220,7 @@ impl std::str::FromStr for Level {
221220
}
222221
}
223222

224-
#[cfg(feature = "cli")]
223+
#[cfg(feature = "clap")]
225224
impl clap::ValueEnum for Level {
226225
fn value_variants<'a>() -> &'a [Self] {
227226
&[Self::Default, Self::Picky]
@@ -235,7 +234,7 @@ impl clap::ValueEnum for Level {
235234
}
236235
}
237236

238-
#[cfg_attr(feature = "cli", derive(Parser))]
237+
#[cfg_attr(feature = "clap", derive(Parser))]
239238
#[derive(Clone, Deserialize, Debug, Default, PartialEq, Eq, Serialize)]
240239
#[serde(rename_all = "camelCase")]
241240
#[non_exhaustive]
@@ -246,21 +245,21 @@ impl clap::ValueEnum for Level {
246245
/// The structure below tries to follow as closely as possible the JSON API described
247246
/// [here](https://languagetool.org/http-api/swagger-ui/#!/default/post_check).
248247
pub struct CheckRequest {
249-
#[cfg(all(feature = "cli", feature = "annotate"))]
248+
#[cfg(all(feature = "clap", feature = "annotate"))]
250249
#[clap(short = 'r', long, takes_value = false)]
251250
#[serde(skip_serializing)]
252251
/// If present, raw JSON output will be printed instead of annotated text.
253252
pub raw: bool,
254-
#[cfg(feature = "cli")]
253+
#[cfg(feature = "clap")]
255254
#[clap(short = 'm', long, takes_value = false)]
256255
#[serde(skip_serializing)]
257256
/// If present, more context (i.e., line number and line offset) will be added to response.
258257
pub more_context: bool,
259-
#[cfg_attr(feature = "cli", clap(short = 't', long, conflicts_with = "data",))]
258+
#[cfg_attr(feature = "clap", clap(short = 't', long, conflicts_with = "data",))]
260259
#[serde(skip_serializing_if = "Option::is_none")]
261260
/// The text to be checked. This or 'data' is required.
262261
pub text: Option<String>,
263-
#[cfg_attr(feature = "cli", clap(short = 'd', long, conflicts_with = "text"))]
262+
#[cfg_attr(feature = "clap", clap(short = 'd', long, conflicts_with = "text"))]
264263
#[serde(skip_serializing_if = "Option::is_none")]
265264
/// The text to be checked, given as a JSON document that specifies what's text and what's markup. This or 'text' is required.
266265
///
@@ -285,14 +284,18 @@ pub struct CheckRequest {
285284
/// The 'data' feature is not limited to HTML or XML, it can be used for any kind of markup. Entities will need to be expanded in this input.
286285
pub data: Option<Data>,
287286
#[cfg_attr(
288-
feature = "cli",
287+
all(feature = "clap", feature = "lazy_static", feature = "regex"),
289288
clap(
290289
short = 'l',
291290
long,
292291
default_value = "auto",
293292
validator = is_language_code
294293
)
295294
)]
295+
#[cfg_attr(
296+
all(feature = "clap", not(all(feature = "lazy_static", feature = "regex"))),
297+
clap(short = 'l', long, default_value = "auto",)
298+
)]
296299
/// A language code like `en-US`, `de-DE`, `fr`, or `auto` to guess the language automatically (see `preferredVariants` below).
297300
///
298301
/// For languages with variants (English, German, Portuguese) spell checking will only be activated when you specify the variant, e.g. `en-GB` instead of just `en`.
@@ -301,45 +304,45 @@ pub struct CheckRequest {
301304
#[serde(skip_serializing_if = "Option::is_none")]
302305
/// Set to get Premium API access: Your username/email as used to log in at languagetool.org.
303306
pub username: Option<String>,
304-
#[cfg_attr(feature = "cli", clap(short = 'k', long, requires = "username"))]
307+
#[cfg_attr(feature = "clap", clap(short = 'k', long, requires = "username"))]
305308
#[serde(skip_serializing_if = "Option::is_none")]
306309
/// Set to get Premium API access: [your API key](https://languagetool.org/editor/settings/api)
307310
pub api_key: Option<String>,
308-
#[cfg_attr(feature = "cli", clap(long, multiple_values = true))]
311+
#[cfg_attr(feature = "clap", clap(long, multiple_values = true))]
309312
#[serde(skip_serializing_if = "Option::is_none")]
310313
/// Comma-separated list of dictionaries to include words from; uses special default dictionary if this is unset
311314
pub dicts: Option<Vec<String>>,
312-
#[cfg_attr(feature = "cli", clap(long))]
315+
#[cfg_attr(feature = "clap", clap(long))]
313316
#[serde(skip_serializing_if = "Option::is_none")]
314317
/// A language code of the user's native language, enabling false friends checks for some language pairs.
315318
pub mother_tongue: Option<String>,
316-
#[cfg_attr(feature = "cli", clap(long, multiple_values = true))]
319+
#[cfg_attr(feature = "clap", clap(long, multiple_values = true))]
317320
#[serde(skip_serializing_if = "Option::is_none")]
318321
/// Comma-separated list of preferred language variants.
319322
///
320323
/// The language detector used with `language=auto` can detect e.g. English, but it cannot decide whether British English or American English is used. Thus this parameter can be used to specify the preferred variants like `en-GB` and `de-AT`. Only available with `language=auto`. You should set variants for at least German and English, as otherwise the spell checking will not work for those, as no spelling dictionary can be selected for just `en` or `de`.
321324
pub preferred_variants: Option<Vec<String>>,
322-
#[cfg_attr(feature = "cli", clap(long, multiple_values = true))]
325+
#[cfg_attr(feature = "clap", clap(long, multiple_values = true))]
323326
#[serde(skip_serializing_if = "Option::is_none")]
324327
/// IDs of rules to be enabled, comma-separated
325328
pub enabled_rules: Option<Vec<String>>,
326-
#[cfg_attr(feature = "cli", clap(long, multiple_values = true))]
329+
#[cfg_attr(feature = "clap", clap(long, multiple_values = true))]
327330
#[serde(skip_serializing_if = "Option::is_none")]
328331
/// IDs of rules to be disabled, comma-separated
329332
pub disabled_rules: Option<Vec<String>>,
330-
#[cfg_attr(feature = "cli", clap(long, multiple_values = true))]
333+
#[cfg_attr(feature = "clap", clap(long, multiple_values = true))]
331334
#[serde(skip_serializing_if = "Option::is_none")]
332335
/// IDs of categories to be enabled, comma-separated
333336
pub enabled_categories: Option<Vec<String>>,
334-
#[cfg_attr(feature = "cli", clap(long, multiple_values = true))]
337+
#[cfg_attr(feature = "clap", clap(long, multiple_values = true))]
335338
#[serde(skip_serializing_if = "Option::is_none")]
336339
/// IDs of categories to be disabled, comma-separated
337340
pub disabled_categories: Option<Vec<String>>,
338-
#[cfg_attr(feature = "cli", clap(long, takes_value = false))]
341+
#[cfg_attr(feature = "clap", clap(long, takes_value = false))]
339342
#[serde(skip_serializing_if = "is_false")]
340343
/// If true, only the rules and categories whose IDs are specified with `enabledRules` or `enabledCategories` are enabled.
341344
pub enabled_only: bool,
342-
#[cfg_attr(feature = "cli", clap(long, default_value = "default", value_parser = clap::builder::EnumValueParser::<Level>::new()))]
345+
#[cfg_attr(feature = "clap", clap(long, default_value = "default", value_parser = clap::builder::EnumValueParser::<Level>::new()))]
343346
#[serde(skip_serializing_if = "Level::is_default")]
344347
/// If set to `picky`, additional rules will be activated, i.e. rules that you might only find useful when checking formal text.
345348
pub level: Level,
@@ -466,7 +469,7 @@ pub struct Context {
466469
pub text: String,
467470
}
468471

469-
#[cfg(feature = "cli")]
472+
#[cfg(feature = "clap")]
470473
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
471474
#[non_exhaustive]
472475
/// More context, post-processed in check response.
@@ -568,7 +571,7 @@ pub struct Match {
568571
pub length: usize,
569572
/// Error message
570573
pub message: String,
571-
#[cfg(feature = "cli")]
574+
#[cfg(feature = "clap")]
572575
#[serde(skip_serializing_if = "Option::is_none")]
573576
/// More context to match, post-processed using original text
574577
pub more_context: Option<MoreContext>,
@@ -726,7 +729,7 @@ impl CheckResponseWithContext {
726729
}
727730
}
728731

729-
#[cfg(feature = "cli")]
732+
#[cfg(feature = "clap")]
730733
impl From<CheckResponseWithContext> for CheckResponse {
731734
#[allow(clippy::needless_borrow)]
732735
fn from(mut resp: CheckResponseWithContext) -> Self {

src/lib/docker.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
//! applications.
33
44
use crate::error::{exit_status_error, Result};
5-
#[cfg(feature = "cli")]
5+
#[cfg(feature = "clap")]
66
use clap::Parser;
77
use std::process::{Command, Output, Stdio};
88

9-
#[cfg_attr(feature = "cli", derive(Parser))]
9+
#[cfg_attr(feature = "clap", derive(Parser))]
1010
#[derive(Debug, Clone)]
1111
/// Commands to pull, start and stop a `LanguageTool` container using Docker.
1212
pub struct Docker {
1313
#[cfg_attr(
14-
feature = "cli",
14+
feature = "clap",
1515
clap(
1616
default_value = "erikvl87/languagetool",
1717
env = "LANGUAGETOOL_DOCKER_IMAGE"
@@ -20,7 +20,7 @@ pub struct Docker {
2020
/// Image or repository from a registry.
2121
name: String,
2222
#[cfg_attr(
23-
feature = "cli",
23+
feature = "clap",
2424
clap(
2525
short = 'b',
2626
long,
@@ -31,13 +31,13 @@ pub struct Docker {
3131
/// Path to Docker's binaries.
3232
bin: String,
3333
#[cfg_attr(
34-
feature = "cli",
34+
feature = "clap",
3535
clap(long, default_value = "languagetool", env = "LANGUAGETOOL_DOCKER_NAME")
3636
)]
3737
/// Name assigned to the container.
3838
container_name: String,
3939
#[cfg_attr(
40-
feature = "cli",
40+
feature = "clap",
4141
clap(
4242
short = 'p',
4343
long,
@@ -47,12 +47,12 @@ pub struct Docker {
4747
)]
4848
/// Publish a container's port(s) to the host.
4949
port: String,
50-
#[cfg_attr(feature = "cli", clap(subcommand))]
50+
#[cfg_attr(feature = "clap", clap(subcommand))]
5151
/// Docker action.
5252
action: Action,
5353
}
5454

55-
#[cfg_attr(feature = "cli", derive(clap::Subcommand))]
55+
#[cfg_attr(feature = "clap", derive(clap::Subcommand))]
5656
#[derive(Clone, Debug)]
5757
/// Enumerate supported Docker actions.
5858
enum Action {

src/lib/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::process::ExitStatus;
44
/// Enumeration of all possible error types.
55
#[derive(Debug, thiserror::Error)]
66
pub enum Error {
7-
#[cfg(feature = "cli")]
7+
#[cfg(feature = "clap")]
88
#[error(transparent)]
99
/// Error from the command line parsing (see [clap::Error]).
1010
Cli(#[from] clap::Error),
@@ -80,10 +80,10 @@ pub(crate) fn exit_status_error(exit_status: &ExitStatus) -> Result<()> {
8080
mod tests {
8181

8282
use crate::error::Error;
83-
#[cfg(feature = "cli")]
83+
#[cfg(feature = "clap")]
8484
use clap::Command;
8585

86-
#[cfg(feature = "cli")]
86+
#[cfg(feature = "clap")]
8787
#[test]
8888
fn test_error_cli() {
8989
let result =

0 commit comments

Comments
 (0)