languagetool_rust/api/words/
mod.rs

1//! Structures for `words` requests and responses.
2
3use crate::error::{Error, Result};
4
5use super::check::serialize_option_vec_string;
6#[cfg(feature = "cli")]
7use clap::Args;
8use serde::{Deserialize, Serialize};
9
10pub mod add;
11pub mod delete;
12
13/// Parse `v` if valid word.
14///
15/// A valid word is any string slice that does not contain any whitespace
16///
17/// # Examples
18///
19/// ```
20/// # use languagetool_rust::api::words::parse_word;
21/// assert!(parse_word("word").is_ok());
22///
23/// assert!(parse_word("some words").is_err());
24/// ```
25pub fn parse_word(v: &str) -> Result<String> {
26    if !v.contains(' ') {
27        return Ok(v.to_string());
28    }
29    Err(Error::InvalidValue(
30        "The value should be a word that does not contain any whitespace".to_string(),
31    ))
32}
33
34/// Login arguments required by the API.
35#[cfg_attr(feature = "cli", derive(Args))]
36#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize, Hash)]
37#[serde(rename_all = "camelCase")]
38#[non_exhaustive]
39pub struct LoginArgs {
40    /// Your username as used to log in at languagetool.org.
41    #[cfg_attr(
42        feature = "cli",
43        clap(short = 'u', long, required = true, env = "LANGUAGETOOL_USERNAME")
44    )]
45    pub username: String,
46    /// Your API key (see <https://languagetool.org/editor/settings/api>).
47    #[cfg_attr(
48        feature = "cli",
49        clap(short = 'k', long, required = true, env = "LANGUAGETOOL_API_KEY")
50    )]
51    pub api_key: String,
52}
53
54/// LanguageTool GET words request.
55///
56/// List words in the user's personal dictionaries.
57#[cfg_attr(feature = "cli", derive(Args))]
58#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize, Hash)]
59#[non_exhaustive]
60pub struct Request {
61    /// Offset of where to start in the list of words.
62    ///
63    /// Defaults to 0.
64    #[cfg_attr(feature = "cli", clap(long))]
65    #[serde(skip_serializing_if = "Option::is_none")]
66    pub offset: Option<isize>,
67    /// Maximum number of words to return.
68    ///
69    /// Defaults to 10.
70    #[cfg_attr(feature = "cli", clap(long))]
71    #[serde(skip_serializing_if = "Option::is_none")]
72    pub limit: Option<isize>,
73    /// Login arguments.
74    #[cfg_attr(feature = "cli", clap(flatten))]
75    #[serde(flatten)]
76    pub login: LoginArgs,
77    /// Comma-separated list of dictionaries to include words from; uses special
78    /// default dictionary if this is unset.
79    #[cfg_attr(feature = "cli", clap(long))]
80    #[serde(serialize_with = "serialize_option_vec_string")]
81    #[serde(skip_serializing_if = "Option::is_none")]
82    pub dicts: Option<Vec<String>>,
83}
84
85/// Copy of [`Request`], but used to CLI only.
86///
87/// This is a temporary solution, until [#4697](https://github.com/clap-rs/clap/issues/4697) is
88/// closed.
89#[cfg(feature = "cli")]
90#[derive(Args, Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
91#[non_exhaustive]
92pub struct RequestArgs {
93    /// Offset of where to start in the list of words.
94    #[cfg_attr(feature = "cli", clap(long, default_value = "0"))]
95    pub offset: isize,
96    /// Maximum number of words to return.
97    #[cfg_attr(feature = "cli", clap(long, default_value = "10"))]
98    pub limit: isize,
99    /// Login arguments.
100    #[cfg_attr(feature = "cli", clap(flatten))]
101    #[serde(flatten)]
102    pub login: Option<LoginArgs>,
103    /// Comma-separated list of dictionaries to include words from; uses special
104    /// default dictionary if this is unset.
105    #[cfg_attr(feature = "cli", clap(long))]
106    #[serde(serialize_with = "serialize_option_vec_string")]
107    pub dicts: Option<Vec<String>>,
108}
109
110#[cfg(feature = "cli")]
111impl From<RequestArgs> for Request {
112    #[inline]
113    fn from(args: RequestArgs) -> Self {
114        Self {
115            offset: Some(args.offset),
116            limit: Some(args.limit),
117            login: args.login.unwrap(),
118            dicts: args.dicts,
119        }
120    }
121}
122
123/// LanguageTool GET words response.
124#[derive(Clone, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
125#[non_exhaustive]
126pub struct Response {
127    /// List of words.
128    pub words: Vec<String>,
129}