1use crate::HasValidate;
96#[cfg(feature = "validator")]
97use crate::HasValidateArgs;
98use axum_serde::{MsgPack, MsgPackRaw};
99#[cfg(feature = "validator")]
100use validator::ValidateArgs;
101
102impl<T> HasValidate for MsgPack<T> {
103 type Validate = T;
104 fn get_validate(&self) -> &T {
105 &self.0
106 }
107}
108
109#[cfg(feature = "validator")]
110impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for MsgPack<T> {
111 type ValidateArgs = T;
112 fn get_validate_args(&self) -> &Self::ValidateArgs {
113 &self.0
114 }
115}
116
117#[cfg(feature = "validify")]
118impl<T: validify::Modify> crate::HasModify for MsgPack<T> {
119 type Modify = T;
120
121 fn get_modify(&mut self) -> &mut Self::Modify {
122 &mut self.0
123 }
124}
125
126#[cfg(feature = "validify")]
127impl<T> crate::PayloadExtractor for MsgPack<T> {
128 type Payload = T;
129
130 fn get_payload(self) -> Self::Payload {
131 self.0
132 }
133}
134
135#[cfg(feature = "validify")]
136impl<T: validify::Validify + validify::ValidifyPayload> crate::HasValidify for MsgPack<T> {
137 type Validify = T;
138 type PayloadExtractor = MsgPack<T::Payload>;
139 fn from_validify(v: Self::Validify) -> Self {
140 MsgPack(v)
141 }
142}
143
144impl<T> HasValidate for MsgPackRaw<T> {
145 type Validate = T;
146 fn get_validate(&self) -> &T {
147 &self.0
148 }
149}
150
151#[cfg(feature = "validator")]
152impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for MsgPackRaw<T> {
153 type ValidateArgs = T;
154 fn get_validate_args(&self) -> &Self::ValidateArgs {
155 &self.0
156 }
157}
158
159#[cfg(feature = "validify")]
160impl<T: validify::Modify> crate::HasModify for MsgPackRaw<T> {
161 type Modify = T;
162
163 fn get_modify(&mut self) -> &mut Self::Modify {
164 &mut self.0
165 }
166}
167
168#[cfg(feature = "validify")]
169impl<T> crate::PayloadExtractor for MsgPackRaw<T> {
170 type Payload = T;
171
172 fn get_payload(self) -> Self::Payload {
173 self.0
174 }
175}
176
177#[cfg(feature = "validify")]
178impl<T: validify::Validify + validify::ValidifyPayload> crate::HasValidify for MsgPackRaw<T> {
179 type Validify = T;
180 type PayloadExtractor = MsgPackRaw<T::Payload>;
181 fn from_validify(v: Self::Validify) -> Self {
182 MsgPackRaw(v)
183 }
184}
185
186#[cfg(test)]
187mod tests {
188 use crate::tests::{ValidTest, ValidTestParameter};
189 use axum::http::StatusCode;
190 use axum_serde::{MsgPack, MsgPackRaw};
191 use reqwest::RequestBuilder;
192 use serde::Serialize;
193
194 impl<T: ValidTestParameter + Serialize> ValidTest for MsgPack<T> {
195 const ERROR_STATUS_CODE: StatusCode = StatusCode::UNPROCESSABLE_ENTITY;
196
197 fn set_valid_request(builder: RequestBuilder) -> RequestBuilder {
198 builder
199 .header(reqwest::header::CONTENT_TYPE, "application/msgpack")
200 .body(
201 rmp_serde::to_vec_named(T::valid())
202 .expect("Failed to serialize parameters to msgpack"),
203 )
204 }
205
206 fn set_error_request(builder: RequestBuilder) -> RequestBuilder {
207 #[derive(Serialize, Default)]
208 struct ErrorData {
209 error_field0: i32,
210 error_field1: Option<String>,
211 }
212 builder
213 .header(reqwest::header::CONTENT_TYPE, "application/msgpack")
214 .body(
215 rmp_serde::to_vec(&ErrorData::default())
216 .expect("Failed to serialize parameters to msgpack"),
217 )
218 }
219
220 fn set_invalid_request(builder: RequestBuilder) -> RequestBuilder {
221 builder
222 .header(reqwest::header::CONTENT_TYPE, "application/msgpack")
223 .body(
224 rmp_serde::to_vec_named(T::invalid())
225 .expect("Failed to serialize parameters to msgpack"),
226 )
227 }
228 }
229
230 impl<T: ValidTestParameter + Serialize> ValidTest for MsgPackRaw<T> {
231 const ERROR_STATUS_CODE: StatusCode = StatusCode::UNPROCESSABLE_ENTITY;
232
233 fn set_valid_request(builder: RequestBuilder) -> RequestBuilder {
234 builder
235 .header(reqwest::header::CONTENT_TYPE, "application/msgpack")
236 .body(
237 rmp_serde::to_vec(T::valid())
238 .expect("Failed to serialize parameters to msgpack"),
239 )
240 }
241
242 fn set_error_request(builder: RequestBuilder) -> RequestBuilder {
243 #[derive(Serialize, Default)]
244 struct ErrorData {
245 error_field0: i32,
246 error_field1: Option<String>,
247 }
248 builder
249 .header(reqwest::header::CONTENT_TYPE, "application/msgpack")
250 .body(
251 rmp_serde::to_vec(&ErrorData::default())
252 .expect("Failed to serialize parameters to msgpack"),
253 )
254 }
255
256 fn set_invalid_request(builder: RequestBuilder) -> RequestBuilder {
257 builder
258 .header(reqwest::header::CONTENT_TYPE, "application/msgpack")
259 .body(
260 rmp_serde::to_vec(T::invalid())
261 .expect("Failed to serialize parameters to msgpack"),
262 )
263 }
264 }
265}