grammarbot_io/response.rs
1//! Response module contains all the structures necessary
2//! for doing all the interesting stuff.
3
4/// Represents a strong type on the grammarly bot version.
5#[derive(Debug, Default, Clone, serde::Deserialize)]
6pub struct GrammarlyVersion(pub String);
7
8/// Represents a strong type on the grammarly api version.
9#[derive(Debug, Default, Clone, serde::Deserialize)]
10pub struct ApiVersion(pub u64);
11
12/// Represents a bot info.
13#[derive(Debug, Default, Clone, serde::Deserialize)]
14pub struct Software {
15 /// The name of the bot.
16 pub name: String,
17 /// The grammarly's bot version
18 pub version: GrammarlyVersion,
19 /// The api version used.
20 #[serde(rename = "apiVersion")]
21 pub api_version: ApiVersion,
22 /// Is true if the API key used is premium one.
23 pub premium: bool,
24 /// Contains premium hint. If the API key is not a premium one,
25 /// suggests to get one.
26 #[serde(rename = "premiumHint")]
27 pub premium_hint: String,
28 /// Grammarly bot check status.
29 pub status: String,
30}
31
32/// Represents warnings in the response. They are useful
33/// but could be ignored (not necessarily fixed) and they
34/// should be treated as an advice.
35#[derive(Debug, Default, Clone, serde::Deserialize)]
36pub struct Warnings {
37 /// True if results are incompleted.
38 #[serde(rename = "incompleteResults")]
39 pub incomplete_results: bool,
40 // TODO Check for other values.
41}
42
43/// Detected language code and name, determined by the
44/// grammarly bot.
45#[derive(Debug, Default, Clone, serde::Deserialize)]
46pub struct DetectedLanguage {
47 /// The language name.
48 pub name: String,
49 /// The language code.
50 pub code: String,
51}
52
53/// Passed language and detected language information.
54#[derive(Debug, Default, Clone, serde::Deserialize)]
55pub struct LanguageDetails {
56 /// The language name.
57 pub name: String,
58 /// The language code.
59 pub code: String,
60 /// The language detected by the grammarly bot.
61 #[serde(rename = "detectedLanguage")]
62 pub detected_language: DetectedLanguage,
63}
64
65/// Represents a single possible replacement of the word.
66/// If you have mispelled a word, the service advises you
67/// to correct the word and all the corrections are suggested
68/// using these `Replacement` objects.
69#[derive(Debug, Default, Clone, serde::Deserialize)]
70pub struct Replacement {
71 /// The suggested word to change to.
72 pub value: String,
73}
74
75/// Represents the problem context, a sentence, or a subsentence.
76#[derive(Debug, Default, Clone, serde::Deserialize)]
77pub struct Context {
78 /// The context itself.
79 pub text: String,
80 /// The word's offset causing the problem inside the context.
81 pub offset: u64,
82 /// The length of the word in the context.
83 pub length: u64,
84}
85
86/// Represents a single problem found.
87#[derive(Debug, Default, Clone, serde::Deserialize)]
88pub struct Match {
89 /// Problem detailed explanation.
90 pub message: String,
91 /// Short explanation, the type of a problem.
92 #[serde(rename = "shortMessage")]
93 pub short_message: String,
94 /// All the replacement suggestions.
95 pub replacements: Vec<Replacement>,
96 /// Offset from the beginning of the passed text.
97 pub offset: u64,
98 /// The length of the word causing the problem.
99 pub length: u64,
100 /// The context in which the problem was found.
101 pub context: Context,
102 /// The whole sentence where the problem was found.
103 pub sentence: String,
104 // TODO specify other fields...
105}
106
107/// Grammarly's response structure.
108#[derive(Debug, Clone, serde::Deserialize)]
109#[serde(untagged)]
110pub enum Response {
111 /// A successful response.
112 Success {
113 /// Grammarly bot information.
114 software: Software,
115 /// Grammarly bot warnings.
116 warnings: Warnings,
117 /// Language information.
118 language: LanguageDetails,
119 /// Problems found.
120 matches: Vec<Match>,
121 },
122 /// Not a successful response.
123 Failure {
124 /// Contains text explaining the problem.
125 message: String,
126 },
127}