Skip to content

Commit 70c44d3

Browse files
committed
Reformat
1 parent e604370 commit 70c44d3

File tree

4 files changed

+108
-71
lines changed

4 files changed

+108
-71
lines changed

src/rust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ pub(crate) mod error_gen;
1818
pub(crate) mod lib_gen;
1919
pub(crate) mod model_gen;
2020
mod printer;
21-
pub(crate) mod types;
21+
pub(crate) mod types;

src/rust/client_gen.rs

Lines changed: 96 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ impl RequestBodyParams {
197197
}
198198

199199
pub fn get_default_request_body_param(&self) -> Option<&Vec<Param>> {
200-
self.params.values().next().filter(|_| self.has_single_content_type())
200+
self.params
201+
.values()
202+
.next()
203+
.filter(|_| self.has_single_content_type())
201204
}
202205
}
203206

@@ -324,38 +327,47 @@ fn request_body_params(
324327
body: &ReferenceOr<RequestBody>,
325328
ref_cache: &mut RefCache,
326329
) -> Result<RequestBodyParams> {
327-
328330
let mut content_type_params = HashMap::new();
329331

330332
match body {
331-
ReferenceOr::Reference { reference } => return Err(Error::unimplemented(format!(
332-
"Unexpected ref request body: '{reference}'."
333-
))),
333+
ReferenceOr::Reference { reference } => {
334+
return Err(Error::unimplemented(format!(
335+
"Unexpected ref request body: '{reference}'."
336+
)))
337+
}
334338
ReferenceOr::Item(body) => {
335-
for (content_type, media_type) in &body.content {
336-
339+
for (content_type, media_type) in &body.content {
337340
if content_type.starts_with("application/json") {
338341
let schema = match &media_type.schema {
339342
None => Err(Error::unimplemented("JSON content without schema.")),
340343
Some(schema) => Ok(schema),
341344
};
342345

343-
content_type_params.insert(ContentType(content_type.clone()), vec![Param {
344-
original_name: "".to_string(),
345-
name: "value".to_string(),
346-
tpe: ref_or_schema_type(schema?, ref_cache, Some(content_type.clone()))?,
347-
required: body.required,
348-
kind: ParamKind::Body,
349-
}]);
346+
content_type_params.insert(
347+
ContentType(content_type.clone()),
348+
vec![Param {
349+
original_name: "".to_string(),
350+
name: "value".to_string(),
351+
tpe: ref_or_schema_type(
352+
schema?,
353+
ref_cache,
354+
Some(content_type.clone()),
355+
)?,
356+
required: body.required,
357+
kind: ParamKind::Body,
358+
}],
359+
);
350360
} else if content_type == "application/octet-stream" {
351-
content_type_params.insert(ContentType(content_type.clone()), vec![Param {
352-
original_name: "".to_string(),
353-
name: "value".to_string(),
354-
tpe: DataType::Binary,
355-
required: body.required,
356-
kind: ParamKind::Body,
357-
}]);
358-
361+
content_type_params.insert(
362+
ContentType(content_type.clone()),
363+
vec![Param {
364+
original_name: "".to_string(),
365+
name: "value".to_string(),
366+
tpe: DataType::Binary,
367+
required: body.required,
368+
kind: ParamKind::Body,
369+
}],
370+
);
359371
} else if content_type == "application/x-yaml" {
360372
let schema = match &media_type.schema {
361373
None => Err(Error::unimplemented("YAML content without schema.")),
@@ -371,14 +383,17 @@ fn request_body_params(
371383
};
372384

373385
content_type_params.insert(ContentType(content_type.clone()), vec![param]);
374-
375386
} else if content_type == "multipart/form-data" {
376387
match &media_type.schema {
377-
None => return Err(Error::unimplemented("Multipart content without schema.")),
388+
None => {
389+
return Err(Error::unimplemented("Multipart content without schema."))
390+
}
378391
Some(schema) => match schema {
379-
ReferenceOr::Reference { reference } => return Err(Error::unimplemented(format!(
380-
"Unexpected ref multipart schema: '{reference}'."
381-
))),
392+
ReferenceOr::Reference { reference } => {
393+
return Err(Error::unimplemented(format!(
394+
"Unexpected ref multipart schema: '{reference}'."
395+
)))
396+
}
382397
ReferenceOr::Item(schema) => match &schema.schema_kind {
383398
SchemaKind::Type(Type::Object(obj)) => {
384399
fn multipart_param(
@@ -396,7 +411,8 @@ fn request_body_params(
396411
})
397412
}
398413

399-
let params = obj.properties
414+
let params = obj
415+
.properties
400416
.iter()
401417
.map(|(name, schema)| {
402418
multipart_param(
@@ -408,31 +424,33 @@ fn request_body_params(
408424
})
409425
.collect::<Result<Vec<_>>>()?;
410426

411-
content_type_params.insert(ContentType(content_type.clone()), params);
427+
content_type_params
428+
.insert(ContentType(content_type.clone()), params);
429+
}
430+
_ => {
431+
return Err(Error::unimplemented(
432+
"Object schema expected for multipart request body.",
433+
))
412434
}
413-
_ => return Err(Error::unimplemented(
414-
"Object schema expected for multipart request body.",
415-
)),
416435
},
417436
},
418437
}
419438
} else {
420-
return Err(Error::unimplemented(format!(
439+
return Err(Error::unimplemented(format!(
421440
"Request body content type: '{content_type}'."
422-
)))
441+
)));
423442
}
424443
}
425444
}
426445
}
427446

428447
Ok(RequestBodyParams {
429-
params: content_type_params
448+
params: content_type_params,
430449
})
431450
}
432451

433452
fn parameters(op: &PathOperation, ref_cache: &mut RefCache) -> Result<Vec<Param>> {
434-
op
435-
.op
453+
op.op
436454
.parameters
437455
.iter()
438456
.map(|p| parameter(p, ref_cache))
@@ -490,7 +508,11 @@ fn response_type(response: &ReferenceOr<Response>, ref_cache: &mut RefCache) ->
490508
Some(schema) => Ok(schema),
491509
};
492510

493-
Ok(ref_or_schema_type(schema?, ref_cache, Some(content_type.clone()))?)
511+
Ok(ref_or_schema_type(
512+
schema?,
513+
ref_cache,
514+
Some(content_type.clone()),
515+
)?)
494516
} else if content_type == "application/octet-stream" {
495517
Ok(DataType::Binary)
496518
} else {
@@ -580,17 +602,20 @@ fn trait_method(
580602
}
581603

582604
let method_name = format!("{}_json", name);
583-
methods.push(
584-
Method {
585-
name: method_name,
586-
path: op.path.clone(),
587-
original_path: op.original_path.clone(),
588-
http_method: op.method.to_string(),
589-
params: new_params.clone(),
590-
result: result_type.clone(),
591-
result_status_code: result_code.clone(),
592-
errors: method_errors(&op.op.responses.responses, result_code.clone(), ref_cache)?,
593-
});
605+
methods.push(Method {
606+
name: method_name,
607+
path: op.path.clone(),
608+
original_path: op.original_path.clone(),
609+
http_method: op.method.to_string(),
610+
params: new_params.clone(),
611+
result: result_type.clone(),
612+
result_status_code: result_code.clone(),
613+
errors: method_errors(
614+
&op.op.responses.responses,
615+
result_code.clone(),
616+
ref_cache,
617+
)?,
618+
});
594619
} else if content_type.is_yaml() {
595620
let mut new_params = main_params.clone();
596621

@@ -600,17 +625,20 @@ fn trait_method(
600625

601626
let method_name = format!("{}_yaml", name);
602627

603-
methods.push(
604-
Method {
605-
name: method_name,
606-
path: op.path.clone(),
607-
original_path: op.original_path.clone(),
608-
http_method: op.method.to_string(),
609-
params: new_params.clone(),
610-
result: result_type.clone(),
611-
result_status_code: result_code.clone(),
612-
errors: method_errors(&op.op.responses.responses, result_code.clone(), ref_cache)?,
613-
});
628+
methods.push(Method {
629+
name: method_name,
630+
path: op.path.clone(),
631+
original_path: op.original_path.clone(),
632+
http_method: op.method.to_string(),
633+
params: new_params.clone(),
634+
result: result_type.clone(),
635+
result_status_code: result_code.clone(),
636+
errors: method_errors(
637+
&op.op.responses.responses,
638+
result_code.clone(),
639+
ref_cache,
640+
)?,
641+
});
614642
}
615643
}
616644

@@ -951,10 +979,14 @@ fn render_method_implementation(method: &Method, error_kind: &ErrorKind) -> Rust
951979
)
952980
+ NewLine
953981
} else if param.tpe == DataType::Yaml {
954-
line(unit() + "request = request.body(serde_yaml::to_string(" + &param.name + ").unwrap_or_default().into_bytes());") +
955-
line(
956-
r#"request = request.header(reqwest::header::CONTENT_TYPE, "application/x-yaml");"#,
957-
) + NewLine
982+
line(
983+
unit()
984+
+ "request = request.body(serde_yaml::to_string("
985+
+ &param.name
986+
+ ").unwrap_or_default().into_bytes());",
987+
) + line(
988+
r#"request = request.header(reqwest::header::CONTENT_TYPE, "application/x-yaml");"#,
989+
) + NewLine
958990
}
959991
// Not sure why everything else is assumed to be json (previously)
960992
else {

src/rust/printer.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ pub fn rust_name_with_alias(import: &str, name: &str, alias: &str) -> TreePrinte
147147
})
148148
}
149149

