Skip to content

Commit 7011839

Browse files
authored
Optimize: replace String with Cow<'_, str> (#60)
* Optimize * Replace `String` with `Cow<'_, str>` * Revert ToTypesenseField * Clippy * Refactor * Fix UnknownValue * Optimize collection API * Owned data in API response models * Rm unused api * XTask schemas_mark_owned_data * Errors no lifetimes * Invert to is-used-as-input * Less lifetimes * OpenAPI fields reorder * Rm PhantomData * Rename flag to has-borrowed-data * Refactor
1 parent 4965034 commit 7011839

File tree

177 files changed

+2252
-2586
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

177 files changed

+2252
-2586
lines changed

.cargo/config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[alias]
22
xtask = "run --package xtask --"
3-
test-clean = "run --package xtask -- test-clean"
3+
test-clean = "run --package xtask --features typesense -- test-clean"

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ base64 = "0.22"
2323
bon = "3"
2424
clap = { version = "4", features = ["derive"] }
2525
hmac = "0.12"
26+
indexmap = { version = "2", features = ["serde"] }
2627
reqwest-retry = "0.7"
2728
serde = { version = "1", features = ["derive"] }
2829
serde_json = "1.0"

openapi-generator-template/model.mustache

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{{>partial_header}}
22
use crate::models;
3+
use ::std::borrow::Cow;
34
use serde::{Deserialize, Serialize};
45
{{#models}}
56
{{#model}}
@@ -123,11 +124,12 @@ impl Default for {{classname}} {
123124
{{^discriminator}}
124125
{{#vendorExtensions.x-rust-builder}}
125126
#[derive(bon::Builder)]
127+
#[builder(on(Cow<'_, str>, into))]
126128
#[builder(on(String, into))]
127-
{{/vendorExtensions.x-rust-builder}}
128-
{{#vendorExtensions.x-rust-has-byte-array}}#[serde_as]
129-
{{/vendorExtensions.x-rust-has-byte-array}}{{#oneOf.isEmpty}}#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
130-
pub struct {{{classname}}}{{{vendorExtensions.x-rust-generic-parameter}}} {
129+
{{/vendorExtensions.x-rust-builder}}{{!
130+
}}{{#vendorExtensions.x-rust-has-byte-array}}#[serde_as]{{/vendorExtensions.x-rust-has-byte-array}}{{!
131+
}}{{#oneOf.isEmpty}}#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
132+
pub struct {{{classname}}}{{#vendorExtensions.x-rust-has-borrowed-data}}<'a{{#vendorExtensions.x-rust-generic-parameter}}, {{{.}}}{{/vendorExtensions.x-rust-generic-parameter}}>{{/vendorExtensions.x-rust-has-borrowed-data}}{{^vendorExtensions.x-rust-has-borrowed-data}}{{#vendorExtensions.x-rust-generic-parameter}}<{{{.}}}>{{/vendorExtensions.x-rust-generic-parameter}}{{/vendorExtensions.x-rust-has-borrowed-data}} {
131133
{{#vars}}
132134
{{#description}}
133135
/// {{{.}}}
@@ -145,32 +147,52 @@ pub struct {{{classname}}}{{{vendorExtensions.x-rust-generic-parameter}}} {
145147
}}{{#isNullable}}Option<{{/isNullable}}{{^required}}Option<{{/required}}{{!
146148
### Enums
147149
}}{{#isEnum}}{{#isArray}}{{#uniqueItems}}std::collections::HashSet<{{/uniqueItems}}{{^uniqueItems}}Vec<{{/uniqueItems}}{{/isArray}}{{{enumName}}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{!
148-
### Non-Enums Start
149-
}}{{^isEnum}}{{!
150150
### Models
151-
}}{{#isModel}}{{^avoidBoxedModels}}Box<{{/avoidBoxedModels}}{{{dataType}}}{{^avoidBoxedModels}}>{{/avoidBoxedModels}}{{/isModel}}{{!
152-
### Primative datatypes
153-
}}{{^isModel}}{{#isByteArray}}Vec<u8>{{/isByteArray}}{{^isByteArray}}{{{dataType}}}{{/isByteArray}}{{/isModel}}{{!
154-
### Non-Enums End
155-
}}{{/isEnum}}{{!
151+
}}{{^isEnum}}{{#isModel}}{{^avoidBoxedModels}}Box<{{/avoidBoxedModels}}{{{dataType}}}{{#vendorExtensions.x-rust-has-borrowed-data}}{{#model}}{{#vendorExtensions.x-rust-has-borrowed-data}}<'a>{{/vendorExtensions.x-rust-has-borrowed-data}}{{^vendorExtensions.x-rust-has-borrowed-data}}<'static>{{/vendorExtensions.x-rust-has-borrowed-data}}{{/model}}{{/vendorExtensions.x-rust-has-borrowed-data}}{{^avoidBoxedModels}}>{{/avoidBoxedModels}}{{/isModel}}{{!
152+
### ByteArray
153+
}}{{^isModel}}{{#isByteArray}}Vec<u8>{{/isByteArray}}{{!
154+
### String
155+
}}{{^isByteArray}}{{#isString}}{{#model}}{{#vendorExtensions.x-rust-has-borrowed-data}}Cow<'a, str>{{/vendorExtensions.x-rust-has-borrowed-data}}{{^vendorExtensions.x-rust-has-borrowed-data}}String{{/vendorExtensions.x-rust-has-borrowed-data}}{{/model}}{{/isString}}{{!
156+
### Arrays
157+
}}{{^isString}}{{#isArray}}Vec<{{#items}}{{!
158+
### Array Models
159+
}}{{#isModel}}{{{dataType}}}{{#vendorExtensions.x-rust-has-borrowed-data}}{{#model}}{{#vendorExtensions.x-rust-has-borrowed-data}}<'a>{{/vendorExtensions.x-rust-has-borrowed-data}}{{^vendorExtensions.x-rust-has-borrowed-data}}<'static>{{/vendorExtensions.x-rust-has-borrowed-data}}{{/model}}{{/vendorExtensions.x-rust-has-borrowed-data}}{{/isModel}}{{!
160+
### Array other datatypes
161+
}}{{^isModel}}{{{dataType}}}{{/isModel}}{{/items}}>{{/isArray}}{{!
162+
### Primitive datatypes
163+
}}{{^isArray}}{{#isPrimitiveType}}{{{dataType}}}{{/isPrimitiveType}}{{!
164+
### Any other Non-Primitive datatypes
165+
}}{{^isPrimitiveType}}{{{dataType}}}{{!
166+
### Closing tags
167+
}}{{/isPrimitiveType}}{{/isArray}}{{/isString}}{{/isByteArray}}{{/isModel}}{{/isEnum}}{{!
156168
### Option End (and trailing comma)
157169
}}{{#isNullable}}>{{/isNullable}}{{^required}}>{{/required}},
158170
{{/vendorExtensions.x-rust-type}}
159171
{{/vars}}
160172
}
161173

162-
impl{{{vendorExtensions.x-rust-generic-parameter}}} {{{classname}}}{{{vendorExtensions.x-rust-generic-parameter}}} {
174+
impl{{#vendorExtensions.x-rust-has-borrowed-data}}<'a{{#vendorExtensions.x-rust-generic-parameter}}, {{{.}}}{{/vendorExtensions.x-rust-generic-parameter}}>{{/vendorExtensions.x-rust-has-borrowed-data}}{{^vendorExtensions.x-rust-has-borrowed-data}}{{#vendorExtensions.x-rust-generic-parameter}}<{{{.}}}>{{/vendorExtensions.x-rust-generic-parameter}}{{/vendorExtensions.x-rust-has-borrowed-data}} {{!
175+
}}{{{classname}}}{{#vendorExtensions.x-rust-has-borrowed-data}}<'a{{#vendorExtensions.x-rust-generic-parameter}}, {{{.}}}{{/vendorExtensions.x-rust-generic-parameter}}>{{/vendorExtensions.x-rust-has-borrowed-data}}{{^vendorExtensions.x-rust-has-borrowed-data}}{{#vendorExtensions.x-rust-generic-parameter}}<{{{.}}}>{{/vendorExtensions.x-rust-generic-parameter}}{{/vendorExtensions.x-rust-has-borrowed-data}} {
163176
{{#description}}
164177
/// {{{.}}}
165178
{{/description}}
166179
pub fn new({{#requiredVars}}{{{name}}}: {{!
167180
### CHANGE 1: If x-rust-type is available for a required var, use it for the parameter type.
168181
}}{{#vendorExtensions.x-rust-type}}{{{.}}}{{/vendorExtensions.x-rust-type}}{{!
169182
### Fallback to original logic if x-rust-type is not present.
170-
}}{{^vendorExtensions.x-rust-type}}{{#isNullable}}Option<{{/isNullable}}{{#isEnum}}{{#isArray}}{{#uniqueItems}}std::collections::HashSet<{{/uniqueItems}}{{^uniqueItems}}Vec<{{/uniqueItems}}{{/isArray}}{{{enumName}}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{^isEnum}}{{#isByteArray}}Vec<u8>{{/isByteArray}}{{^isByteArray}}{{{dataType}}}{{/isByteArray}}{{/isEnum}}{{#isNullable}}>{{/isNullable}}{{/vendorExtensions.x-rust-type}}{{!
183+
}}{{^vendorExtensions.x-rust-type}}{{#isNullable}}Option<{{/isNullable}}{{!
184+
}}{{#isEnum}}{{#isArray}}{{#uniqueItems}}std::collections::HashSet<{{/uniqueItems}}{{^uniqueItems}}Vec<{{/uniqueItems}}{{/isArray}}{{{enumName}}}{{#isArray}}>{{/isArray}}{{/isEnum}}{{!
185+
}}{{^isEnum}}{{#isByteArray}}Vec<u8>{{/isByteArray}}{{!
186+
}}{{^isByteArray}}{{#isString}}{{#model}}{{#vendorExtensions.x-rust-has-borrowed-data}}Cow<'a, str>{{/vendorExtensions.x-rust-has-borrowed-data}}{{^vendorExtensions.x-rust-has-borrowed-data}}String{{/vendorExtensions.x-rust-has-borrowed-data}}{{/model}}{{/isString}}{{!
187+
}}{{^isString}}{{#isArray}}Vec<{{#items}}{{!
188+
}}{{#isModel}}{{{dataType}}}{{#vendorExtensions.x-rust-has-borrowed-data}}{{#model}}{{#vendorExtensions.x-rust-has-borrowed-data}}<'a>{{/vendorExtensions.x-rust-has-borrowed-data}}{{^vendorExtensions.x-rust-has-borrowed-data}}<'static>{{/vendorExtensions.x-rust-has-borrowed-data}}{{/model}}{{/vendorExtensions.x-rust-has-borrowed-data}}{{/isModel}}{{!
189+
}}{{^isModel}}{{{dataType}}}{{/isModel}}{{/items}}>{{/isArray}}{{!
190+
}}{{^isArray}}{{{dataType}}}{{#isModel}}{{#vendorExtensions.x-rust-has-borrowed-data}}{{#model}}{{#vendorExtensions.x-rust-has-borrowed-data}}<'a>{{/vendorExtensions.x-rust-has-borrowed-data}}{{^vendorExtensions.x-rust-has-borrowed-data}}<'static>{{/vendorExtensions.x-rust-has-borrowed-data}}{{/model}}{{/vendorExtensions.x-rust-has-borrowed-data}}{{/isModel}}{{!
191+
}}{{/isArray}}{{/isString}}{{/isByteArray}}{{/isEnum}}{{!
192+
}}{{#isNullable}}>{{/isNullable}}{{/vendorExtensions.x-rust-type}}{{!
171193
### Comma for next arguement
172-
}}{{^-last}}, {{/-last}}{{/requiredVars}}) -> {{{classname}}}{{{vendorExtensions.x-rust-generic-parameter}}}{
173-
{{{classname}}} {
194+
}}{{^-last}}, {{/-last}}{{/requiredVars}}) -> Self {
195+
Self {
174196
{{#vars}}
175197
{{#vendorExtensions.x-rust-type}}
176198
{{! Differentiate between required and optional fields with x-rust-type.}}
@@ -196,16 +218,16 @@ impl{{{vendorExtensions.x-rust-generic-parameter}}} {{{classname}}}{{{vendorExte
196218
{{/description}}
197219
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
198220
#[serde(untagged)]
199-
pub enum {{classname}} {
221+
pub enum {{classname}}{{#vendorExtensions.x-rust-has-borrowed-data}}<'a>{{/vendorExtensions.x-rust-has-borrowed-data}} {
200222
{{#composedSchemas.oneOf}}
201223
{{#description}}
202224
/// {{{.}}}
203225
{{/description}}
204-
{{{name}}}({{#isModel}}{{^avoidBoxedModels}}Box<{{/avoidBoxedModels}}{{/isModel}}{{{dataType}}}{{#isModel}}{{^avoidBoxedModels}}>{{/avoidBoxedModels}}{{/isModel}}),
226+
{{{name}}}({{#isModel}}{{^avoidBoxedModels}}Box<{{/avoidBoxedModels}}{{/isModel}}{{#isArray}}Vec<{{{items.dataType}}}{{#vendorExtensions.x-rust-has-borrowed-data}}<'a>{{/vendorExtensions.x-rust-has-borrowed-data}}>{{/isArray}}{{^isArray}}{{{dataType}}}{{#vendorExtensions.x-rust-has-borrowed-data}}<'a>{{/vendorExtensions.x-rust-has-borrowed-data}}{{/isArray}}{{#isModel}}{{^avoidBoxedModels}}>{{/avoidBoxedModels}}{{/isModel}}),
205227
{{/composedSchemas.oneOf}}
206228
}
207229

208-
impl Default for {{classname}} {
230+
impl Default for {{classname}}{{#vendorExtensions.x-rust-has-borrowed-data}}<'_>{{/vendorExtensions.x-rust-has-borrowed-data}} {
209231
fn default() -> Self {
210232
{{#composedSchemas.oneOf}}{{#-first}}Self::{{{name}}}(Default::default()){{/-first}}{{/composedSchemas.oneOf}}
211233
}

openapi-generator-template/reqwest/api.mustache

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{{>partial_header}}
22

3+
use super::{Error, configuration, ContentType};
4+
use crate::{apis::ResponseContent, models};
5+
use ::std::borrow::Cow;
36
use reqwest;
47
use serde::{Deserialize, Serialize, de::Error as _};
5-
use crate::{apis::ResponseContent, models};
6-
use super::{Error, configuration, ContentType};
78

89
{{#operations}}
910
{{#operation}}
@@ -12,15 +13,15 @@ use super::{Error, configuration, ContentType};
1213
{{#-first}}
1314
/// struct for passing parameters to the method [`{{operationId}}`]
1415
#[derive(Clone, Debug)]
15-
pub struct {{{operationIdCamelCase}}}Params{{!
16+
pub struct {{{operationIdCamelCase}}}Params<'p{{!
1617
Iterate through ALL parameters in the operation.
1718
Only the requestBody has this extension defined, so it will print "<B>".
1819
The other parameters have nothing, so they will print nothing.
1920
This effectively extract the generic parameter from the requestBody
2021
and places it on the struct definition line.
2122
}}{{#allParams}}
22-
{{{vendorExtensions.x-rust-params-generic-parameter}}}
23-
{{/allParams}} {
23+
{{#vendorExtensions.x-rust-params-generic-parameter}}, {{{.}}}{{/vendorExtensions.x-rust-params-generic-parameter}}
24+
{{/allParams}}> {
2425
{{/-first}}
2526
{{#description}}
2627
/// {{{.}}}
@@ -33,15 +34,15 @@ pub struct {{{operationIdCamelCase}}}Params{{!
3334
### Option Start
3435
}}{{^required}}Option<{{/required}}{{#required}}{{#isNullable}}Option<{{/isNullable}}{{/required}}{{!
3536
### &str and Vec<&str>
36-
}}{{^isUuid}}{{#isString}}{{#isArray}}Vec<{{/isArray}}String{{#isArray}}>{{/isArray}}{{/isString}}{{/isUuid}}{{!
37+
}}{{^isUuid}}{{#isString}}{{#isArray}}Vec<{{/isArray}}Cow<'p, str>{{#isArray}}>{{/isArray}}{{/isString}}{{/isUuid}}{{!
3738
### UUIDs
3839
}}{{#isUuid}}{{#isArray}}Vec<{{/isArray}}String{{#isArray}}>{{/isArray}}{{/isUuid}}{{!
3940
### Models and primative types
40-
}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{/isUuid}}{{/isString}}{{!
41+
}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}models::{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{#isModel}}<'p>{{/isModel}}{{/isUuid}}{{/isString}}{{!
4142
### Option End
4243
}}{{^required}}>{{/required}}{{#required}}{{#isNullable}}>{{/isNullable}}{{/required}}{{!
4344
### Comma for next arguement
44-
}}{{^-last}},{{/-last}}
45+
}},
4546
{{/vendorExtensions.x-rust-type}}
4647
{{#-last}}
4748
}
@@ -105,11 +106,17 @@ pub enum {{{operationIdCamelCase}}}Error {
105106
/// {{{.}}}
106107
{{/notes}}
107108
{{#vendorExtensions.x-group-parameters}}
108-
pub {{#supportAsync}}async {{/supportAsync}}fn {{{operationId}}}{{{vendorExtensions.x-rust-generic-parameter}}}(configuration: &configuration::Configuration{{#allParams}}{{#-first}}, {{!
109+
pub {{#supportAsync}}async {{/supportAsync}}fn {{{operationId}}}{{#vendorExtensions.x-rust-generic-parameter}}<{{{.}}}>{{/vendorExtensions.x-rust-generic-parameter}}(configuration: &configuration::Configuration{{#allParams}}{{#-first}}, {{!
109110
### Params
110-
}}params: &{{{operationIdCamelCase}}}Params{{#allParams}}{{{vendorExtensions.x-rust-params-generic-parameter}}}{{/allParams}}{{/-first}}{{/allParams}}{{!
111+
}}params: &{{{operationIdCamelCase}}}Params<'_{{#allParams}}{{#vendorExtensions.x-rust-params-generic-parameter}}, {{{.}}}{{/vendorExtensions.x-rust-params-generic-parameter}}{{/allParams}}>{{/-first}}{{/allParams}}{{!
111112
### Function return type
112-
}}) -> Result<{{#vendorExtensions.x-rust-return-type}}{{{.}}}{{/vendorExtensions.x-rust-return-type}}{{^vendorExtensions.x-rust-return-type}}{{#isResponseFile}}{{#supportAsync}}reqwest::Response{{/supportAsync}}{{^supportAsync}}reqwest::blocking::Response{{/supportAsync}}{{/isResponseFile}}{{^isResponseFile}}{{#supportMultipleResponses}}ResponseContent<{{{operationIdCamelCase}}}Success>{{/supportMultipleResponses}}{{^supportMultipleResponses}}{{^returnType}}(){{/returnType}}{{{returnType}}}{{/supportMultipleResponses}}{{/isResponseFile}}{{/vendorExtensions.x-rust-return-type}}, Error<{{{operationIdCamelCase}}}Error>> {
113+
}}) -> Result<{{#vendorExtensions.x-rust-return-type}}{{{.}}}{{/vendorExtensions.x-rust-return-type}}{{^vendorExtensions.x-rust-return-type}}{{!
114+
}}{{#isResponseFile}}{{#supportAsync}}reqwest::Response{{/supportAsync}}{{^supportAsync}}reqwest::blocking::Response{{/supportAsync}}{{/isResponseFile}}{{!
115+
}}{{^isResponseFile}}{{#supportMultipleResponses}}ResponseContent<{{{operationIdCamelCase}}}Success>{{/supportMultipleResponses}}{{^supportMultipleResponses}}{{!
116+
}}{{^returnType}}(){{/returnType}}{{!
117+
}}{{#isArray}}Vec<{{#returnProperty.items}}{{{dataType}}}{{#isModel}}{{#vendorExtensions.x-rust-has-borrowed-data}}<'static>{{/vendorExtensions.x-rust-has-borrowed-data}}{{/isModel}}{{/returnProperty.items}}>{{/isArray}}{{!
118+
}}{{^isArray}}{{#returnProperty}}{{{dataType}}}{{#isModel}}{{#vendorExtensions.x-rust-has-borrowed-data}}<'static>{{/vendorExtensions.x-rust-has-borrowed-data}}{{/isModel}}{{/returnProperty}}{{!
119+
}}{{/isArray}}{{/supportMultipleResponses}}{{/isResponseFile}}{{/vendorExtensions.x-rust-return-type}}, Error<{{{operationIdCamelCase}}}Error>> {
113120
{{/vendorExtensions.x-group-parameters}}
114121
let uri_str = format!("{}{{{path}}}", configuration.base_path{{#pathParams}}, {{{baseName}}}={{#isString}}crate::apis::urlencode(&{{/isString}}{{{vendorExtensions.x-rust-param-identifier}}}{{^required}}.unwrap(){{/required}}{{#required}}{{#isNullable}}.unwrap(){{/isNullable}}{{/required}}{{#isArray}}.join(",").as_ref(){{/isArray}}{{^isString}}{{^isUuid}}{{^isPrimitiveType}}{{^isContainer}}.to_string(){{/isContainer}}{{/isPrimitiveType}}{{/isUuid}}{{/isString}}{{#isString}}){{/isString}}{{/pathParams}});
115122
let mut req_builder = configuration.client.request(reqwest::Method::{{{httpMethod}}}, &uri_str);
@@ -416,7 +423,7 @@ pub {{#supportAsync}}async {{/supportAsync}}fn {{{operationId}}}{{{vendorExtensi
416423
{{#vendorExtensions.x-rust-body-is-raw-text}}
417424
{{! If the flag is true, we generate only the raw text body logic }}
418425
{{#bodyParams}}
419-
req_builder = req_builder.header(reqwest::header::CONTENT_TYPE, "text/plain").body({{{vendorExtensions.x-rust-param-identifier}}}.to_owned());
426+
req_builder = req_builder.header(reqwest::header::CONTENT_TYPE, "text/plain").body({{{vendorExtensions.x-rust-param-identifier}}}.clone().into_owned());
420427
{{/bodyParams}}
421428
{{/vendorExtensions.x-rust-body-is-raw-text}}
422429
{{! Now, handle the case where the extension is NOT present. This is the "else" block }}

openapi-generator-template/reqwest/api_mod.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{error, fmt};
1+
use ::std::{error, fmt};
22

33
{{#withAWSV4Signature}}
44
use aws_sigv4;

0 commit comments

Comments
 (0)