Skip to content

Commit de091e6

Browse files
feat(rust): add code formatting with rustfmt (#9315)
* feat(rust): enable `rustfmt` * feat(rust): move rust formatter to base * fix(rust): lint:biome * chore(rust): update seed * chore(rust): update test * fix: biome * chore(rust): bumb version 0.0.7 -> 0.0.8 * chore(rust): update
1 parent 2d2fc07 commit de091e6

File tree

1,507 files changed

+22489
-17294
lines changed

Some content is hidden

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

1,507 files changed

+22489
-17294
lines changed

generators/rust/base/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@fern-api/base-generator": "workspace:*",
3535
"@fern-api/core-utils": "workspace:*",
3636
"@fern-api/fs-utils": "workspace:*",
37+
"@fern-api/logger": "workspace:*",
3738
"@fern-fern/ir-sdk": "58.2.0",
3839
"zod": "^3.22.4"
3940
},
Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use crate::client::CLIENT_NAME;
2+
use crate::{ApiError, ClientConfig};
13
use std::collections::HashMap;
24
use std::time::Duration;
3-
use crate::{ClientConfig, ApiError};
4-
use crate::client::{{CLIENT_NAME}};
55

66
/// Builder for creating API clients with custom configuration
77
pub struct ApiClientBuilder {
@@ -15,64 +15,78 @@ impl ApiClientBuilder {
1515
config.base_url = base_url.into();
1616
Self { config }
1717
}
18-
18+
1919
/// Set the API key for authentication
2020
pub fn api_key(mut self, key: impl Into<String>) -> Self {
2121
self.config.api_key = Some(key.into());
2222
self
2323
}
24-
24+
2525
/// Set the bearer token for authentication
2626
pub fn bearer_token(mut self, token: impl Into<String>) -> Self {
2727
self.config.bearer_token = Some(token.into());
2828
self
2929
}
30-
30+
3131
/// Set the username for basic authentication
3232
pub fn username(mut self, username: impl Into<String>) -> Self {
3333
self.config.username = Some(username.into());
3434
self
3535
}
36-
36+
3737
/// Set the password for basic authentication
3838
pub fn password(mut self, password: impl Into<String>) -> Self {
3939
self.config.password = Some(password.into());
4040
self
4141
}
42-
42+
4343
/// Set the request timeout
4444
pub fn timeout(mut self, timeout: Duration) -> Self {
4545
self.config.timeout = timeout;
4646
self
4747
}
48-
48+
4949
/// Set the maximum number of retries
5050
pub fn max_retries(mut self, retries: u32) -> Self {
5151
self.config.max_retries = retries;
5252
self
5353
}
54-
54+
5555
/// Add a custom header
5656
pub fn custom_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
5757
self.config.custom_headers.insert(key.into(), value.into());
5858
self
5959
}
60-
60+
6161
/// Add multiple custom headers
6262
pub fn custom_headers(mut self, headers: HashMap<String, String>) -> Self {
6363
self.config.custom_headers.extend(headers);
6464
self
6565
}
66-
66+
6767
/// Set the user agent
6868
pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
6969
self.config.user_agent = user_agent.into();
7070
self
7171
}
72-
72+
7373
/// Build the client with validation
74-
pub fn build(self) -> Result<{{CLIENT_NAME}}, ApiError> {
74+
pub fn build(
75+
self,
76+
) -> Result<
77+
{
78+
{
79+
CLIENT_NAME
80+
}
81+
},
82+
ApiError,
83+
> {
7584
// Call the client constructor with all authentication parameters
76-
{{CLIENT_NAME}}::new(self.config)
85+
{
86+
{
87+
CLIENT_NAME
88+
}
89+
}
90+
::new(self.config)
7791
}
78-
}
92+
}
Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
use std::str::FromStr;
2-
use reqwest::{Client, Request, Response, Method, header::{HeaderName, HeaderValue}};
1+
use crate::{ApiError, ClientConfig, RequestOptions};
2+
use reqwest::{
3+
header::{HeaderName, HeaderValue},
4+
Client, Method, Request, Response,
5+
};
36
use serde::de::DeserializeOwned;
4-
use crate::{ClientConfig, RequestOptions, ApiError};
7+
use std::str::FromStr;
58

