Skip to content

Commit 4cefa17

Browse files
committed
Add documentation for generated enums
This commit adds documentation for generated enums. The documentation is sourced from the rest API specs.
1 parent 8682e30 commit 4cefa17

File tree

8 files changed

+35
-9
lines changed

8 files changed

+35
-9
lines changed

api_generator/src/api_generator/code_gen/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn ident<I: AsRef<str>>(name: I) -> syn::Ident {
4141
}
4242

4343
/// AST for document attribute
44-
fn doc(comment: String) -> syn::Attribute {
44+
fn doc<I: Into<String>>(comment: I) -> syn::Attribute {
4545
syn::Attribute {
4646
style: syn::AttrStyle::Outer,
4747
value: syn::MetaItem::NameValue(ident("doc".to_string()), lit(comment)),

api_generator/src/api_generator/code_gen/params.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,14 @@ fn generate_param(tokens: &mut Tokens, e: &ApiEnum) {
4242
})
4343
.unzip();
4444

45+
let doc = match &e.description {
46+
Some(description) => Some(code_gen::doc(description)),
47+
None => None,
48+
};
49+
4550
let generated_enum_tokens = quote!(
4651
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
52+
#doc
4753
pub enum #name {
4854
#(#[serde(rename = #renames)] #variants),*
4955
}

api_generator/src/api_generator/code_gen/request/request_builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl<'a> RequestBuilder<'a> {
227227
ident: ident("body<T>"),
228228
vis: syn::Visibility::Public,
229229
defaultness: syn::Defaultness::Final,
230-
attrs: vec![doc("The body for the API call".into())],
230+
attrs: vec![doc("The body for the API call")],
231231
node: syn::ImplItemKind::Method(
232232
syn::MethodSig {
233233
unsafety: syn::Unsafety::Normal,
@@ -266,7 +266,7 @@ impl<'a> RequestBuilder<'a> {
266266

267267
/// Creates the AST for a builder fn to add a HTTP header
268268
fn create_header_fn(field: &syn::Ident) -> syn::ImplItem {
269-
let doc_attr = doc("Adds a HTTP header".into());
269+
let doc_attr = doc("Adds a HTTP header");
270270

271271
syn::ImplItem {
272272
ident: ident("header"),
@@ -315,7 +315,7 @@ impl<'a> RequestBuilder<'a> {
315315
let value_ident = ident(&name);
316316
let ty = typekind_to_ty(&f.0, f.1.ty, true);
317317
let doc_attr = match &f.1.description {
318-
Some(docs) => vec![doc(docs.into())],
318+
Some(docs) => vec![doc(docs)],
319319
_ => vec![],
320320
};
321321

@@ -554,7 +554,7 @@ impl<'a> RequestBuilder<'a> {
554554
};
555555

556556
let method_doc = match &endpoint.documentation.description {
557-
Some(description) => Some(doc(description.into())),
557+
Some(description) => Some(doc(description)),
558558
_ => None,
559559
};
560560

api_generator/src/api_generator/code_gen/url/enum_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<'a> EnumBuilder<'a> {
135135
fn parts_none() -> syn::Variant {
136136
syn::Variant {
137137
ident: ident("None"),
138-
attrs: vec![doc("No parts".into())],
138+
attrs: vec![doc("No parts")],
139139
data: syn::VariantData::Unit,
140140
discriminant: None,
141141
}

api_generator/src/api_generator/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ pub struct Common {
233233
/// An enum defined in the REST API specs
234234
pub struct ApiEnum {
235235
pub name: String,
236+
pub description: Option<String>,
236237
pub values: Vec<String>,
237238
}
238239

@@ -385,6 +386,7 @@ fn read_api(branch: &str, download_dir: &PathBuf) -> Result<Api, failure::Error>
385386

386387
enums.insert(ApiEnum {
387388
name: param.0.to_string(),
389+
description: param.1.description.clone(),
388390
values: options,
389391
});
390392
}

elasticsearch/src/generated/params.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// -----------------------------------------------
1717
use serde::{Deserialize, Serialize};
1818
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
19+
#[doc = "The unit in which to display byte values"]
1920
pub enum Bytes {
2021
#[serde(rename = "b")]
2122
B,
@@ -41,20 +42,23 @@ pub enum Bytes {
4142
Pb,
4243
}
4344
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
45+
#[doc = "What to do when the delete by query hits version conflicts?"]
4446
pub enum Conflicts {
4547
#[serde(rename = "abort")]
4648
Abort,
4749
#[serde(rename = "proceed")]
4850
Proceed,
4951
}
5052
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
53+
#[doc = "The default operator for query string query (AND or OR)"]
5154
pub enum DefaultOperator {
5255
#[serde(rename = "AND")]
5356
And,
5457
#[serde(rename = "OR")]
5558
Or,
5659
}
5760
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
61+
#[doc = "Whether to expand wildcard expression to concrete indices that are open, closed or both."]
5862
pub enum ExpandWildcards {
5963
#[serde(rename = "open")]
6064
Open,
@@ -66,6 +70,7 @@ pub enum ExpandWildcards {
6670
All,
6771
}
6872
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
73+
#[doc = "Group tasks by nodes or parent/child relationships"]
6974
pub enum GroupBy {
7075
#[serde(rename = "nodes")]
7176
Nodes,
@@ -75,6 +80,7 @@ pub enum GroupBy {
7580
None,
7681
}
7782
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
83+
#[doc = "A health status (\"green\", \"yellow\", or \"red\" to filter only indices matching the specified health status"]
7884
pub enum Health {
7985
#[serde(rename = "green")]
8086
Green,
@@ -84,6 +90,7 @@ pub enum Health {
8490
Red,
8591
}
8692
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
93+
#[doc = "Specify the level of detail for returned information"]
8794
pub enum Level {
8895
#[serde(rename = "cluster")]
8996
Cluster,
@@ -93,13 +100,15 @@ pub enum Level {
93100
Shards,
94101
}
95102
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
103+
#[doc = "Explicit operation type. Defaults to `index` for requests with an explicit document ID, and to `create`for requests without an explicit document ID"]
96104
pub enum OpType {
97105
#[serde(rename = "index")]
98106
Index,
99107
#[serde(rename = "create")]
100108
Create,
101109
}
102110
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
111+
#[doc = "If `true` then refresh the effected shards to make this operation visible to search, if `wait_for` then wait for a refresh to make this operation visible to search, if `false` (the default) then do nothing with refreshes."]
103112
pub enum Refresh {
104113
#[serde(rename = "true")]
105114
True,
@@ -109,13 +118,15 @@ pub enum Refresh {
109118
WaitFor,
110119
}
111120
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
121+
#[doc = "Search operation type"]
112122
pub enum SearchType {
113123
#[serde(rename = "query_then_fetch")]
114124
QueryThenFetch,
115125
#[serde(rename = "dfs_query_then_fetch")]
116126
DfsQueryThenFetch,
117127
}
118128
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
129+
#[doc = "The multiplier in which to display values"]
119130
pub enum Size {
120131
#[serde(rename = "k")]
121132
K,
@@ -129,6 +140,7 @@ pub enum Size {
129140
P,
130141
}
131142
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
143+
#[doc = "Specify suggest mode"]
132144
pub enum SuggestMode {
133145
#[serde(rename = "missing")]
134146
Missing,
@@ -138,6 +150,7 @@ pub enum SuggestMode {
138150
Always,
139151
}
140152
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
153+
#[doc = "The unit in which to display time values"]
141154
pub enum Time {
142155
#[serde(rename = "d")]
143156
D,
@@ -155,6 +168,7 @@ pub enum Time {
155168
Nanos,
156169
}
157170
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
171+
#[doc = "The type to sample (default: cpu)"]
158172
pub enum Type {
159173
#[serde(rename = "cpu")]
160174
Cpu,
@@ -164,6 +178,7 @@ pub enum Type {
164178
Block,
165179
}
166180
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
181+
#[doc = "Specific version type"]
167182
pub enum VersionType {
168183
#[serde(rename = "internal")]
169184
Internal,
@@ -173,6 +188,7 @@ pub enum VersionType {
173188
ExternalGte,
174189
}
175190
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
191+
#[doc = "Wait until all currently queued events with the given priority are processed"]
176192
pub enum WaitForEvents {
177193
#[serde(rename = "immediate")]
178194
Immediate,
@@ -188,6 +204,7 @@ pub enum WaitForEvents {
188204
Languid,
189205
}
190206
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone, Copy)]
207+
#[doc = "Wait until cluster is in a specific state"]
191208
pub enum WaitForStatus {
192209
#[serde(rename = "green")]
193210
Green,

elasticsearch/src/http/request.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ use serde::Serialize;
99
/// expect JSON, however, there are some APIs that expect newline-delimited JSON (NDJSON).
1010
/// The [Body] trait allows modelling different API body implementations.
1111
pub trait Body {
12-
/// A ready-made immutable buffer that can be used to avoid writing
12+
/// An existing immutable buffer that can be used to avoid writing
13+
/// having to write to another buffer that will then be written to the request stream.
1314
///
1415
/// If this method returns `Some`, the bytes must be the same as
15-
/// what would be written by `write`.
16+
/// those that would be written by `write`.
1617
fn bytes(&self) -> Option<Bytes> {
1718
None
1819
}

elasticsearch/src/params/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
pub use super::generated::params::*;
44
use serde::{Deserialize, Serialize};
55

6-
/// Allows you to control how the total number of hits should be tracked.
6+
/// Control how the total number of hits should be tracked.
77
///
88
/// When set to `Track` with a value `true`, the response will always track the number of hits that
99
/// match the query accurately.

0 commit comments

Comments
 (0)