1use std::collections::{HashMap, HashSet};
2
3use serde::{Deserialize, Serialize};
4
5use crate::{
6 error::{ApiErrorItem, ApiErrors},
7 ty::{ApiModel, ApiTy},
8};
9
10#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
12pub struct ApiResponse {
13 pub header: Vec<ApiTy>,
15
16 pub content_type: Option<String>,
18
19 pub body: Option<ApiTy>,
21
22 pub error: HashSet<ApiErrorItem>,
24}
25
26pub trait ApiResultExtractor {
27 fn api_response(models: &mut HashMap<String, Option<ApiModel>>) -> ApiResponse;
28}
29
30impl<T: ApiResultExtractor, E: ApiErrors> ApiResultExtractor for Result<T, E> {
31 fn api_response(models: &mut HashMap<String, Option<ApiModel>>) -> ApiResponse {
32 let mut response = T::api_response(models);
33 response.error.extend(E::api_errors());
34 response
35 }
36}
37
38impl ApiResultExtractor for () {
39 fn api_response(_models: &mut HashMap<String, Option<ApiModel>>) -> ApiResponse {
40 ApiResponse::default()
41 }
42}