1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::collections::{HashMap, HashSet};

use serde::{Deserialize, Serialize};

use crate::{
    error::ApiErrorItem,
    ty::{ApiModel, ApiTy},
};

// 请求参数
#[derive(Debug, Default, Serialize, Deserialize)]
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, Serialize, Deserialize)]
pub enum ApiParamPart {
    Header(ApiTy),
    Path(ApiTy),
    Query(ApiTy),
    Body { content_type: String, ty: ApiTy },
}

#[derive(Debug, Serialize, Deserialize)]
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>;
}