Skip to content

Commit a7af767

Browse files
[Rust] Replace more HeaderValue::from_str with compile time HeaderValue::from_static (#21689)
1 parent de3e227 commit a7af767

File tree

53 files changed

+511
-1169
lines changed

Some content is hidden

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

53 files changed

+511
-1169
lines changed

modules/openapi-generator/src/main/resources/rust-axum/server-operation.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ let result = api_impl.as_ref().{{#vendorExtensions}}{{{x-operation-id}}}{{/vendo
356356
let mut response_headers = response.headers_mut().unwrap();
357357
response_headers.insert(
358358
CONTENT_TYPE,
359-
HeaderValue::from_str("{{{x-mime-type}}}").map_err(|e| { error!(error = ?e); StatusCode::INTERNAL_SERVER_ERROR })?);
359+
HeaderValue::from_static("{{{x-mime-type}}}"));
360360
}
361361

362362
{{/vendorExtensions}}

modules/openapi-generator/src/main/resources/rust-server-deprecated/client-request-body-instance.mustache

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@
6464
*request.body_mut() = Body::from(body.into_bytes());
6565

6666
let header = "{{#consumes}}{{#-first}}{{{mediaType}}}{{/-first}}{{/consumes}}{{^consumes}}application/json{{/consumes}}";
67-
request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) {
68-
Ok(h) => h,
69-
Err(e) => return Err(ApiError(format!("Unable to create header: {header} - {e}")))
70-
});
67+
request.headers_mut().insert(CONTENT_TYPE, HeaderValue::from_static(header));
7168
{{/-last}}
7269
{{/formParams}}
7370
{{/x-consumes-form}}
@@ -101,10 +98,7 @@
10198
{{/required}}
10299

103100
let header = "{{#consumes}}{{#-first}}{{{mediaType}}}{{/-first}}{{/consumes}}{{^consumes}}application/json{{/consumes}}";
104-
request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) {
105-
Ok(h) => h,
106-
Err(e) => return Err(ApiError(format!("Unable to create header: {header} - {e}")))
107-
});
101+
request.headers_mut().insert(CONTENT_TYPE, HeaderValue::from_static(header));
108102
{{/bodyParam}}
109103
{{/x-consumes-basic}}
110104
{{/vendorExtensions}}

modules/openapi-generator/src/main/resources/rust-server-deprecated/server-response-body-instance.mustache

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
{{^x-produces-multipart-related}}
44
response.headers_mut().insert(
55
CONTENT_TYPE,
6-
HeaderValue::from_str("{{{x-mime-type}}}")
7-
.expect("Unable to create Content-Type header for {{{x-mime-type}}}"));
6+
HeaderValue::from_static("{{{x-mime-type}}}"));
87
{{/x-produces-multipart-related}}
98
{{#x-produces-xml}}
109
// XML Body

modules/openapi-generator/src/main/resources/rust-server/client-request-body-instance.mustache

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@
6666
*request.body_mut() = body_from_string(body);
6767

6868
let header = "{{#consumes}}{{#-first}}{{{mediaType}}}{{/-first}}{{/consumes}}{{^consumes}}application/json{{/consumes}}";
69-
request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) {
70-
Ok(h) => h,
71-
Err(e) => return Err(ApiError(format!("Unable to create header: {header} - {e}")))
72-
});
69+
request.headers_mut().insert(CONTENT_TYPE, HeaderValue::from_static(header));
7370
{{/-last}}
7471
{{/formParams}}
7572
{{/x-consumes-form}}
@@ -103,10 +100,7 @@
103100
{{/required}}
104101

105102
let header = "{{#consumes}}{{#-first}}{{{mediaType}}}{{/-first}}{{/consumes}}{{^consumes}}application/json{{/consumes}}";
106-
request.headers_mut().insert(CONTENT_TYPE, match HeaderValue::from_str(header) {
107-
Ok(h) => h,
108-
Err(e) => return Err(ApiError(format!("Unable to create header: {header} - {e}")))
109-
});
103+
request.headers_mut().insert(CONTENT_TYPE, HeaderValue::from_static(header));
110104
{{/bodyParam}}
111105
{{/x-consumes-basic}}
112106
{{/vendorExtensions}}

