1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use clap::Parser;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Software {
    name: String,
    version: String,
    build_date: String,
    api_version: isize,
    status: String,
    premium: bool,
}
#[derive(Debug, Deserialize)]
pub struct DetectedLanguage {
    name: String,
    code: String,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LanguageCheck {
    name: String,
    code: String,
    detected_language: DetectedLanguage,
}
#[derive(Debug, Deserialize)]
pub struct Replacement {
    value: String,
}
#[derive(Debug, Deserialize)]
pub struct Context {
    text: String,
    offset: isize,
    length: isize,
}
#[derive(Debug, Deserialize)]
pub struct Url {
    value: String,
}
#[derive(Debug, Deserialize)]
pub struct Category {
    id: String,
    name: String,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Rule {
    id: String,
    sub_id: String,
    description: String,
    urls: Vec<Url>,
    issue_type: String,
    category: Category,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Match {
    message: String,
    short_message: String,
    offset: isize,
    length: isize,
    replacements: Vec<Replacement>,
    context: Context,
    sentence: String,
    // rule: Rule, // Seems to cause problems with missing fields
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "lowercase")]
enum Level {
    Default,
    Picky,
}

impl Default for Level {
    fn default() -> Self {
        Level::Default
    }
}

impl std::str::FromStr for Level {
    type Err = clap::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match &s.to_lowercase()[..] {
            "default" => Ok(Level::Default),
            "picky" => Ok(Level::Picky),
            _ => Err(clap::Command::new("").error(
                clap::ErrorKind::InvalidValue,
                format!("Could not convert `{}` into either `default` or `picky`", s),
            )),
        }
    }
}

#[derive(Parser, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CheckRequest {
    #[clap(short = 'c', long)]
    text: Option<String>,
    //#[clap(short = 'd', long, conflicts_with = "text")]
    //data: ,
    #[clap(short = 'l', long, required = true)]
    language: String,
    #[clap(short = 'u', long)]
    username: Option<String>,
    #[clap(short = 'k', long)]
    api_key: Option<String>,
    /*
    dicts: Option<Vec<String>>,
    mother_tongue: Option<String>,
    preferred_variants: Option<Vec<String>>,
    enabled_rules: Option<Vec<isize>>,
    disabled_rules: Option<Vec<isize>>,
    enabled_categories: Option<Vec<isize>>,
    disabled_categories: Option<Vec<isize>>,*/
    #[clap(long, takes_value = false)]
    enabled_only: bool,
    #[clap(long, default_value = "default")]
    level: Level,
}

impl CheckRequest {
    pub fn with_text(mut self, text: &str) -> Self {
        self.text = Some(text.to_string());
        //self.data = None;
        self
    }

    pub fn with_language(mut self, language: &str) -> Self {
        self.language = language.to_string();
        self
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CheckResponse {
    software: Software,
    language: LanguageCheck,
    matches: Vec<Match>,
}