Skip to content

Commit c2fb9d0

Browse files
committed
Reformat code for improved readability and consistency
This commit applies consistent code formatting across several files, including breaking up long lines, aligning parameters, and improving indentation. No functional changes were made; these updates enhance code readability and maintainability.
1 parent 2df8a62 commit c2fb9d0

8 files changed

Lines changed: 87 additions & 61 deletions

File tree

crates/rustapi-core/src/app.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,11 @@ impl RustApi {
275275
self = self.route(&path, method_router);
276276
}
277277

278-
tracing::info!(paths = path_count, routes = route_count, "Auto-registered routes");
278+
tracing::info!(
279+
paths = path_count,
280+
routes = route_count,
281+
"Auto-registered routes"
282+
);
279283

280284
// Apply any auto-registered schemas.
281285
crate::auto_schema::apply_auto_schemas(&mut self.openapi_spec);

crates/rustapi-rs/tests/auto_route.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ fn test_openapi_includes_query_params() {
146146
.expect("Query params should be present");
147147

148148
assert!(
149-
params.iter().any(|p| p.location == "query" && p.name == "page"),
149+
params
150+
.iter()
151+
.any(|p| p.location == "query" && p.name == "page"),
150152
"OpenAPI should include query parameter 'page'"
151153
);
152154
assert!(

crates/rustapi-toon/src/error.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ impl From<toon_format::ToonError> for ToonError {
3535
impl From<ToonError> for ApiError {
3636
fn from(err: ToonError) -> Self {
3737
match err {
38-
ToonError::Encode(msg) => {
39-
ApiError::internal(format!("Failed to encode TOON: {}", msg))
40-
}
38+
ToonError::Encode(msg) => ApiError::internal(format!("Failed to encode TOON: {}", msg)),
4139
ToonError::Decode(msg) => ApiError::bad_request(format!("Invalid TOON: {}", msg)),
4240
ToonError::InvalidContentType => ApiError::bad_request(
4341
"Invalid content type: expected application/toon or text/toon",

crates/rustapi-toon/src/extractor.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use bytes::Bytes;
66
use http::{header, StatusCode};
77
use http_body_util::Full;
88
use rustapi_core::{ApiError, FromRequest, IntoResponse, Request, Response, Result};
9-
use rustapi_openapi::{MediaType, Operation, OperationModifier, ResponseModifier, ResponseSpec, SchemaRef};
9+
use rustapi_openapi::{
10+
MediaType, Operation, OperationModifier, ResponseModifier, ResponseSpec, SchemaRef,
11+
};
1012
use serde::de::DeserializeOwned;
1113
use serde::Serialize;
1214
use std::collections::HashMap;
@@ -90,8 +92,8 @@ impl<T: DeserializeOwned + Send> FromRequest for Toon<T> {
9092
let body_str =
9193
std::str::from_utf8(&body).map_err(|e| ApiError::bad_request(e.to_string()))?;
9294

93-
let value: T = toon_format::decode_default(body_str)
94-
.map_err(|e| ToonError::Decode(e.to_string()))?;
95+
let value: T =
96+
toon_format::decode_default(body_str).map_err(|e| ToonError::Decode(e.to_string()))?;
9597

9698
Ok(Toon(value))
9799
}

crates/rustapi-toon/src/negotiate.rs

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use bytes::Bytes;
88
use http::{header, StatusCode};
99
use http_body_util::Full;
1010
use rustapi_core::{ApiError, FromRequestParts, IntoResponse, Request, Response};
11-
use rustapi_openapi::{MediaType, Operation, OperationModifier, ResponseModifier, ResponseSpec, SchemaRef};
11+
use rustapi_openapi::{
12+
MediaType, Operation, OperationModifier, ResponseModifier, ResponseSpec, SchemaRef,
13+
};
1214
use serde::Serialize;
1315
use std::collections::HashMap;
1416

@@ -94,12 +96,19 @@ impl AcceptHeader {
9496
(part.to_string(), 1.0)
9597
};
9698

97-
Some(MediaTypeEntry { media_type, quality })
99+
Some(MediaTypeEntry {
100+
media_type,
101+
quality,
102+
})
98103
})
99104
.collect();
100105

101106
// Sort by quality (descending)
102-
entries.sort_by(|a, b| b.quality.partial_cmp(&a.quality).unwrap_or(std::cmp::Ordering::Equal));
107+
entries.sort_by(|a, b| {
108+
b.quality
109+
.partial_cmp(&a.quality)
110+
.unwrap_or(std::cmp::Ordering::Equal)
111+
});
103112

104113
// Determine preferred format
105114
let preferred = Self::determine_format(&entries);
@@ -114,23 +123,23 @@ impl AcceptHeader {
114123
fn determine_format(entries: &[MediaTypeEntry]) -> OutputFormat {
115124
for entry in entries {
116125
let mt = entry.media_type.to_lowercase();
117-
126+
118127
// Check for TOON
119128
if mt == TOON_CONTENT_TYPE || mt == TOON_CONTENT_TYPE_TEXT {
120129
return OutputFormat::Toon;
121130
}
122-
131+
123132
// Check for JSON
124133
if mt == JSON_CONTENT_TYPE || mt == "application/json" || mt == "text/json" {
125134
return OutputFormat::Json;
126135
}
127-
136+
128137
// Wildcard accepts anything, default to JSON
129138
if mt == "*/*" || mt == "application/*" || mt == "text/*" {
130139
return OutputFormat::Json;
131140
}
132141
}
133-
142+
134143
// Default to JSON
135144
OutputFormat::Json
136145
}
@@ -139,7 +148,10 @@ impl AcceptHeader {
139148
pub fn accepts_toon(&self) -> bool {
140149
self.media_types.iter().any(|e| {
141150
let mt = e.media_type.to_lowercase();
142-
mt == TOON_CONTENT_TYPE || mt == TOON_CONTENT_TYPE_TEXT || mt == "*/*" || mt == "application/*"
151+
mt == TOON_CONTENT_TYPE
152+
|| mt == TOON_CONTENT_TYPE_TEXT
153+
|| mt == "*/*"
154+
|| mt == "application/*"
143155
})
144156
}
145157

@@ -160,7 +172,7 @@ impl FromRequestParts for AcceptHeader {
160172
.and_then(|v| v.to_str().ok())
161173
.map(AcceptHeader::parse)
162174
.unwrap_or_default();
163-
175+
164176
Ok(accept)
165177
}
166178
}
@@ -235,32 +247,28 @@ impl<T> Negotiate<T> {
235247
impl<T: Serialize> IntoResponse for Negotiate<T> {
236248
fn into_response(self) -> Response {
237249
match self.format {
238-
OutputFormat::Json => {
239-
match serde_json::to_vec(&self.data) {
240-
Ok(body) => http::Response::builder()
241-
.status(StatusCode::OK)
242-
.header(header::CONTENT_TYPE, JSON_CONTENT_TYPE)
243-
.body(Full::new(Bytes::from(body)))
244-
.unwrap(),
245-
Err(err) => {
246-
let error = ApiError::internal(format!("JSON serialization error: {}", err));
247-
error.into_response()
248-
}
250+
OutputFormat::Json => match serde_json::to_vec(&self.data) {
251+
Ok(body) => http::Response::builder()
252+
.status(StatusCode::OK)
253+
.header(header::CONTENT_TYPE, JSON_CONTENT_TYPE)
254+
.body(Full::new(Bytes::from(body)))
255+
.unwrap(),
256+
Err(err) => {
257+
let error = ApiError::internal(format!("JSON serialization error: {}", err));
258+
error.into_response()
249259
}
250-
}
251-
OutputFormat::Toon => {
252-
match toon_format::encode_default(&self.data) {
253-
Ok(body) => http::Response::builder()
254-
.status(StatusCode::OK)
255-
.header(header::CONTENT_TYPE, TOON_CONTENT_TYPE)
256-
.body(Full::new(Bytes::from(body)))
257-
.unwrap(),
258-
Err(err) => {
259-
let error = ApiError::internal(format!("TOON serialization error: {}", err));
260-
error.into_response()
261-
}
260+
},
261+
OutputFormat::Toon => match toon_format::encode_default(&self.data) {
262+
Ok(body) => http::Response::builder()
263+
.status(StatusCode::OK)
264+
.header(header::CONTENT_TYPE, TOON_CONTENT_TYPE)
265+
.body(Full::new(Bytes::from(body)))
266+
.unwrap(),
267+
Err(err) => {
268+
let error = ApiError::internal(format!("TOON serialization error: {}", err));
269+
error.into_response()
262270
}
263-
}
271+
},
264272
}
265273
}
266274
}
@@ -275,7 +283,7 @@ impl<T: Send> OperationModifier for Negotiate<T> {
275283
impl<T: Serialize> ResponseModifier for Negotiate<T> {
276284
fn update_response(op: &mut Operation) {
277285
let mut content = HashMap::new();
278-
286+
279287
// JSON response
280288
content.insert(
281289
JSON_CONTENT_TYPE.to_string(),
@@ -286,7 +294,7 @@ impl<T: Serialize> ResponseModifier for Negotiate<T> {
286294
})),
287295
},
288296
);
289-
297+
290298
// TOON response
291299
content.insert(
292300
TOON_CONTENT_TYPE.to_string(),
@@ -299,7 +307,8 @@ impl<T: Serialize> ResponseModifier for Negotiate<T> {
299307
);
300308

301309
let response = ResponseSpec {
302-
description: "Content-negotiated response (JSON or TOON based on Accept header)".to_string(),
310+
description: "Content-negotiated response (JSON or TOON based on Accept header)"
311+
.to_string(),
303312
content: Some(content),
304313
};
305314
op.responses.insert("200".to_string(), response);

crates/rustapi-toon/src/openapi.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub fn token_headers_schema() -> serde_json::Value {
103103
pub fn format_comparison_example<T: serde::Serialize>(data: &T) -> serde_json::Value {
104104
let json_str = serde_json::to_string_pretty(data).unwrap_or_default();
105105
let toon_str = toon_format::encode_default(data).unwrap_or_default();
106-
106+
107107
let json_bytes = json_str.len();
108108
let toon_bytes = toon_str.len();
109109
let json_tokens = json_bytes / 4;
@@ -113,7 +113,7 @@ pub fn format_comparison_example<T: serde::Serialize>(data: &T) -> serde_json::V
113113
} else {
114114
0.0
115115
};
116-
116+
117117
serde_json::json!({
118118
"json": {
119119
"content": json_str,
@@ -151,26 +151,26 @@ pub fn api_description_with_toon(base_description: &str) -> String {
151151
mod tests {
152152
use super::*;
153153
use serde::Serialize;
154-
154+
155155
#[derive(Serialize)]
156156
struct TestUser {
157157
id: u64,
158158
name: String,
159159
}
160-
160+
161161
#[test]
162162
fn test_toon_schema() {
163163
let schema = toon_schema();
164164
assert_eq!(schema["type"], "string");
165165
assert_eq!(schema["format"], "toon");
166166
}
167-
167+
168168
#[test]
169169
fn test_toon_extension() {
170170
let ext = toon_extension();
171171
assert!(ext["x-toon-support"]["enabled"].as_bool().unwrap());
172172
}
173-
173+
174174
#[test]
175175
fn test_token_headers_schema() {
176176
let headers = token_headers_schema();
@@ -179,21 +179,30 @@ mod tests {
179179
assert!(headers["X-Token-Savings"].is_object());
180180
assert!(headers["X-Format-Used"].is_object());
181181
}
182-
182+
183183
#[test]
184184
fn test_format_comparison_example() {
185185
let users = vec![
186-
TestUser { id: 1, name: "Alice".to_string() },
187-
TestUser { id: 2, name: "Bob".to_string() },
186+
TestUser {
187+
id: 1,
188+
name: "Alice".to_string(),
189+
},
190+
TestUser {
191+
id: 2,
192+
name: "Bob".to_string(),
193+
},
188194
];
189195
let comparison = format_comparison_example(&users);
190-
196+
191197
assert!(comparison["json"]["bytes"].as_u64().unwrap() > 0);
192198
assert!(comparison["toon"]["bytes"].as_u64().unwrap() > 0);
193199
// TOON should be smaller
194-
assert!(comparison["toon"]["bytes"].as_u64().unwrap() < comparison["json"]["bytes"].as_u64().unwrap());
200+
assert!(
201+
comparison["toon"]["bytes"].as_u64().unwrap()
202+
< comparison["json"]["bytes"].as_u64().unwrap()
203+
);
195204
}
196-
205+
197206
#[test]
198207
fn test_api_description_with_toon() {
199208
let desc = api_description_with_toon("My API");

examples/hello-world/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ struct Message {
88

99
#[rustapi_rs::get("/hello/{name}")]
1010
async fn hello(Path(name): Path<String>) -> Json<Message> {
11-
Json(Message { greeting: format!("Hello, {name}!") })
11+
Json(Message {
12+
greeting: format!("Hello, {name}!"),
13+
})
1214
}
1315

1416
#[tokio::main]
1517
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
1618
RustApi::auto().run("0.0.0.0:8080").await
17-
}
19+
}

examples/proof-of-concept/src/handlers/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::models::HealthResponse;
1414
#[rustapi_rs::tag("System")]
1515
#[rustapi_rs::summary("Frontend")]
1616
async fn index() -> Html<&'static str> {
17-
Html(
18-
r#"<!doctype html>
17+
Html(
18+
r#"<!doctype html>
1919
<html lang=\"en\">
2020
<head>
2121
<meta charset=\"utf-8\" />
@@ -28,7 +28,7 @@ async fn index() -> Html<&'static str> {
2828
<p>Health: <a href=\"/health\">/health</a></p>
2929
</body>
3030
</html>"#,
31-
)
31+
)
3232
}
3333

3434
/// Health check endpoint

0 commit comments

Comments
 (0)