modules/openapi-generator/src/main/resources/rust-server/server-response-body-instance.mustache

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
{{^x-produces-multipart-related}}
44
response.headers_mut().insert(
55
CONTENT_TYPE,
6-
HeaderValue::from_str("{{{x-mime-type}}}")
7-
.expect("Unable to create Content-Type header for {{{x-mime-type}}}"));
6+
HeaderValue::from_static("{{{x-mime-type}}}"));
87
{{/x-produces-multipart-related}}
98
{{#x-produces-xml}}
109
// XML Body

samples/server/petstore/rust-axum/output/apikey-authorization/src/models.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl std::str::FromStr for Amount {
135135
None => {
136136
return std::result::Result::Err(
137137
"Missing value while parsing Amount".to_string(),
138-
);
138+
)
139139
}
140140
};
141141

@@ -153,7 +153,7 @@ impl std::str::FromStr for Amount {
153153
_ => {
154154
return std::result::Result::Err(
155155
"Unexpected key while parsing Amount".to_string(),
156-
);
156+
)
157157
}
158158
}
159159
}
@@ -288,7 +288,7 @@ impl std::str::FromStr for CheckoutError {
288288
None => {
289289
return std::result::Result::Err(
290290
"Missing value while parsing CheckoutError".to_string(),
291-
);
291+
)
292292
}
293293
};
294294

@@ -306,7 +306,7 @@ impl std::str::FromStr for CheckoutError {
306306
_ => {
307307
return std::result::Result::Err(
308308
"Unexpected key while parsing CheckoutError".to_string(),
309-
);
309+
)
310310
}
311311
}
312312
}
@@ -474,7 +474,7 @@ impl std::str::FromStr for Payment {
474474
None => {
475475
return std::result::Result::Err(
476476
"Missing value while parsing Payment".to_string(),
477-
);
477+
)
478478
}
479479
};
480480

@@ -506,7 +506,7 @@ impl std::str::FromStr for Payment {
506506
_ => {
507507
return std::result::Result::Err(
508508
"Unexpected key while parsing Payment".to_string(),
509-
);
509+
)
510510
}
511511
}
512512
}
@@ -656,7 +656,7 @@ impl std::str::FromStr for PaymentMethod {
656656
None => {
657657
return std::result::Result::Err(
658658
"Missing value while parsing PaymentMethod".to_string(),
659-
);
659+
)
660660
}
661661
};
662662

@@ -674,7 +674,7 @@ impl std::str::FromStr for PaymentMethod {
674674
_ => {
675675
return std::result::Result::Err(
676676
"Unexpected key while parsing PaymentMethod".to_string(),
677-
);
677+
)
678678
}
679679
}
680680
}
@@ -805,7 +805,7 @@ impl std::str::FromStr for PaymentResult {
805805
None => {
806806
return std::result::Result::Err(
807807
"Missing value while parsing PaymentResult".to_string(),
808-
);
808+
)
809809
}
810810
};
811811

@@ -823,7 +823,7 @@ impl std::str::FromStr for PaymentResult {
823823
_ => {
824824
return std::result::Result::Err(
825825
"Unexpected key while parsing PaymentResult".to_string(),
826-
);
826+
)
827827
}
828828
}
829829
}

samples/server/petstore/rust-axum/output/apikey-authorization/src/server/mod.rs

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::HashMap;
33
use axum::{body::Body, extract::*, response::Response, routing::*};
44
use axum_extra::extract::{CookieJar, Host, Query as QueryExtra};
55
use bytes::Bytes;
6-
use http::{HeaderMap, HeaderName, HeaderValue, Method, StatusCode, header::CONTENT_TYPE};
6+
use http::{header::CONTENT_TYPE, HeaderMap, HeaderName, HeaderValue, Method, StatusCode};
77
use tracing::error;
88
use validator::{Validate, ValidationErrors};
99

@@ -127,13 +127,8 @@ where
127127
let mut response = response.status(200);
128128
{
129129
let mut response_headers = response.headers_mut().unwrap();
130-
response_headers.insert(
131-
CONTENT_TYPE,
132-
HeaderValue::from_str("application/json").map_err(|e| {
133-
error!(error = ?e);
134-
StatusCode::INTERNAL_SERVER_ERROR
135-
})?,
136-
);
130+
response_headers
131+
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
137132
}
138133

