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 extra: Option<Value>,
}
Expand description

API response wrapper with unified structure

§Fields

  • success: Indicates if the request was successful
  • data: Contains either a single value or a list (via ResponseData)
  • message: Optional message or error description
  • code: Optional error code
  • pagination: Optional pagination metadata for list responses
  • extra: Additional metadata as key-value pairs

§JSON Serialization Examples

// Single value
{ "success": true, "data": { "id": 1, "name": "Alice" } }

// List
{ "success": true, "data": [{ "id": 1 }, { "id": 2 }], "pagination": {...} }

// Empty success
{ "success": true }

// Error
{ "success": false, "message": "Not found", "code": 404 }

Fields§

§success: bool§data: Option<ResponseData<T>>§message: Option<String>§code: Option<i32>§pagination: Option<ResultPagination>§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 to avoid confusion with the success field

Source

pub fn ok() -> Self

Create a successful response without data (recommended method name)

Source

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

Add extra metadata to the response

§Example
use anycms_core::ApiResult;
use serde_json::json;

let result: ApiResult<()> = ApiResult::ok()
    .with_extra("timestamp", json!(1234567890))
    .with_extra("version", json!("1.0.0"));
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

Trait Implementations§

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> 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> 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, 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>,