pub struct ApiResponse {
pub code: i32,
pub msg: String,
pub data: Value,
}Expand description
标准 API 响应结构体
严格对齐 PHP renderJson 输出格式:{code, msg, data},字段顺序固定。
§用法
ⓘ
use sz_rust_core::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 字符串
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 moreSource§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.