apidoc_core/
request.rs

1use std::collections::{HashMap, HashSet};
2
3use serde::{Deserialize, Serialize};
4
5use crate::{
6    error::ApiErrorItem,
7    ty::{ApiModel, ApiTy},
8};
9
10// 请求参数
11#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
12pub struct ApiRequest {
13    // 路径参数
14    pub path: Vec<ApiTy>,
15
16    // 查询参数
17    pub query: Vec<ApiTy>,
18
19    // 请求头参数
20    pub header: Vec<ApiTy>,
21
22    // 内容类型
23    pub content_type: Option<String>,
24
25    // 请求体参数
26    pub body: Option<ApiTy>,
27
28    // 请求错误
29    pub error: HashSet<ApiErrorItem>,
30}
31
32impl ApiRequest {
33    pub fn add_param(&mut self, param_ty: ApiParamType) {
34        match param_ty {
35            ApiParamType::Param(param) => {
36                for item in param {
37                    match item {
38                        ApiParamPart::Header(value) => {
39                            self.header.push(value);
40                        }
41                        ApiParamPart::Path(value) => {
42                            self.path.push(value);
43                        }
44                        ApiParamPart::Query(value) => {
45                            self.query.push(value);
46                        }
47                        ApiParamPart::Body { content_type, ty } => {
48                            self.body = Some(ty);
49                            self.content_type = Some(content_type);
50                        }
51                    }
52                }
53            }
54            ApiParamType::State => {}
55        }
56    }
57
58    pub fn add_error(&mut self, errors: Vec<ApiErrorItem>) {
59        self.error.extend(errors);
60    }
61}
62
63#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
64pub enum ApiParamPart {
65    Header(ApiTy),
66    Path(ApiTy),
67    Query(ApiTy),
68    Body { content_type: String, ty: ApiTy },
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
72pub enum ApiParamType {
73    Param(Vec<ApiParamPart>),
74
75    State,
76}
77
78pub trait ApiParamExtractor {
79    fn api_param_kind(models: &mut HashMap<String, Option<ApiModel>>) -> ApiParamType;
80
81    fn api_error() -> Vec<ApiErrorItem>;
82}