69
/// Internal HTTP client that handles requests with authentication and retries
710
#[derive(Clone)]
@@ -17,10 +20,10 @@ impl HttpClient {
1720
.user_agent(&config.user_agent)
1821
.build()
1922
.map_err(ApiError::Network)?;
20-
23+
2124
Ok(Self { client, config })
2225
}
23-
26+
2427
/// Execute a request with the given method, path, and options
2528
pub async fn execute_request<T>(
2629
&self,
@@ -33,101 +36,119 @@ impl HttpClient {
3336
where
3437
T: DeserializeOwned,
3538
{
36-
let url = format!("{}/{}",
37-
self.config.base_url.trim_end_matches('/'),
39+
let url = format!(
40+
"{}/{}",
41+
self.config.base_url.trim_end_matches('/'),
3842
path.trim_start_matches('/')
3943
);
4044
let mut request = self.client.request(method, &url);
41-
45+
4246
// Apply query parameters if provided
4347
if let Some(params) = query_params {
4448
request = request.query(&params);
4549
}
46-
50+
4751
// Apply body if provided
4852
if let Some(body) = body {
4953
request = request.json(&body);
5054
}
51-
55+
5256
// Build the request
5357
let mut req = request.build().map_err(|e| ApiError::Network(e))?;
54-
58+
5559
// Apply authentication and headers
5660
self.apply_auth_headers(&mut req, &options)?;
5761
self.apply_custom_headers(&mut req, &options)?;
58-
62+
5963
// Execute with retries
6064
let response = self.execute_with_retries(req, &options).await?;
6165
self.parse_response(response).await
6266
}
63-
64-
fn apply_auth_headers(&self, request: &mut Request, options: &Option<RequestOptions>) -> Result<(), ApiError> {
67+
68+
fn apply_auth_headers(
69+
&self,
70+
request: &mut Request,
71+
options: &Option<RequestOptions>,
72+
) -> Result<(), ApiError> {
6573
let headers = request.headers_mut();
66-
74+
6775
// Apply API key (request options override config)
68-
let api_key = options.as_ref()
76+
let api_key = options
77+
.as_ref()
6978
.and_then(|opts| opts.api_key.as_ref())
7079
.or(self.config.api_key.as_ref());
71-
80+
7281
if let Some(key) = api_key {
7382
headers.insert("api_key", key.parse().map_err(|_| ApiError::InvalidHeader)?);
7483
}
75-
84+
7685
// Apply bearer token (request options override config)
77-
let bearer_token = options.as_ref()
86+
let bearer_token = options
87+
.as_ref()
7888
.and_then(|opts| opts.bearer_token.as_ref())
7989
.or(self.config.bearer_token.as_ref());
80-
90+
8191
if let Some(token) = bearer_token {
8292
let auth_value = format!("Bearer {}", token);
83-
headers.insert("Authorization", auth_value.parse().map_err(|_| ApiError::InvalidHeader)?);
93+
headers.insert(
94+
"Authorization",
95+
auth_value.parse().map_err(|_| ApiError::InvalidHeader)?,
96+
);
8497
}
85-
98+
8699
Ok(())
87100
}
88-
89-
fn apply_custom_headers(&self, request: &mut Request, options: &Option<RequestOptions>) -> Result<(), ApiError> {
101+
102+
fn apply_custom_headers(
103+
&self,
104+
request: &mut Request,
105+
options: &Option<RequestOptions>,
106+
) -> Result<(), ApiError> {
90107
let headers = request.headers_mut();
91-
108+
92109
// Apply config-level custom headers
93110
for (key, value) in &self.config.custom_headers {
94111
headers.insert(
95-
HeaderName::from_str(key).map_err(|_| ApiError::InvalidHeader)?,
96-
HeaderValue::from_str(value).map_err(|_| ApiError::InvalidHeader)?
112+
HeaderName::from_str(key).map_err(|_| ApiError::InvalidHeader)?,
113+
HeaderValue::from_str(value).map_err(|_| ApiError::InvalidHeader)?,
97114
);
98115
}
99-
116+
100117
// Apply request-level custom headers (override config)
101118
if let Some(options) = options {
102119
for (key, value) in &options.additional_headers {
103120
headers.insert(
104-
HeaderName::from_str(key).map_err(|_| ApiError::InvalidHeader)?,
105-
HeaderValue::from_str(value).map_err(|_| ApiError::InvalidHeader)?
121+
HeaderName::from_str(key).map_err(|_| ApiError::InvalidHeader)?,
122+
HeaderValue::from_str(value).map_err(|_| ApiError::InvalidHeader)?,
106123
);
107124
}
108125
}
109-
126+
110127
Ok(())
111128
}
112-
113-
async fn execute_with_retries(&self, request: Request, options: &Option<RequestOptions>) -> Result<Response, ApiError> {
114-
let max_retries = options.as_ref()
129+
130+
async fn execute_with_retries(
131+
&self,
132+
request: Request,
133+
options: &Option<RequestOptions>,
134+
) -> Result<Response, ApiError> {
135+
let max_retries = options
136+
.as_ref()
115137
.and_then(|opts| opts.max_retries)
116138
.unwrap_or(self.config.max_retries);
117-
139+
118140
let mut last_error = None;
119-
141+
120142
for attempt in 0..=max_retries {
121-
let cloned_request = request.try_clone()
122-
.ok_or(ApiError::RequestClone)?;
123-
143+
let cloned_request = request.try_clone().ok_or(ApiError::RequestClone)?;
144+
124145
match self.client.execute(cloned_request).await {
125146
Ok(response) if response.status().is_success() => return Ok(response),
126147
Ok(response) => {
127148
let status_code = response.status().as_u16();
128149
let body = response.text().await.ok();
129150
return Err(ApiError::from_response(status_code, body.as_deref()));
130-
},
151+
}
131152
Err(e) if attempt < max_retries => {
132153
last_error = Some(e);
133154
// Exponential backoff
@@ -137,15 +158,15 @@ impl HttpClient {
137158
Err(e) => return Err(ApiError::Network(e)),
138159
}
139160
}
140-
161+
141162
Err(ApiError::Network(last_error.unwrap()))
142163
}
143-
164+
144165
async fn parse_response<T>(&self, response: Response) -> Result<T, ApiError>
145166
where
146167
T: DeserializeOwned,
147168
{
148169
let text = response.text().await.map_err(ApiError::Network)?;
149170
serde_json::from_str(&text).map_err(ApiError::Serialization)
150171
}
151-
}
172+
}

0 commit comments

Comments
 (0)