From b05b2404ab54bff95c2c0fa517a4fa745cdb7e14 Mon Sep 17 00:00:00 2001 From: Kurt Wolf Date: Sun, 17 Dec 2023 17:23:51 -0500 Subject: [PATCH] code is hard --- libninja/src/rust.rs | 13 ++++++++----- libninja/src/rust/request.rs | 12 +++++++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/libninja/src/rust.rs b/libninja/src/rust.rs index f7e9fc1..fbbb363 100644 --- a/libninja/src/rust.rs +++ b/libninja/src/rust.rs @@ -17,7 +17,8 @@ use format::format_code; use ln_core::{copy_builtin_files, copy_builtin_templates, create_context, get_template_file, prepare_templates}; use ::mir::{Visibility, Import, File}; use ln_core::fs; -use hir::{HirSpec, IntegerSerialization, DateSerialization}; +use hir::{HirSpec, IntegerSerialization, DateSerialization, Location, Parameter}; +use mir::Ident; use crate::{add_operation_models, extract_spec, PackageConfig, OutputConfig}; use crate::rust::client::build_Client_authenticate; @@ -25,7 +26,7 @@ pub use crate::rust::codegen::generate_example; use crate::rust::codegen::{codegen_function, sanitize_filename, ToRustCode}; use crate::rust::io::write_rust_file_to_path; use crate::rust::lower_mir::{generate_model_rs, generate_single_model_file}; -use crate::rust::request::{build_request_struct, build_request_struct_builder_methods, build_url, generate_request_model_rs}; +use crate::rust::request::{assign_inputs_to_request, build_request_struct, build_request_struct_builder_methods, build_url, generate_request_model_rs}; pub mod client; pub mod codegen; @@ -262,7 +263,6 @@ fn write_lib_rs(spec: &HirSpec, extras: &Extras, opts: &PackageConfig) -> Result Ok(()) } - fn write_request_module(spec: &HirSpec, opts: &PackageConfig) -> Result<()> { let src_path = opts.dest.join("src"); let client_name = opts.client_name().to_rust_struct(); @@ -293,6 +293,9 @@ fn write_request_module(spec: &HirSpec, opts: &PackageConfig) -> Result<()> { .into_iter() .map(|s| codegen_function(s, quote! { mut self , })); + + let assign_inputs = assign_inputs_to_request(&operation.parameters); + let file = quote! { use crate::#client_name; #(#request_structs)* @@ -306,10 +309,10 @@ fn write_request_module(spec: &HirSpec, opts: &PackageConfig) -> Result<()> { type IntoFuture = ::futures::future::BoxFuture<'a, Self::Output>; fn into_future(self) -> Self::IntoFuture { - Box::pin(async { + Box::pin(async move { let url = #url; let mut r = self.client.client.#method(url); - r = r.set_query(self.params); + #assign_inputs #authenticate let res = r.await?; res.json().map_err(Into::into) diff --git a/libninja/src/rust/request.rs b/libninja/src/rust/request.rs index b305ef8..0b80c11 100644 --- a/libninja/src/rust/request.rs +++ b/libninja/src/rust/request.rs @@ -17,6 +17,12 @@ use crate::rust::codegen::ToRustIdent; use crate::rust::codegen::ToRustType; pub fn assign_inputs_to_request(inputs: &[Parameter]) -> TokenStream { + let params_except_path: Vec<&Parameter> = inputs.iter().filter(|&input| input.location != Location::Path).collect(); + if params_except_path.iter().all(|&input| input.location == Location::Query) { + return quote! { + r = r.set_query(self.params); + }; + } let assigns = inputs .iter() .filter(|input| input.location != Location::Path) @@ -32,7 +38,7 @@ pub fn assign_inputs_to_request(inputs: &[Parameter]) -> TokenStream { } else if input.optional { quote! { unwrapped } } else { - quote! { self.#field } + quote! { self.params.#field } }; match input.location { Location::Path => panic!("Should be filtered."), @@ -55,7 +61,7 @@ pub fn assign_inputs_to_request(inputs: &[Parameter]) -> TokenStream { let container = if input.optional { quote! { unwrapped } } else { - quote! { self.#field } + quote! { self.params.#field } }; assign = quote! { for item in #container { @@ -66,7 +72,7 @@ pub fn assign_inputs_to_request(inputs: &[Parameter]) -> TokenStream { if input.optional { assign = quote! { - if let Some(ref unwrapped) = self.#field { + if let Some(ref unwrapped) = self.params.#field { #assign } };