pub struct ApiResponse {
pub code: i32,
pub msg: String,
pub data: Value,
}Expand description
标准 API 响应结构体
严格对齐 PHP renderJson 输出格式:{code, msg, data},字段顺序固定。
§用法
ⓘ
use sz_rust_http_facade::response::ApiResponse;
use serde_json::json;
// 成功响应
let resp = ApiResponse::success(json!({"id": 1}), "ok");
// 错误响应
let resp = ApiResponse::error("参数错误");
// 自定义 code
let resp = ApiResponse::new(-1, "未登录", json!({}));Fields§
§code: i32业务状态码(1=成功,0=失败,-1=未登录,与 PHP BaseException 对齐)
msg: String业务消息
data: Value业务数据
Implementations§
Source§impl ApiResponse
impl ApiResponse
Sourcepub fn success(data: Value, msg: impl Into<String>) -> Self
pub fn success(data: Value, msg: impl Into<String>) -> Self
创建成功响应(code=1)
对齐 PHP renderSuccess($data = [], $msg = '')。
Sourcepub fn success_empty() -> Self
pub fn success_empty() -> Self
创建成功响应(默认空 data + 空 msg)
Sourcepub fn error(msg: impl Into<String>) -> Self
pub fn error(msg: impl Into<String>) -> Self
创建错误响应(code=0)
对齐 PHP renderError($msg = '', $data = [])。
Sourcepub fn error_with_data(msg: impl Into<String>, data: Value) -> Self
pub fn error_with_data(msg: impl Into<String>, data: Value) -> Self
创建带数据的错误响应(code=0)
Sourcepub fn error_with_code(code: i32, msg: impl Into<String>, data: Value) -> Self
pub fn error_with_code(code: i32, msg: impl Into<String>, data: Value) -> Self
创建带自定义错误码的错误响应
对齐 PHP renderError($code, $msg, $data)。
Sourcepub fn to_value(&self) -> Value
pub fn to_value(&self) -> Value
序列化为 serde_json::Value(保证字段顺序 code → msg → data)
使用 serde_json::Map(启用 preserve_order)保证插入顺序。
Sourcepub fn to_json_string(&self) -> String
pub fn to_json_string(&self) -> String
序列化为 JSON 字符串
Sourcepub fn to_json_bytes(&self) -> Bytes
pub fn to_json_bytes(&self) -> Bytes
序列化为 bytes::Bytes(零拷贝引用计数字节容器)
P3 优化:替代 to_json_string 的 String 分配,
使用 serde_json::to_vec 序列化到 Vec<u8> 后转为 Bytes,
避免 String 的 UTF-8 验证开销,支持零拷贝传递。
输出与 to_json_string 逐字节一致。
Trait Implementations§
Source§impl Clone for ApiResponse
impl Clone for ApiResponse
Source§fn clone(&self) -> ApiResponse
fn clone(&self) -> ApiResponse
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ApiResponse
impl Debug for ApiResponse
Source§impl IntoResponse for ApiResponse
让 ApiResponse 可以直接作为 axum handler 返回值
impl IntoResponse for ApiResponse
让 ApiResponse 可以直接作为 axum handler 返回值
自动设置:
- HTTP 状态码:200(无论业务 code 是 1 还是 0,HTTP 都是 200,对齐 PHP 行为)
- Content-Type:
application/json; charset=utf-8
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Create a response.
Auto Trait Implementations§
impl Freeze for ApiResponse
impl RefUnwindSafe for ApiResponse
impl Send for ApiResponse
impl Sync for ApiResponse
impl Unpin for ApiResponse
impl UnsafeUnpin for ApiResponse
impl UnwindSafe for ApiResponse
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, S> Handler<IntoResponseHandler, S> for T
impl<T, S> Handler<IntoResponseHandler, S> for T
Source§fn call(
self,
_req: Request<Body>,
_state: S,
) -> <T as Handler<IntoResponseHandler, S>>::Future
fn call( self, _req: Request<Body>, _state: S, ) -> <T as Handler<IntoResponseHandler, S>>::Future
Call the handler with the given request.
Source§fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
Apply a
tower::Layer to the handler. Read moreSource§fn with_state(self, state: S) -> HandlerService<Self, T, S>
fn with_state(self, state: S) -> HandlerService<Self, T, S>
Convert the handler into a
Service by providing the stateSource§impl<H, T> HandlerWithoutStateExt<T> for H
impl<H, T> HandlerWithoutStateExt<T> for H
Source§fn into_service(self) -> HandlerService<H, T, ()>
fn into_service(self) -> HandlerService<H, T, ()>
Convert the handler into a
Service and no state.Source§fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
Convert the handler into a
MakeService and no state. Read moreSource§fn into_make_service_with_connect_info<C>(
self,
) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
fn into_make_service_with_connect_info<C>( self, ) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
Convert the handler into a
MakeService which stores information
about the incoming connection and has no state. Read more