Skip to content

Commit 6110666

Browse files
authored
Autogenerate struct and enum doc comments (#659)
* Add doc comments to structs and enums * Updated mgmt and svc with enum and struct doc comments Co-authored-by: John Batty <johnbatty@microsoft.com>
1 parent 56823a4 commit 6110666

File tree

1,347 files changed

+561144
-34
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,347 files changed

+561144
-34
lines changed

services/autorust/codegen/src/codegen_models.rs

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ struct SchemaGen {
3434
all_of: Vec<SchemaGen>,
3535
}
3636

37+
#[derive(Clone)]
38+
struct EnumValue {
39+
value: String,
40+
description: Option<String>,
41+
}
42+
3743
impl SchemaGen {
3844
fn new(ref_key: Option<RefKey>, schema: Schema, doc_file: PathBuf) -> Self {
3945
Self {
@@ -88,8 +94,19 @@ impl SchemaGen {
8894
get_schema_array_items(&self.schema.common).map_err(Error::ArrayItems)
8995
}
9096

91-
fn enum_values_as_strings(&self) -> Vec<&str> {
92-
enum_values_as_strings(&self.schema.common.enum_)
97+
fn enum_values(&self) -> Vec<EnumValue> {
98+
self.schema
99+
.common
100+
.enum_
101+
.iter()
102+
.filter_map(|v| match v {
103+
Value::String(s) => Some(EnumValue {
104+
value: s.to_owned(),
105+
description: None,
106+
}),
107+
_ => None,
108+
})
109+
.collect()
93110
}
94111

95112
fn properties(&self) -> Vec<&PropertyGen> {
@@ -210,16 +227,6 @@ fn resolve_all_of(all_schemas: &IndexMap<RefKey, SchemaGen>, schema: &SchemaGen,
210227
Ok(schema)
211228
}
212229

213-
fn enum_values_as_strings(values: &[Value]) -> Vec<&str> {
214-
values
215-
.iter()
216-
.filter_map(|v| match v {
217-
Value::String(s) => Some(s.as_str()),
218-
_ => None,
219-
})
220-
.collect()
221-
}
222-
223230
fn all_schemas(spec: &Spec) -> Result<IndexMap<RefKey, SchemaGen>, Error> {
224231
let mut all_schemas: IndexMap<RefKey, SchemaGen> = IndexMap::new();
225232

@@ -347,31 +354,40 @@ fn add_schema_refs(resolved: &mut IndexMap<RefKey, SchemaGen>, spec: &Spec, doc_
347354
}
348355

349356
fn create_enum(namespace: &TokenStream, property: &SchemaGen, property_name: &str, lowercase_workaround: bool) -> Result<TypeCode, Error> {
350-
let enum_values = property.enum_values_as_strings();
357+
let enum_values = property.enum_values();
351358
let id = &property_name.to_camel_case_ident().map_err(|source| Error::EnumName {
352359
source,
353360
property: property_name.to_owned(),
354361
})?;
355362
let mut values = TokenStream::new();
356-
for name in enum_values {
357-
let nm = name.to_camel_case_ident().map_err(|source| Error::EnumName {
363+
for enum_value in enum_values {
364+
let value = &enum_value.value;
365+
let nm = value.to_camel_case_ident().map_err(|source| Error::EnumName {
358366
source,
359367
property: property_name.to_owned(),
360368
})?;
361-
let lower = name.to_lowercase();
362-
let rename = if nm.to_string() == name {
369+
let doc_comment = match enum_value.description {
370+
Some(description) => {
371+
quote! { #[doc = #description] }
372+
}
373+
None => quote! {},
374+
};
375+
let lower = value.to_lowercase();
376+
let rename = if &nm.to_string() == value {
363377
quote! {}
364-
} else if name != lower && lowercase_workaround {
365-
quote! { #[serde(rename = #name, alias = #lower)] }
378+
} else if value != &lower && lowercase_workaround {
379+
quote! { #[serde(rename = #value, alias = #lower)] }
366380
} else {
367-
quote! { #[serde(rename = #name)] }
381+
quote! { #[serde(rename = #value)] }
368382
};
369-
let value = quote! {
383+
let value_token = quote! {
384+
#doc_comment
370385
#rename
371386
#nm,
372387
};
373-
values.extend(value);
388+
values.extend(value_token);
374389
}
390+
375391
let nm = property_name.to_camel_case_ident().map_err(|source| Error::EnumName {
376392
source,
377393
property: property_name.to_owned(),
@@ -391,7 +407,16 @@ fn create_enum(namespace: &TokenStream, property: &SchemaGen, property_name: &st
391407
} else {
392408
quote! {}
393409
};
410+
411+
let doc_comment = match &property.schema.common.description {
412+
Some(description) => {
413+
quote! { #[doc = #description] }
414+
}
415+
None => quote! {},
416+
};
417+
394418
let code = quote! {
419+
#doc_comment
395420
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
396421
pub enum #nm {
397422
#values
@@ -490,7 +515,14 @@ fn create_struct(cg: &CodeGen, schema: &SchemaGen, struct_name: &str) -> Result<
490515
if should_box {
491516
type_name = quote! { Box<#type_name> };
492517
}
518+
519+
let doc_comment = match &property.schema.schema.common.description {
520+
Some(description) => quote! { #[doc = #description] },
521+
None => quote! {},
522+
};
523+
493524
props.extend(quote! {
525+
#doc_comment
494526
#serde
495527
pub #field_name: #type_name,
496528
});
@@ -520,7 +552,13 @@ fn create_struct(cg: &CodeGen, schema: &SchemaGen, struct_name: &str) -> Result<
520552
quote! {}
521553
};
522554

555+
let doc_comment = match &schema.schema.common.description {
556+
Some(description) => quote! { #[doc = #description] },
557+
None => quote! {},
558+
};
559+
523560
let struct_code = quote! {
561+
#doc_comment
524562
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
525563
#default_code
526564
pub struct #struct_name_code {
@@ -600,15 +638,3 @@ fn create_struct_field_code(
600638
}
601639
}
602640
}
603-
604-
#[cfg(test)]
605-
mod tests {
606-
use super::*;
607-
use serde_json::json;
608-
609-
#[test]
610-
fn test_enum_values_as_strings() {
611-
let values = vec![json!("/"), json!("/keys")];
612-
assert_eq!(enum_values_as_strings(&values), vec!["/", "/keys"]);
613-
}
614-
}

services/mgmt/activedirectory/src/package_2017_04_01/models.rs

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)