Skip to main content

ApiResponse

Struct ApiResponse 

Source
pub struct ApiResponse<T, C, D = ()> { /* private fields */ }
Expand description

REST wire contract: {code,msg,data?,error?}.

Fields are private; constructors and deserialization enforce code/state consistency. This type has no web-framework dependency and never chooses a status, header or error message.

use aster_forge_api::response::{ApiResponse, ApiResponseCode};
#[derive(serde::Serialize)]
enum ProductCode { Success }
impl ApiResponseCode for ProductCode {
    fn is_success(&self) -> bool { matches!(self, Self::Success) }
}
let response = ApiResponse::<_, ProductCode>::ok(ProductCode::Success, vec![1, 2])?;
assert_eq!(response.data(), Some(&vec![1, 2]));

Callers cannot bypass state validation with a struct literal:

ⓘ
use aster_forge_api::response::ApiResponse;
let response = ApiResponse::<(), u8> {
    code: 0, msg: String::new(), data: Some(()), error: None,
};

Implementations§

Source§

impl<T, C: ApiResponseCode, D> ApiResponse<T, C, D>

Source

pub fn ok(code: C, data: T) -> Result<Self, ResponseEnvelopeError>

Builds success with data and an empty message, preserving a null-valued T as data.

§Errors

Returns ResponseEnvelopeError::SuccessCodeRequired for a failure code.

Source

pub fn ok_with_message( code: C, message: impl Into<String>, data: T, ) -> Result<Self, ResponseEnvelopeError>

Builds success with a product-provided message and data.

§Errors

Returns ResponseEnvelopeError::SuccessCodeRequired for a failure code.

Source§

impl<C: ApiResponseCode, D> ApiResponse<(), C, D>

Source

pub fn ok_empty(code: C) -> Result<Self, ResponseEnvelopeError>

Builds success with an empty message and omitted data.

§Errors

Returns ResponseEnvelopeError::SuccessCodeRequired for a failure code.

Source

pub fn error( code: C, message: impl Into<String>, ) -> Result<Self, ResponseEnvelopeError>

Builds a failure with retryable: false and no diagnostic, matching AD’s ordinary error.

§Errors

Returns ResponseEnvelopeError::FailureCodeRequired for a success code.

Source

pub fn error_with_details( code: C, message: impl Into<String>, error: Option<ApiErrorInfo<D>>, ) -> Result<Self, ResponseEnvelopeError>

Builds a failure with optional product error metadata. None omits the error field.

§Errors

Returns ResponseEnvelopeError::FailureCodeRequired for a success code.

Source§

impl<C: ApiResponseCode, D> ApiResponse<ApiEmptyData, C, D>

Source

pub fn ok_empty_data(code: C) -> Result<Self, ResponseEnvelopeError>

Builds success with data: {} and an empty message.

§Errors

Returns ResponseEnvelopeError::SuccessCodeRequired for a failure code.

Source§

impl<T, C, D> ApiResponse<T, C, D>

Source

pub const fn code(&self) -> &C

The product’s typed code.

Source

pub fn message(&self) -> &str

Product-provided message.

Source

pub const fn data(&self) -> Option<&T>

Data if the success includes a data field.

Source

pub fn into_data(self) -> Option<T>

Consumes the envelope and returns its optional data payload.

Source

pub const fn error_info(&self) -> Option<&ApiErrorInfo<D>>

Optional product error metadata.

Trait Implementations§

Source§

impl<T: Clone, C: Clone, D: Clone> Clone for ApiResponse<T, C, D>

Source§

fn clone(&self) -> ApiResponse<T, C, D>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: ToSchema, C: ToSchema, D: ToSchema> ComposeSchema for ApiResponse<T, C, D>

Source§

fn compose(_generics: Vec<RefOr<Schema>>) -> RefOr<Schema>

Source§

impl<T: Debug, C: Debug, D: Debug> Debug for ApiResponse<T, C, D>

Source§

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

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

impl<'de, T, C, D> Deserialize<'de> for ApiResponse<T, C, D>
where T: Deserialize<'de>, C: Deserialize<'de> + ApiResponseCode, D: Deserialize<'de>,

Source§

fn deserialize<E: Deserializer<'de>>(deserializer: E) -> Result<Self, E::Error>

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

impl<T: PartialEq, C: PartialEq, D: PartialEq> PartialEq for ApiResponse<T, C, D>

Source§

fn eq(&self, other: &ApiResponse<T, C, D>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, C, D> Serialize for ApiResponse<T, C, D>
where T: Serialize, C: Serialize, D: 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
Source§

impl<T: ToSchema, C: ToSchema, D: ToSchema> ToSchema for ApiResponse<T, C, D>

Source§

fn name() -> Cow<'static, str>

Return name of the schema. Read more
Source§

fn schemas(schemas: &mut Vec<(String, RefOr<Schema>)>)

Implement reference [utoipa::openapi::schema::Schema]s for this type. Read more
Source§

impl<T: Eq, C: Eq, D: Eq> Eq for ApiResponse<T, C, D>

Source§

impl<T, C, D> StructuralPartialEq for ApiResponse<T, C, D>

Auto Trait Implementations§

§

impl<T, C, D> Freeze for ApiResponse<T, C, D>
where C: Freeze, T: Freeze, D: Freeze,

§

impl<T, C, D> RefUnwindSafe for ApiResponse<T, C, D>

§

impl<T, C, D> Send for ApiResponse<T, C, D>
where C: Send, T: Send, D: Send,

§

impl<T, C, D> Sync for ApiResponse<T, C, D>
where C: Sync, T: Sync, D: Sync,

§

impl<T, C, D> Unpin for ApiResponse<T, C, D>
where C: Unpin, T: Unpin, D: Unpin,

§

impl<T, C, D> UnsafeUnpin for ApiResponse<T, C, D>

§

impl<T, C, D> UnwindSafe for ApiResponse<T, C, D>
where C: UnwindSafe, T: UnwindSafe, D: 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
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.

§

impl<T> PartialSchema for T
where T: ComposeSchema + ?Sized,

§

fn schema() -> RefOr<Schema>

Return ref or schema of implementing type that can then be used to construct combined schemas.
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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,