From 70c44d3586e026605444ab0589aa7b9674fbd810 Mon Sep 17 00:00:00 2001 From: Afsal Thaj Date: Mon, 4 Nov 2024 17:16:13 +1100 Subject: [PATCH] Reformat --- src/rust.rs | 2 +- src/rust/client_gen.rs | 160 ++++++++++++++++++++++++----------------- src/rust/printer.rs | 1 - src/rust/types.rs | 16 +++-- 4 files changed, 108 insertions(+), 71 deletions(-) diff --git a/src/rust.rs b/src/rust.rs index c6233b9..8042153 100644 --- a/src/rust.rs +++ b/src/rust.rs @@ -18,4 +18,4 @@ pub(crate) mod error_gen; pub(crate) mod lib_gen; pub(crate) mod model_gen; mod printer; -pub(crate) mod types; \ No newline at end of file +pub(crate) mod types; diff --git a/src/rust/client_gen.rs b/src/rust/client_gen.rs index 304640e..3d13608 100644 --- a/src/rust/client_gen.rs +++ b/src/rust/client_gen.rs @@ -197,7 +197,10 @@ impl RequestBodyParams { } pub fn get_default_request_body_param(&self) -> Option<&Vec> { - self.params.values().next().filter(|_| self.has_single_content_type()) + self.params + .values() + .next() + .filter(|_| self.has_single_content_type()) } } @@ -324,38 +327,47 @@ fn request_body_params( body: &ReferenceOr, ref_cache: &mut RefCache, ) -> Result { - let mut content_type_params = HashMap::new(); match body { - ReferenceOr::Reference { reference } => return Err(Error::unimplemented(format!( - "Unexpected ref request body: '{reference}'." - ))), + ReferenceOr::Reference { reference } => { + return Err(Error::unimplemented(format!( + "Unexpected ref request body: '{reference}'." + ))) + } ReferenceOr::Item(body) => { - for (content_type, media_type) in &body.content { - + for (content_type, media_type) in &body.content { if content_type.starts_with("application/json") { let schema = match &media_type.schema { None => Err(Error::unimplemented("JSON content without schema.")), Some(schema) => Ok(schema), }; - content_type_params.insert(ContentType(content_type.clone()), vec![Param { - original_name: "".to_string(), - name: "value".to_string(), - tpe: ref_or_schema_type(schema?, ref_cache, Some(content_type.clone()))?, - required: body.required, - kind: ParamKind::Body, - }]); + content_type_params.insert( + ContentType(content_type.clone()), + vec![Param { + original_name: "".to_string(), + name: "value".to_string(), + tpe: ref_or_schema_type( + schema?, + ref_cache, + Some(content_type.clone()), + )?, + required: body.required, + kind: ParamKind::Body, + }], + ); } else if content_type == "application/octet-stream" { - content_type_params.insert(ContentType(content_type.clone()), vec![Param { - original_name: "".to_string(), - name: "value".to_string(), - tpe: DataType::Binary, - required: body.required, - kind: ParamKind::Body, - }]); - + content_type_params.insert( + ContentType(content_type.clone()), + vec![Param { + original_name: "".to_string(), + name: "value".to_string(), + tpe: DataType::Binary, + required: body.required, + kind: ParamKind::Body, + }], + ); } else if content_type == "application/x-yaml" { let schema = match &media_type.schema { None => Err(Error::unimplemented("YAML content without schema.")), @@ -371,14 +383,17 @@ fn request_body_params( }; content_type_params.insert(ContentType(content_type.clone()), vec![param]); - } else if content_type == "multipart/form-data" { match &media_type.schema { - None => return Err(Error::unimplemented("Multipart content without schema.")), + None => { + return Err(Error::unimplemented("Multipart content without schema.")) + } Some(schema) => match schema { - ReferenceOr::Reference { reference } => return Err(Error::unimplemented(format!( - "Unexpected ref multipart schema: '{reference}'." - ))), + ReferenceOr::Reference { reference } => { + return Err(Error::unimplemented(format!( + "Unexpected ref multipart schema: '{reference}'." + ))) + } ReferenceOr::Item(schema) => match &schema.schema_kind { SchemaKind::Type(Type::Object(obj)) => { fn multipart_param( @@ -396,7 +411,8 @@ fn request_body_params( }) } - let params = obj.properties + let params = obj + .properties .iter() .map(|(name, schema)| { multipart_param( @@ -408,31 +424,33 @@ fn request_body_params( }) .collect::>>()?; - content_type_params.insert(ContentType(content_type.clone()), params); + content_type_params + .insert(ContentType(content_type.clone()), params); + } + _ => { + return Err(Error::unimplemented( + "Object schema expected for multipart request body.", + )) } - _ => return Err(Error::unimplemented( - "Object schema expected for multipart request body.", - )), }, }, } } else { - return Err(Error::unimplemented(format!( + return Err(Error::unimplemented(format!( "Request body content type: '{content_type}'." - ))) + ))); } } } } Ok(RequestBodyParams { - params: content_type_params + params: content_type_params, }) } fn parameters(op: &PathOperation, ref_cache: &mut RefCache) -> Result> { - op - .op + op.op .parameters .iter() .map(|p| parameter(p, ref_cache)) @@ -490,7 +508,11 @@ fn response_type(response: &ReferenceOr, ref_cache: &mut RefCache) -> Some(schema) => Ok(schema), }; - Ok(ref_or_schema_type(schema?, ref_cache, Some(content_type.clone()))?) + Ok(ref_or_schema_type( + schema?, + ref_cache, + Some(content_type.clone()), + )?) } else if content_type == "application/octet-stream" { Ok(DataType::Binary) } else { @@ -580,17 +602,20 @@ fn trait_method( } let method_name = format!("{}_json", name); - methods.push( - Method { - name: method_name, - path: op.path.clone(), - original_path: op.original_path.clone(), - http_method: op.method.to_string(), - params: new_params.clone(), - result: result_type.clone(), - result_status_code: result_code.clone(), - errors: method_errors(&op.op.responses.responses, result_code.clone(), ref_cache)?, - }); + methods.push(Method { + name: method_name, + path: op.path.clone(), + original_path: op.original_path.clone(), + http_method: op.method.to_string(), + params: new_params.clone(), + result: result_type.clone(), + result_status_code: result_code.clone(), + errors: method_errors( + &op.op.responses.responses, + result_code.clone(), + ref_cache, + )?, + }); } else if content_type.is_yaml() { let mut new_params = main_params.clone(); @@ -600,17 +625,20 @@ fn trait_method( let method_name = format!("{}_yaml", name); - methods.push( - Method { - name: method_name, - path: op.path.clone(), - original_path: op.original_path.clone(), - http_method: op.method.to_string(), - params: new_params.clone(), - result: result_type.clone(), - result_status_code: result_code.clone(), - errors: method_errors(&op.op.responses.responses, result_code.clone(), ref_cache)?, - }); + methods.push(Method { + name: method_name, + path: op.path.clone(), + original_path: op.original_path.clone(), + http_method: op.method.to_string(), + params: new_params.clone(), + result: result_type.clone(), + result_status_code: result_code.clone(), + errors: method_errors( + &op.op.responses.responses, + result_code.clone(), + ref_cache, + )?, + }); } } @@ -951,10 +979,14 @@ fn render_method_implementation(method: &Method, error_kind: &ErrorKind) -> Rust ) + NewLine } else if param.tpe == DataType::Yaml { - line(unit() + "request = request.body(serde_yaml::to_string(" + ¶m.name + ").unwrap_or_default().into_bytes());") + - line( - r#"request = request.header(reqwest::header::CONTENT_TYPE, "application/x-yaml");"#, - ) + NewLine + line( + unit() + + "request = request.body(serde_yaml::to_string(" + + ¶m.name + + ").unwrap_or_default().into_bytes());", + ) + line( + r#"request = request.header(reqwest::header::CONTENT_TYPE, "application/x-yaml");"#, + ) + NewLine } // Not sure why everything else is assumed to be json (previously) else { diff --git a/src/rust/printer.rs b/src/rust/printer.rs index e0e5be5..9ef5db7 100644 --- a/src/rust/printer.rs +++ b/src/rust/printer.rs @@ -147,7 +147,6 @@ pub fn rust_name_with_alias(import: &str, name: &str, alias: &str) -> TreePrinte }) } - impl IntoRustTree for TreePrinter { fn tree(self) -> TreePrinter { self diff --git a/src/rust/types.rs b/src/rust/types.rs index 2473cb2..2924c05 100644 --- a/src/rust/types.rs +++ b/src/rust/types.rs @@ -76,7 +76,6 @@ pub enum DataType { Yaml, } - pub fn escape_keywords(name: &str) -> String { if name == "type" { "r#type".to_string() @@ -166,7 +165,11 @@ pub fn ref_type_name(reference: &str, ref_cache: &mut RefCache) -> Result) -> Result { +fn schema_type( + schema: &Schema, + ref_cache: &mut RefCache, + content_type: Option, +) -> Result { match &schema.schema_kind { SchemaKind::Type(tpe) => match tpe { Type::String(string_type) => { @@ -252,19 +255,22 @@ fn schema_type(schema: &Schema, ref_cache: &mut RefCache, content_type: Option, ref_cache: &mut RefCache, - content_type: Option + content_type: Option, ) -> Result { match ref_or_schema { ReferenceOr::Reference { reference } => ref_type_name(reference, ref_cache),