feat(codegen): decreased complexity of generated code

This commit is contained in:
pyrite 2025-10-26 21:15:58 +01:00
parent 3d80414441
commit 12cfcf7f11
Signed by: pyrite
GPG key ID: 7F1BA9170CD35D15
3 changed files with 12 additions and 9 deletions

View file

@ -1,7 +1,7 @@
[package]
name = "torn-api-codegen"
authors = ["Pyrit [2111649]"]
version = "0.7.4"
version = "0.7.5"
edition = "2021"
description = "Contains the v2 torn API model descriptions and codegen for the bindings"
license = { workspace = true }

View file

@ -258,12 +258,14 @@ impl Path {
} else {
let ty = if param.required {
convert_field.push(quote! {
.chain(std::iter::once(&self.#name).map(|v| (#query_val, v.to_string())))
parameters.push((#query_val, self.#name.to_string()));
});
ty
} else {
convert_field.push(quote! {
.chain(self.#name.as_ref().into_iter().map(|v| (#query_val, v.to_string())))
if let Some(value) = &self.#name {
parameters.push((#query_val, value.to_string()));
}
});
quote! { Option<#ty>}
};
@ -295,8 +297,8 @@ impl Path {
let mut path_fmt_str = String::new();
for seg in &self.segments {
match seg {
PathSegment::Constant(val) => _ = write!(path_fmt_str, "/{}", val),
PathSegment::Parameter { name } => _ = write!(path_fmt_str, "/{{{}}}", name),
PathSegment::Constant(val) => _ = write!(path_fmt_str, "/{val}"),
PathSegment::Parameter { name } => _ = write!(path_fmt_str, "/{{{name}}}"),
}
}
@ -324,14 +326,15 @@ impl Path {
type Response = #response_ty;
fn into_request(self) -> (Self::Discriminant, crate::request::ApiRequest) {
let path = format!(#path_fmt_str, #(#fmt_val),*);
let mut parameters = Vec::new();
#(#convert_field)*
#[allow(unused_parens)]
(
(#(#discriminant_val),*),
crate::request::ApiRequest {
path,
parameters: std::iter::empty()
#(#convert_field)*
.collect(),
parameters,
}
)
}