Skip to main content

ApiResult

Struct ApiResult 

Source
pub struct ApiResult<T> {
    pub success: bool,
    pub data: Option<ResponseData<T>>,
    pub message: Option<String>,
    pub code: Option<i32>,
    pub pagination: Option<ResultPagination>,
    pub errors: Option<Vec<FieldError>>,
    pub trace_id: Option<String>,
    pub extra: Option<Value>,
}
Expand description

API response wrapper with unified structure

Fields§

§success: bool§data: Option<ResponseData<T>>§message: Option<String>§code: Option<i32>§pagination: Option<ResultPagination>§errors: Option<Vec<FieldError>>§trace_id: Option<String>§extra: Option<Value>

Implementations§

Source§

impl<T> ApiResult<T>

Source

pub fn value(v: T) -> Self

Create a successful response with a single value

Source

pub fn list(v: Vec<T>) -> Self

Create a successful response with a list of values

Source

pub fn fail(message: &str) -> Self

Create a failed response with a message (alias for failure)

Source

pub fn failure(message: &str) -> Self

Create a failed response with a message

Source

pub fn success() -> Self

Create a successful response without data

Alias for ok() - use ok() for clarity

Source

pub fn ok() -> Self

Create a successful response without data (recommended method name)

Source

pub fn validation_errors(errors: Vec<FieldError>) -> Self

Create a validation error response with field-level details

Source

pub fn from_result<E: Display>(result: Result<T, E>) -> Self

Convert a Result<T, E> into ApiResult<T>, mapping all errors to InternalError

Source

pub fn with_extra(self, key: &str, value: Value) -> Self

Add extra metadata to the response

Source

pub fn with_code(self, code: i32) -> Self

Set error code for the response

Source

pub fn with_pagination(self, pagination: ResultPagination) -> Self

Set pagination metadata for list responses

Source

pub fn with_message(self, message: &str) -> Self

Set message for the response

Source

pub fn with_errors(self, errors: Vec<FieldError>) -> Self

Set field-level validation errors

Source

pub fn with_error( self, field: impl Into<String>, message: impl Into<String>, ) -> Self

Add a single field-level validation error

Source

pub fn with_trace_id(self, id: impl Into<String>) -> Self

Set request trace ID

Trait Implementations§

Source§

impl<T: Clone> Clone for ApiResult<T>

Source§

fn clone(&self) -> ApiResult<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for ApiResult<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, T> Deserialize<'de> for ApiResult<T>
where T: Deserialize<'de>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> From<ApiError> for ApiResult<T>

Source§

fn from(err: ApiError) -> Self

Converts to this type from the input type.
Source§

impl<T> From<AppError> for ApiResult<T>

Source§

fn from(err: AppError) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Error> for ApiResult<T>

Source§

fn from(err: Error) -> Self

Converts to this type from the input type.
Source§

impl<T, E: Display + Debug> From<Result<T, E>> for ApiResult<T>

Convert any Result<T, E> where E implements Display to ApiResult<T>

Source§

fn from(result: Result<T, E>) -> Self

Converts to this type from the input type.
Source§

impl<T, E> Into<Result<ApiResult<T>, E>> for ApiResult<T>

Conversion into Result for convenient error handling

Source§

fn into(self) -> Result<ApiResult<T>, E>

Converts this type into the (usually inferred) input type.
Source§

impl<T: Serialize> Responder for ApiResult<T>

Available on crate feature actix only.

Actix-web framework integration for ApiResult

Source§

type Body = BoxBody

Source§

fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body>

Convert self to HttpResponse.
Source§

fn customize(self) -> CustomizeResponder<Self>
where Self: Sized,

Wraps responder to allow alteration of its response. Read more
Source§

impl<T> Serialize for ApiResult<T>
where T: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T> Freeze for ApiResult<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for ApiResult<T>
where T: RefUnwindSafe,

§

impl<T> Send for ApiResult<T>
where T: Send,

§

impl<T> Sync for ApiResult<T>
where T: Sync,

§

impl<T> Unpin for ApiResult<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for ApiResult<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for ApiResult<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,