Skip to content

Commit

Permalink
Merge pull request #64 from jeertmans/fix-words-requests
Browse files Browse the repository at this point in the history
feat(cli/lib): fix words requests and add environ. variables for login
  • Loading branch information
jeertmans authored Feb 9, 2023
2 parents d673198 + 3ef99e3 commit 2d6c957
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 19 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unrealease](https://github.com/jeertmans/languagetool-rust/compare/v2.0.0...HEAD)

### Added

- Added environment variables for login arguments. [#64](https://github.com/jeertmans/languagetool-rust/pull/64)

### Fixed

- Fixed words requests. [#64](https://github.com/jeertmans/languagetool-rust/pull/64)

## [2.0.0](https://github.com/jeertmans/languagetool-rust/compare/v1.3.0...v2.0.0) 2023-02-07

### Chore
Expand Down
10 changes: 8 additions & 2 deletions src/lib/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,18 @@ pub struct CheckRequest {
pub language: String,
/// Set to get Premium API access: Your username/email as used to log in at
/// languagetool.org.
#[cfg_attr(feature = "cli", clap(short = 'u', long, requires = "api_key"))]
#[cfg_attr(
feature = "cli",
clap(short = 'u', long, requires = "api_key", env = "LANGUAGETOOL_USERNAME")
)]
#[serde(skip_serializing_if = "Option::is_none")]
pub username: Option<String>,
/// Set to get Premium API access: [your API
/// key](https://languagetool.org/editor/settings/api).
#[cfg_attr(feature = "cli", clap(short = 'k', long, requires = "username"))]
#[cfg_attr(
feature = "cli",
clap(short = 'k', long, requires = "username", env = "LANGUAGETOOL_API_KEY")
)]
#[serde(skip_serializing_if = "Option::is_none")]
pub api_key: Option<String>,
/// Comma-separated list of dictionaries to include words from; uses special
Expand Down
2 changes: 1 addition & 1 deletion src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl Cli {
serde_json::to_string_pretty(&words_response)?
},
None => {
let words_response = server_client.words(&cmd.request).await?;
let words_response = server_client.words(&cmd.request.into()).await?;
serde_json::to_string_pretty(&words_response)?
},
};
Expand Down
80 changes: 64 additions & 16 deletions src/lib/words.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ pub fn parse_word(v: &str) -> Result<String> {
#[non_exhaustive]
pub struct LoginArgs {
/// Your username as used to log in at languagetool.org.
#[cfg_attr(feature = "cli", clap(short = 'u', long, required = true))]
#[cfg_attr(
feature = "cli",
clap(short = 'u', long, required = true, env = "LANGUAGETOOL_USERNAME")
)]
pub username: String,
/// [Your API key](https://languagetool.org/editor/settings/api)
#[cfg_attr(feature = "cli", clap(short = 'k', long, required = true))]
/// [Your API key](https://languagetool.org/editor/settings/api).
#[cfg_attr(
feature = "cli",
clap(short = 'k', long, required = true, env = "LANGUAGETOOL_API_KEY")
)]
pub api_key: String,
}

Expand All @@ -53,16 +59,55 @@ pub struct WordsRequest {
/// Maximum number of words to return.
#[cfg_attr(feature = "cli", clap(long, default_value = "10"))]
pub limit: isize,
/// Login arguments
/// Login arguments.
#[cfg_attr(feature = "cli", clap(flatten))]
#[serde(flatten)]
pub login: LoginArgs,
/// Comma-separated list of dictionaries to include words from; uses special
/// default dictionary if this is unset
/// default dictionary if this is unset.
#[cfg_attr(feature = "cli", clap(long))]
#[serde(skip_serializing_if = "Option::is_none")]
pub dicts: Option<Vec<String>>,
}

/// Copy of [`WordsRequest`], but used to CLI only.
///
/// This is a temporary solution, until [#3165](https://github.com/clap-rs/clap/issues/3165) is
/// closed.
#[cfg(feature = "cli")]
#[derive(Args, Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
#[non_exhaustive]
pub struct WordsRequestArgs {
/// Offset of where to start in the list of words.
#[cfg_attr(feature = "cli", clap(long, default_value = "0"))]
offset: isize,
/// Maximum number of words to return.
#[cfg_attr(feature = "cli", clap(long, default_value = "10"))]
pub limit: isize,
/// Login arguments.
#[cfg_attr(feature = "cli", clap(flatten))]
#[serde(flatten)]
pub login: Option<LoginArgs>,
/// Comma-separated list of dictionaries to include words from; uses special
/// default dictionary if this is unset.
#[cfg_attr(feature = "cli", clap(long))]
#[serde(skip_serializing_if = "Option::is_none")]
pub dicts: Option<Vec<String>>,
}

#[cfg(feature = "cli")]
impl From<WordsRequestArgs> for WordsRequest {
#[inline]
fn from(args: WordsRequestArgs) -> Self {
Self {
offset: args.offset,
limit: args.limit,
login: args.login.unwrap(),
dicts: args.dicts,
}
}
}

/// LanguageTool POST words add request.
///
/// Add a word to one of the user's personal dictionaries. Please note that this
Expand All @@ -77,15 +122,16 @@ pub struct WordsAddRequest {
/// languages.
#[cfg_attr(feature = "cli", clap(required = true, value_parser = parse_word))]
pub word: String,
/// Login arguments
/// Login arguments.
#[cfg_attr(feature = "cli", clap(flatten))]
#[serde(flatten)]
pub login: LoginArgs,
/// Name of the dictionary to add the word to; non-existent dictionaries are
/// created after calling this; if unset, adds to special default
/// dictionary
/// dictionary.
#[cfg_attr(feature = "cli", clap(long))]
#[serde(skip_serializing_if = "Option::is_none")]
dict: Option<String>,
pub dict: Option<String>,
}

/// LanguageTool POST words delete request.
Expand All @@ -100,6 +146,7 @@ pub struct WordsDeleteRequest {
pub word: String,
/// Login arguments.
#[cfg_attr(feature = "cli", clap(flatten))]
#[serde(flatten)]
pub login: LoginArgs,
/// Name of the dictionary to add the word to; non-existent dictionaries are
/// created after calling this; if unset, adds to special default
Expand All @@ -122,11 +169,12 @@ pub enum WordsSubcommand {
/// Retrieve some user's words list.
#[cfg(feature = "cli")]
#[derive(Debug, Parser)]
#[clap(subcommand_negates_reqs(true))]
#[clap(args_conflicts_with_subcommands = true)]
#[clap(subcommand_negates_reqs = true)]
pub struct WordsCommand {
/// Actual GET request.
#[command(flatten)]
pub request: WordsRequest,
pub request: WordsRequestArgs,
/// Optional subcommand.
#[command(subcommand)]
pub subcommand: Option<WordsSubcommand>,
Expand All @@ -136,22 +184,22 @@ pub struct WordsCommand {
#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
#[non_exhaustive]
pub struct WordsResponse {
/// List of words
words: Vec<String>,
/// List of words.
pub words: Vec<String>,
}

/// LanguageTool POST word add response.
#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
#[non_exhaustive]
pub struct WordsAddResponse {
/// `true` if word was correctly added
added: bool,
/// `true` if word was correctly added.
pub added: bool,
}

/// LanguageTool POST word delete response.
#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
#[non_exhaustive]
pub struct WordsDeleteResponse {
/// `true` if word was correctly removed
deleted: bool,
/// `true` if word was correctly removed.
pub deleted: bool,
}

0 comments on commit 2d6c957

Please sign in to comment.