Skip to content

Commit

Permalink
code is hard
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtbuilds committed Dec 17, 2023
1 parent 1d4879d commit b05b240
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
13 changes: 8 additions & 5 deletions libninja/src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ 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;
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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)*
Expand All @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions libninja/src/rust/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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."),
Expand All @@ -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 {
Expand All @@ -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
}
};
Expand Down

0 comments on commit b05b240

Please sign in to comment.