139134
let body_content = tokio::task::spawn_blocking(move || {
@@ -150,13 +145,8 @@ where
150145
let mut response = response.status(422);
151146
{
152147
let mut response_headers = response.headers_mut().unwrap();
153-
response_headers.insert(
154-
CONTENT_TYPE,
155-
HeaderValue::from_str("application/json").map_err(|e| {
156-
error!(error = ?e);
157-
StatusCode::INTERNAL_SERVER_ERROR
158-
})?,
159-
);
148+
response_headers
149+
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
160150
}
161151

162152
let body_content = tokio::task::spawn_blocking(move || {
@@ -290,13 +280,8 @@ where
290280
let mut response = response.status(200);
291281
{
292282
let mut response_headers = response.headers_mut().unwrap();
293-
response_headers.insert(
294-
CONTENT_TYPE,
295-
HeaderValue::from_str("application/json").map_err(|e| {
296-
error!(error = ?e);
297-
StatusCode::INTERNAL_SERVER_ERROR
298-
})?,
299-
);
283+
response_headers
284+
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
300285
}
301286

302287
let body_content = tokio::task::spawn_blocking(move || {
@@ -450,13 +435,8 @@ where
450435
let mut response = response.status(200);
451436
{
452437
let mut response_headers = response.headers_mut().unwrap();
453-
response_headers.insert(
454-
CONTENT_TYPE,
455-
HeaderValue::from_str("application/json").map_err(|e| {
456-
error!(error = ?e);
457-
StatusCode::INTERNAL_SERVER_ERROR
458-
})?,
459-
);
438+
response_headers
439+
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
460440
}
461441

462442
let body_content = tokio::task::spawn_blocking(move || {
@@ -473,13 +453,8 @@ where
473453
let mut response = response.status(422);
474454
{
475455
let mut response_headers = response.headers_mut().unwrap();
476-
response_headers.insert(
477-
CONTENT_TYPE,
478-
HeaderValue::from_str("application/json").map_err(|e| {
479-
error!(error = ?e);
480-
StatusCode::INTERNAL_SERVER_ERROR
481-
})?,
482-
);
456+
response_headers
457+
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
483458
}
484459

485460
let body_content = tokio::task::spawn_blocking(move || {

samples/server/petstore/rust-axum/output/apikey-authorization/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{mem, str::FromStr};
22

3-
use base64::{Engine, engine::general_purpose};
3+
use base64::{engine::general_purpose, Engine};
44
use serde::{Deserialize, Deserializer, Serialize, Serializer};
55

66
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]

samples/server/petstore/rust-axum/output/apikey-auths/src/models.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl std::str::FromStr for Amount {
135135
None => {
136136
return std::result::Result::Err(
137137
"Missing value while parsing Amount".to_string(),
138-
);
138+
)
139139
}
140140
};
141141

@@ -153,7 +153,7 @@ impl std::str::FromStr for Amount {
153153
_ => {
154154
return std::result::Result::Err(
155155
"Unexpected key while parsing Amount".to_string(),
156-
);
156+
)
157157
}
158158
}
159159
}
@@ -288,7 +288,7 @@ impl std::str::FromStr for CheckoutError {
288288
None => {
289289
return std::result::Result::Err(
290290
"Missing value while parsing CheckoutError".to_string(),
291-
);
291+
)
292292
}
293293
};
294294

@@ -306,7 +306,7 @@ impl std::str::FromStr for CheckoutError {
306306
_ => {
307307
return std::result::Result::Err(
308308
"Unexpected key while parsing CheckoutError".to_string(),
309-
);
309+
)
310310
}
311311
}
312312
}
@@ -474,7 +474,7 @@ impl std::str::FromStr for Payment {
474474
None => {
475475
return std::result::Result::Err(
476476
"Missing value while parsing Payment".to_string(),
477-
);
477+
)
478478
}
479479
};
480480

@@ -506,7 +506,7 @@ impl std::str::FromStr for Payment {
506506
_ => {
507507
return std::result::Result::Err(
508508
"Unexpected key while parsing Payment".to_string(),
509-
);
509+
)
510510
}
511511
}
512512
}
@@ -656,7 +656,7 @@ impl std::str::FromStr for PaymentMethod {
656656
None => {
657657
return std::result::Result::Err(
658658
"Missing value while parsing PaymentMethod".to_string(),
659-
);
659+
)
660660
}
661661
};
662662

@@ -674,7 +674,7 @@ impl std::str::FromStr for PaymentMethod {
674674
_ => {
675675
return std::result::Result::Err(
676676
"Unexpected key while parsing PaymentMethod".to_string(),
677-
);
677+
)
678678
}
679679
}
680680
}
@@ -805,7 +805,7 @@ impl std::str::FromStr for PaymentResult {
805805
None => {
806806
return std::result::Result::Err(
807807
"Missing value while parsing PaymentResult".to_string(),
808-
);
808+
)
809809
}
810810
};
811811

@@ -823,7 +823,7 @@ impl std::str::FromStr for PaymentResult {
823823
_ => {
824824
return std::result::Result::Err(
825825
"Unexpected key while parsing PaymentResult".to_string(),
826-
);
826+
)
827827
}
828828
}
829829
}

0 commit comments

Comments
 (0)