150-
151150
impl IntoRustTree for TreePrinter<RustContext> {
152151
fn tree(self) -> TreePrinter<RustContext> {
153152
self

src/rust/types.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ pub enum DataType {
7676
Yaml,
7777
}
7878

79-
8079
pub fn escape_keywords(name: &str) -> String {
8180
if name == "type" {
8281
"r#type".to_string()
@@ -166,7 +165,11 @@ pub fn ref_type_name(reference: &str, ref_cache: &mut RefCache) -> Result<DataTy
166165
}))
167166
}
168167

169-
fn schema_type(schema: &Schema, ref_cache: &mut RefCache, content_type: Option<String>) -> Result<DataType> {
168+
fn schema_type(
169+
schema: &Schema,
170+
ref_cache: &mut RefCache,
171+
content_type: Option<String>,
172+
) -> Result<DataType> {
170173
match &schema.schema_kind {
171174
SchemaKind::Type(tpe) => match tpe {
172175
Type::String(string_type) => {
@@ -252,19 +255,22 @@ fn schema_type(schema: &Schema, ref_cache: &mut RefCache, content_type: Option<S
252255
} else if &content_type == "application/x-yaml" {
253256
Ok(DataType::Yaml)
254257
} else {
255-
Err(Error::unexpected(format!("Cannot resolve the data type for content_type {} with `any` schema-kind", content_type)))
258+
Err(Error::unexpected(format!(
259+
"Cannot resolve the data type for content_type {} with `any` schema-kind",
260+
content_type
261+
)))
256262
}
257263
} else {
258264
Err(Error::unexpected("Cannot resolve the data type for any schema-kind with no details on content_type"))
259265
}
260-
},
266+
}
261267
}
262268
}
263269

264270
pub fn ref_or_schema_type(
265271
ref_or_schema: &ReferenceOr<Schema>,
266272
ref_cache: &mut RefCache,
267-
content_type: Option<String>
273+
content_type: Option<String>,
268274
) -> Result<DataType> {
269275
match ref_or_schema {
270276
ReferenceOr::Reference { reference } => ref_type_name(reference, ref_cache),

0 commit comments

Comments
 (0)