Skip to content

Commit e6b2138

Browse files
authored
crate::Error for services (#497)
1 parent 1db35c6 commit e6b2138

File tree

1,587 files changed

+531535
-306323
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,587 files changed

+531535
-306323
lines changed

services/autorust/codegen/examples/gen_mgmt.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const SKIP_SERVICES: &[&str] = &[
1515
"m365securityandcompliance", // can't find privateLinkServicesForO365ManagementActivityAPI.json
1616
"marketplace",
1717
"mixedreality", // TODO #83 AccountKeyRegenerateRequest not generated
18-
"powerplatform", // https://github.com/Azure/azure-rest-api-specs/pull/11580 incorrect ref & duplicate Operations_List
1918
"service-map", // Ident "Ref:machine"
2019
"servicefabric", // https://github.com/Azure/azure-rest-api-specs/pull/11581 allOf mistakes and duplicate Operations_List
2120
"servicefabricmanagedclusters",
@@ -32,7 +31,6 @@ const SKIP_SERVICE_TAGS: &[(&str, &str)] = &[
3231
("consumption", "package-2018-03"), // defines get_balances_by_billing_account twice
3332
("consumption", "package-2019-11"), // ReservationRecommendationDetails_Get has a path and query param both named "scope"
3433
("consumption", "package-2021-05"),
35-
("cosmos-db", "package-2021-06"), // duplicate tag https://github.com/Azure/azure-rest-api-specs/issues/14996
3634
("databricks", "package-2021-04-01-preview"), // duplicate tag https://github.com/Azure/azure-rest-api-specs/issues/14995
3735
// datamigration, same error for all
3836
// SchemaNotFound MigrateSqlServerSqlDbTask.json ValidationStatus, but may be buried
@@ -43,6 +41,7 @@ const SKIP_SERVICE_TAGS: &[(&str, &str)] = &[
4341
("datamigration", "package-2017-11-15-preview"),
4442
("datamigration", "package-2021-06"),
4543
("deploymentmanager", "package-2018-09-01-preview"), // identifiers are bound more than once in param list. https://github.com/Azure/azure-sdk-for-rust/issues/415
44+
("iothub", "package-preview-2021-07"), // duplicate tag https://github.com/Azure/azure-rest-api-specs/issues/16692
4645
("mediaservices", "package-2019-05-preview"), // invalid unicode character of a dash instead of a hyphen https://github.com/Azure/azure-rest-api-specs/pull/11576
4746
("marketplace", "package-2020-01-01"),
4847
("marketplace", "package-2020-12-01"),

services/autorust/codegen/src/codegen.rs

Lines changed: 77 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ pub enum Error {
8383
PropertyName(#[source] crate::identifier::Error),
8484
#[error("creating name for module: {0}")]
8585
ModuleName(#[source] crate::identifier::Error),
86+
#[error("creating name for enum variant: {0}")]
87+
EnumVariantName(#[source] crate::identifier::Error),
8688
#[error("creating name for enum {property}: {source}")]
8789
EnumName {
8890
source: crate::identifier::Error,
@@ -117,13 +119,6 @@ pub enum Error {
117119
StatusCode(#[from] crate::status_codes::Error),
118120
}
119121

120-
/// Whether or not to pass a type is a reference.
121-
#[derive(Copy, Clone)]
122-
pub enum AsReference {
123-
True,
124-
False,
125-
}
126-
127122
pub fn is_vec(ts: &TokenStream) -> bool {
128123
ts.to_string().starts_with("Vec <")
129124
}
@@ -181,70 +176,104 @@ pub fn enum_values_as_strings(values: &[Value]) -> Vec<&str> {
181176
.collect()
182177
}
183178

184-
pub fn get_type_name_for_schema(schema: &SchemaCommon, as_ref: AsReference) -> Result<TokenStream, Error> {
185-
if let Some(schema_type) = &schema.type_ {
179+
pub enum TypeName {
180+
Reference(String),
181+
Array(Box<TypeName>),
182+
Value,
183+
Bytes,
184+
Int32,
185+
Int64,
186+
Float32,
187+
Float64,
188+
Boolean,
189+
String,
190+
}
191+
192+
impl TypeName {
193+
pub fn to_token_stream(&self, as_ref: bool, qualify_models: bool) -> Result<TokenStream, Error> {
194+
Ok(match self {
195+
TypeName::Reference(name) => {
196+
let idt = ident(&name.to_camel_case()).map_err(Error::TypeNameForSchemaRef)?;
197+
let idt = if qualify_models {
198+
quote! { models::#idt }
199+
} else {
200+
idt
201+
};
202+
match as_ref {
203+
true => quote! { &#idt },
204+
false => idt,
205+
}
206+
}
207+
TypeName::Array(vec_items_typ) => {
208+
let vec_items_typ = vec_items_typ.to_token_stream(as_ref, qualify_models)?;
209+
match as_ref {
210+
true => quote! { &[#vec_items_typ] },
211+
false => quote! { Vec<#vec_items_typ> },
212+
}
213+
}
214+
TypeName::Value => match as_ref {
215+
true => quote! { &serde_json::Value },
216+
false => quote! { serde_json::Value },
217+
},
218+
TypeName::Bytes => quote! { bytes::Bytes },
219+
TypeName::Int32 => quote! { i32 },
220+
TypeName::Int64 => quote! { i64 },
221+
TypeName::Float32 => quote! { f32 },
222+
TypeName::Float64 => quote! { f64 },
223+
TypeName::Boolean => quote! { bool },
224+
TypeName::String => match as_ref {
225+
true => quote! { &str },
226+
false => quote! { String },
227+
},
228+
})
229+
}
230+
}
231+
232+
pub fn get_type_name_for_schema(schema: &SchemaCommon) -> Result<TypeName, Error> {
233+
Ok(if let Some(schema_type) = &schema.type_ {
186234
let format = schema.format.as_deref();
187-
let ts = match schema_type {
235+
match schema_type {
188236
DataType::Array => {
189237
let items = get_schema_array_items(schema)?;
190-
let vec_items_typ = get_type_name_for_schema_ref(items, as_ref)?;
191-
match as_ref {
192-
AsReference::True => quote! { &[#vec_items_typ] },
193-
AsReference::False => quote! { Vec<#vec_items_typ> },
194-
}
238+
let vec_items_typ = get_type_name_for_schema_ref(items)?;
239+
TypeName::Array(Box::new(vec_items_typ))
195240
}
196241
DataType::Integer => {
197242
if format == Some("int32") {
198-
quote! { i32 }
243+
TypeName::Int32
199244
} else {
200-
quote! { i64 }
245+
TypeName::Int64
201246
}
202247
}
203248
DataType::Number => {
204249
if format == Some("float") {
205-
quote! { f32 }
250+
TypeName::Float32
206251
} else {
207-
quote! { f64 }
252+
TypeName::Float64
208253
}
209254
}
210-
DataType::String => match as_ref {
211-
AsReference::True => quote! { &str },
212-
AsReference::False => quote! { String },
213-
},
214-
DataType::Boolean => quote! { bool },
215-
DataType::Object => match as_ref {
216-
AsReference::True => quote! { &serde_json::Value },
217-
AsReference::False => quote! { serde_json::Value },
218-
},
219-
DataType::File => {
220-
quote! { bytes::Bytes }
221-
}
222-
};
223-
Ok(ts)
255+
DataType::String => TypeName::String,
256+
DataType::Boolean => TypeName::Boolean,
257+
DataType::Object => TypeName::Value,
258+
DataType::File => TypeName::Bytes,
259+
}
224260
} else {
225261
// eprintln!(
226262
// "WARN unknown type in get_type_name_for_schema, description {:?}",
227263
// schema.description
228264
// );
229-
match as_ref {
230-
AsReference::True => Ok(quote! { &serde_json::Value }),
231-
AsReference::False => Ok(quote! { serde_json::Value }),
232-
}
233-
}
265+
TypeName::Value
266+
})
234267
}
235268

236-
pub fn get_type_name_for_schema_ref(schema: &ReferenceOr<Schema>, as_ref: AsReference) -> Result<TokenStream, Error> {
237-
match schema {
269+
pub fn get_type_name_for_schema_ref(schema: &ReferenceOr<Schema>) -> Result<TypeName, Error> {
270+
Ok(match schema {
238271
ReferenceOr::Reference { reference, .. } => {
239-
let name = &reference.name.as_ref().ok_or(Error::NoNameForRef)?;
240-
let idt = ident(&name.to_camel_case()).map_err(Error::TypeNameForSchemaRef)?;
241-
match as_ref {
242-
AsReference::True => Ok(quote! { &#idt }),
243-
AsReference::False => Ok(quote! { #idt }),
244-
}
272+
let name = reference.name.as_ref().ok_or(Error::NoNameForRef)?;
273+
TypeName::Reference(name.to_owned())
245274
}
246-
ReferenceOr::Item(schema) => get_type_name_for_schema(&schema.common, as_ref),
247-
}
275+
ReferenceOr::Item(schema) => get_type_name_for_schema(&schema.common)?,
276+
})
248277
}
249278

250279
pub fn create_mod(api_version: &str) -> TokenStream {

services/autorust/codegen/src/codegen_models.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
codegen::{
33
create_generated_by_header, enum_values_as_strings, get_schema_array_items, get_type_name_for_schema, get_type_name_for_schema_ref,
4-
is_array, is_basic_type, is_local_enum, is_local_struct, is_vec, require, AsReference, Error,
4+
is_array, is_basic_type, is_local_enum, is_local_struct, is_vec, require, Error,
55
},
66
identifier::{ident, CamelCaseIdent},
77
spec, CodeGen, PropertyName, ResolvedSchema,
@@ -51,8 +51,8 @@ pub fn create_models(cg: &CodeGen) -> Result<TokenStream, Error> {
5151
}
5252

5353
// any referenced schemas from other files
54-
for (doc_file, doc) in cg.spec.input_docs() {
55-
for reference in openapi::get_api_schema_references(doc) {
54+
for (doc_file, api) in cg.spec.input_docs() {
55+
for reference in openapi::get_api_schema_references(doc_file, api) {
5656
add_schema_refs(cg, &mut all_schemas, doc_file, reference)?;
5757
}
5858
}
@@ -86,7 +86,7 @@ pub fn create_models(cg: &CodeGen) -> Result<TokenStream, Error> {
8686

8787
fn create_basic_type_alias(property_name: &str, property: &ResolvedSchema) -> Result<(TokenStream, TokenStream), Error> {
8888
let id = ident(&property_name.to_camel_case()).map_err(Error::StructName)?;
89-
let value = get_type_name_for_schema(&property.schema.common, AsReference::False)?;
89+
let value = get_type_name_for_schema(&property.schema.common)?.to_token_stream(false, false)?;
9090
Ok((id, value))
9191
}
9292

@@ -158,7 +158,7 @@ fn create_enum(
158158
fn create_vec_alias(alias_name: &str, schema: &ResolvedSchema) -> Result<TokenStream, Error> {
159159
let items = get_schema_array_items(&schema.schema.common)?;
160160
let typ = ident(&alias_name.to_camel_case()).map_err(Error::VecAliasName)?;
161-
let items_typ = get_type_name_for_schema_ref(items, AsReference::False)?;
161+
let items_typ = get_type_name_for_schema_ref(items)?.to_token_stream(false, false)?;
162162
Ok(quote! { pub type #typ = Vec<#items_typ>; })
163163
}
164164

@@ -172,7 +172,7 @@ fn create_struct(cg: &CodeGen, doc_file: &Path, struct_name: &str, schema: &Reso
172172
let required: HashSet<&str> = schema.schema.required.iter().map(String::as_str).collect();
173173

174174
for schema in &schema.schema.all_of {
175-
let type_name = get_type_name_for_schema_ref(schema, AsReference::False)?;
175+
let type_name = get_type_name_for_schema_ref(schema)?.to_token_stream(false, false)?;
176176
let field_name = ident(&type_name.to_string().to_snake_case()).map_err(Error::StructFieldName)?;
177177
props.extend(quote! {
178178
#[serde(flatten)]
@@ -285,7 +285,10 @@ fn create_struct_field_type(
285285
// println!("creating local struct {:?} {}", tp_name, tps.len());
286286
Ok((tp_name, tps))
287287
} else {
288-
Ok((get_type_name_for_schema(&property.schema.common, AsReference::False)?, Vec::new()))
288+
Ok((
289+
get_type_name_for_schema(&property.schema.common)?.to_token_stream(false, false)?,
290+
Vec::new(),
291+
))
289292
}
290293
}
291294
}

0 commit comments

Comments
 (0)