use std::collections::{HashMap, HashSet};
use serde::{Deserialize, Serialize};
use crate::{
error::ApiErrorItem,
ty::{ApiModel, ApiTy},
};
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct ApiRequest {
pub path: Vec<ApiTy>,
pub query: Vec<ApiTy>,
pub header: Vec<ApiTy>,
pub content_type: Option<String>,
pub body: Option<ApiTy>,
pub error: HashSet<ApiErrorItem>,
}
impl ApiRequest {
pub fn add_param(&mut self, param_ty: ApiParamType) {
match param_ty {
ApiParamType::Param(param) => {
for item in param {
match item {
ApiParamPart::Header(value) => {
self.header.push(value);
}
ApiParamPart::Path(value) => {
self.path.push(value);
}
ApiParamPart::Query(value) => {
self.query.push(value);
}
ApiParamPart::Body { content_type, ty } => {
self.body = Some(ty);
self.content_type = Some(content_type);
}
}
}
}
ApiParamType::State => {}
}
}
pub fn add_error(&mut self, errors: Vec<ApiErrorItem>) {
self.error.extend(errors);
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ApiParamPart {
Header(ApiTy),
Path(ApiTy),
Query(ApiTy),
Body { content_type: String, ty: ApiTy },
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ApiParamType {
Param(Vec<ApiParamPart>),
State,
}
pub trait ApiParamExtractor {
fn api_param_kind(models: &mut HashMap<String, Option<ApiModel>>) -> ApiParamType;
fn api_error() -> Vec<ApiErrorItem>;
}