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
130
131
132
133
134
135
136
137
138
139
//! byte请求
use std::{
    any::type_name,
    fmt::{Debug, Display, Formatter},
    ops::{Deref, DerefMut},
    sync::Arc,
};

use serde::ser::Error;
use serde::{Serialize, Serializer};
use serde_json::value::RawValue;
use utoipa::{
    openapi::{ObjectBuilder, RefOr, Schema, SchemaType},
    ToSchema,
};

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

/// 二进制请求体
pub struct HttpReqByte {
    data: Vec<u8>,
    application: Application,
    session: Session,
}

impl RequestAttribute for HttpReqByte {
    fn application(&self) -> Application {
        self.application.to_owned()
    }
    fn session(&self) -> Session {
        self.session.to_owned()
    }
}

impl HttpReqByte {
    /// 构建
    pub fn new(data: Vec<u8>, application: Application, session: Session) -> HttpReqByte {
        HttpReqByte {
            data,
            application,
            session,
        }
    }
    /// 提取信息
    pub fn into_inner(self) -> (Vec<u8>, Session) {
        (self.data, self.session)
    }
    /// 提取请求体数据
    pub fn into_data(self) -> Vec<u8> {
        self.data
    }
    /// 获取扩展
    pub fn extension<T: Send + Sync + 'static>(&self) -> AppResult<Arc<T>> {
        self.application.extension()
    }
}

impl From<HttpReqByte> for (Vec<u8>, Session) {
    fn from(value: HttpReqByte) -> Self {
        (value.data, value.session)
    }
}

impl From<HttpReqByte> for (Vec<u8>, Session, Application) {
    fn from(value: HttpReqByte) -> Self {
        (value.data, value.session, value.application)
    }
}

impl<'a> ToSchema<'a> for HttpReqByte {
    fn schema() -> (&'a str, RefOr<Schema>) {
        (type_name::<HttpReqByte>(), RefOr::T(Schema::from(ObjectBuilder::new().schema_type(SchemaType::Object))))
    }
}

impl Default for HttpReqByte {
    fn default() -> Self {
        HttpReqByte {
            data: Vec::new(),
            application: AppConfig::new().into(),
            session: Session::default(),
        }
    }
}

impl Deref for HttpReqByte {
    type Target = Vec<u8>;

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

impl DerefMut for HttpReqByte {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.data
    }
}

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

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

impl Serialize for HttpReqByte {
    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
    where
        S: Serializer,
    {
        match self.data.is_empty() {
            true => serializer.serialize_unit(),
            false => {
                let str = String::from_utf8(self.data.to_vec()).map_err(|err| <S as Serializer>::Error::custom(format!("{:?}", err)))?;
                match str.not_empty() {
                    true => {
                        let raw_value = RawValue::from_string(str).map_err(|err| <S as Serializer>::Error::custom(format!("{:?}", err)))?;
                        raw_value.serialize(serializer)
                    }
                    false => serializer.serialize_str(str.as_str()),
                }
            }
        }
    }
}