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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! 请求路径参数
use serde::{de::DeserializeOwned, Serialize, Serializer};
use std::{
    fmt::{Debug, Display, Formatter},
    ops::{Deref, DerefMut},
    sync::Arc,
};
use utoipa::{
    openapi::{RefOr, Schema},
    ToSchema,
};

use crate::tina::{
    data::AppResult,
    server::{
        application::{AppConfig, Application},
        http::request::RequestAttribute,
        session::Session,
    },
};

/// 请求路径变量
pub struct HttpReqPath<D: DeserializeOwned> {
    names: Vec<String>,
    data: D,
    application: Application,
    session: Session,
}

impl<D: DeserializeOwned> RequestAttribute for HttpReqPath<D> {
    fn application(&self) -> Application {
        self.application.to_owned()
    }
    fn session(&self) -> Session {
        self.session.to_owned()
    }
}

impl<D: DeserializeOwned> HttpReqPath<D> {
    /// 构建
    pub fn new(names: Vec<String>, data: D, application: Application, session: Session) -> HttpReqPath<D> {
        HttpReqPath {
            names,
            data,
            application,
            session,
        }
    }
    /// 获取参数名称
    pub fn names(&self) -> &[String] {
        self.names.as_slice()
    }
    /// 提取信息
    pub fn into_inner(self) -> (Vec<String>, D, Session) {
        (self.names, self.data, self.session)
    }
    /// 提取数据
    pub fn into_data(self) -> D {
        self.data
    }
    /// 获取扩展
    pub fn extension<T: Send + Sync + 'static>(&self) -> AppResult<Arc<T>> {
        self.application.extension()
    }
}

impl<D: DeserializeOwned> From<HttpReqPath<D>> for (D, Session) {
    fn from(value: HttpReqPath<D>) -> Self {
        (value.data, value.session)
    }
}

impl<D: DeserializeOwned> From<HttpReqPath<D>> for (D, Session, Application) {
    fn from(value: HttpReqPath<D>) -> Self {
        (value.data, value.session, value.application)
    }
}

impl<D: DeserializeOwned + Default> Default for HttpReqPath<D> {
    fn default() -> Self {
        HttpReqPath {
            names: vec![],
            data: D::default(),
            application: AppConfig::new().into(),
            session: Session::default(),
        }
    }
}

impl<'a, D: DeserializeOwned + ToSchema<'a>> ToSchema<'a> for HttpReqPath<D> {
    fn schema() -> (&'a str, RefOr<Schema>) {
        D::schema()
    }
}

impl<D: DeserializeOwned> Deref for HttpReqPath<D> {
    type Target = D;

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl<D: DeserializeOwned> DerefMut for HttpReqPath<D> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.data
    }
}

impl<D: DeserializeOwned + Debug> Debug for HttpReqPath<D> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.data.fmt(f)
    }
}

impl<D: DeserializeOwned + Display> Display for HttpReqPath<D> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.data.fmt(f)
    }
}

impl<D: DeserializeOwned + Serialize> Serialize for HttpReqPath<D> {
    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
    where
        S: Serializer,
    {
        self.data.serialize(serializer)
    }
}