swadhinbiswas WIP save-point: production hardening, H3 removal, fmt gate, and accumulated phase work · 7/15/2026
2080d40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use anyhow::Result;
use bytes::Bytes;
use http_body_util::combinators::UnsyncBoxBody;
use http_body_util::BodyExt;
use hyper::header::HeaderName;
use hyper::{Response, StatusCode};
use crate::ResponseBody;
// ---------------------------------------------------------------------------
// Low-level serialization helpers
// ---------------------------------------------------------------------------
/// Serialize a value to a JSON string.
///
/// Uses `simd-json` when the `simd-json` feature is enabled,
/// falling back to `serde_json` otherwise.
#[cfg(feature = "simd-json")]
pub fn to_json_string<T: serde::Serialize>(value: &T) -> Result<String> {
Ok(simd_json::serde::to_string(value)?)
}
#[cfg(not(feature = "simd-json"))]
pub fn to_json_string<T: serde::Serialize>(value: &T) -> Result<String> {
Ok(serde_json::to_string(value)?)
}
/// Serialize a value to a JSON byte vector.
#[cfg(feature = "simd-json")]
pub fn to_json_vec<T: serde::Serialize>(value: &T) -> Result<Vec<u8>> {
Ok(simd_json::serde::to_vec(value)?)
}
#[cfg(not(feature = "simd-json"))]
pub fn to_json_vec<T: serde::Serialize>(value: &T) -> Result<Vec<u8>> {
Ok(serde_json::to_vec(value)?)
}
// ---------------------------------------------------------------------------
// JsonResponse — typed response-model shaping
// ---------------------------------------------------------------------------
/// A JSON response shaped from any `Serialize` type (FastAPI's `response_model`
/// equivalent, but zero-copy Rust + serde — no Python GIL overhead).
///
/// Use the builder methods to set status code and headers, then call
/// [`into_response`](Self::into_response) to produce a `Response<ResponseBody>`.
///
/// # Example
///
/// ```ignore
/// #[derive(Serialize)]
/// struct User { id: u64, name: String }
///
/// let resp = JsonResponse::new(User { id: 1, name: "Alice".into() })
/// .with_status(StatusCode::CREATED)
/// .with_header("x-request-id", "abc-123")
/// .into_response();
/// ```
pub struct JsonResponse<T: serde::Serialize> {
data: T,
status: StatusCode,
headers: Vec<(HeaderName, String)>,
}
impl<T: serde::Serialize> JsonResponse<T> {
/// Create a new `JsonResponse` with status 200 OK and no extra headers.
pub fn new(data: T) -> Self {
Self { data, status: StatusCode::OK, headers: Vec::new() }
}
/// Set the HTTP status code.
pub fn with_status(mut self, status: StatusCode) -> Self {
self.status = status;
self
}
/// Add a response header.
pub fn with_header(mut self, name: &str, value: &str) -> Self {
if let Ok(h) = HeaderName::from_bytes(name.as_bytes()) {
self.headers.push((h, value.to_string()));
}
self
}
/// Serialize the wrapped data and produce a `Response<ResponseBody>`.
pub fn into_response(self) -> Result<Response<ResponseBody>> {
let body_bytes = to_json_vec(&self.data)?;
let mut builder = Response::builder().status(self.status);
builder = builder.header("content-type", "application/json");
builder = builder.header("content-length", body_bytes.len().to_string());
for (name, value) in &self.headers {
builder = builder.header(name, value);
}
let resp = builder
.body(UnsyncBoxBody::new(
http_body_util::Full::new(Bytes::from(body_bytes))
.map_err(|e: std::convert::Infallible| -> anyhow::Error { match e {} }),
))
.unwrap();
Ok(resp)
}
}
/// Convenience: create a `JsonResponse` with status 200 from any Serialize value.
pub fn json_response_from<T: serde::Serialize>(data: T) -> JsonResponse<T> {
JsonResponse::new(data)
}
#[cfg(test)]
mod tests {
use super::*;
use http_body_util::BodyExt;
use serde::Serialize;
#[derive(Serialize)]
struct TestPayload {
message: String,
count: u32,
}
#[test]
fn test_to_json_string() {
let payload = TestPayload { message: "hello".into(), count: 42 };
let json = to_json_string(&payload).unwrap();
assert!(json.contains(r#""message":"hello""#));
assert!(json.contains(r#""count":42"#));
}
#[test]
fn test_to_json_vec() {
let payload = TestPayload { message: "hello".into(), count: 42 };
let vec = to_json_vec(&payload).unwrap();
let json = String::from_utf8(vec).unwrap();
assert!(json.contains(r#""message":"hello""#));
}
// --- JsonResponse tests ---
#[derive(Serialize)]
struct UserResp {
id: u64,
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
email: Option<String>,
}
#[tokio::test]
async fn json_response_default_status() {
let user = UserResp { id: 1, name: "Alice".into(), email: None };
let resp = JsonResponse::new(user).into_response().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let body = resp.into_body().collect().await.unwrap().to_bytes();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["id"], 1);
assert_eq!(json["name"], "Alice");
// email field skipped because None
assert!(json.get("email").is_none());
}
#[tokio::test]
async fn json_response_custom_status_and_headers() {
let user = UserResp { id: 2, name: "Bob".into(), email: Some("[email protected]".into()) };
let resp = JsonResponse::new(user)
.with_status(StatusCode::CREATED)
.with_header("x-request-id", "req-999")
.with_header("x-version", "2.0")
.into_response()
.unwrap();
assert_eq!(resp.status(), StatusCode::CREATED);
assert_eq!(resp.headers()["x-request-id"], "req-999");
assert_eq!(resp.headers()["x-version"], "2.0");
assert_eq!(resp.headers()["content-type"], "application/json");
let body = resp.into_body().collect().await.unwrap().to_bytes();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["email"], "[email protected]");
}
#[tokio::test]
async fn json_response_from_convenience() {
let user = UserResp { id: 3, name: "Carol".into(), email: None };
let resp = json_response_from(user).into_response().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
#[tokio::test]
async fn json_response_serde_rename() {
#[derive(Serialize)]
struct Renamed {
#[serde(rename = "userId")]
user_id: u64,
}
let resp = JsonResponse::new(Renamed { user_id: 42 }).into_response().unwrap();
let body = resp.into_body().collect().await.unwrap().to_bytes();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["userId"], 42);
assert!(json.get("user_id").is_none());
}
#[tokio::test]
async fn json_response_flatten() {
use std::collections::HashMap;
#[derive(Serialize)]
struct WithMeta {
data: String,
#[serde(flatten)]
meta: HashMap<String, serde_json::Value>,
}
let mut meta = HashMap::new();
meta.insert("page".into(), serde_json::json!(1));
let val = WithMeta { data: "content".into(), meta };
let resp = JsonResponse::new(val).into_response().unwrap();
let body = resp.into_body().collect().await.unwrap().to_bytes();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["data"], "content");
assert_eq!(json["page"], 1);
}
}