use crate::HasValidate;
#[cfg(feature = "validator")]
use crate::HasValidateArgs;
use axum_serde::{MsgPack, MsgPackRaw};
#[cfg(feature = "validator")]
use validator::ValidateArgs;
impl<T> HasValidate for MsgPack<T> {
type Validate = T;
fn get_validate(&self) -> &T {
&self.0
}
}
#[cfg(feature = "validator")]
impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for MsgPack<T> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.0
}
}
#[cfg(feature = "validify")]
impl<T: validify::Modify> crate::HasModify for MsgPack<T> {
type Modify = T;
fn get_modify(&mut self) -> &mut Self::Modify {
&mut self.0
}
}
#[cfg(feature = "validify")]
impl<T> crate::PayloadExtractor for MsgPack<T> {
type Payload = T;
fn get_payload(self) -> Self::Payload {
self.0
}
}
#[cfg(feature = "validify")]
impl<T: validify::Validify + validify::ValidifyPayload> crate::HasValidify for MsgPack<T> {
type Validify = T;
type PayloadExtractor = MsgPack<T::Payload>;
fn from_validify(v: Self::Validify) -> Self {
MsgPack(v)
}
}
impl<T> HasValidate for MsgPackRaw<T> {
type Validate = T;
fn get_validate(&self) -> &T {
&self.0
}
}
#[cfg(feature = "validator")]
impl<'v, T: ValidateArgs<'v>> HasValidateArgs<'v> for MsgPackRaw<T> {
type ValidateArgs = T;
fn get_validate_args(&self) -> &Self::ValidateArgs {
&self.0
}
}
#[cfg(feature = "validify")]
impl<T: validify::Modify> crate::HasModify for MsgPackRaw<T> {
type Modify = T;
fn get_modify(&mut self) -> &mut Self::Modify {
&mut self.0
}
}
#[cfg(feature = "validify")]
impl<T> crate::PayloadExtractor for MsgPackRaw<T> {
type Payload = T;
fn get_payload(self) -> Self::Payload {
self.0
}
}
#[cfg(feature = "validify")]
impl<T: validify::Validify + validify::ValidifyPayload> crate::HasValidify for MsgPackRaw<T> {
type Validify = T;
type PayloadExtractor = MsgPackRaw<T::Payload>;
fn from_validify(v: Self::Validify) -> Self {
MsgPackRaw(v)
}
}
#[cfg(test)]
mod tests {
use crate::tests::{ValidTest, ValidTestParameter};
use axum::http::StatusCode;
use axum_serde::{MsgPack, MsgPackRaw};
use reqwest::RequestBuilder;
use serde::Serialize;
impl<T: ValidTestParameter + Serialize> ValidTest for MsgPack<T> {
const ERROR_STATUS_CODE: StatusCode = StatusCode::UNPROCESSABLE_ENTITY;
fn set_valid_request(builder: RequestBuilder) -> RequestBuilder {
builder
.header(reqwest::header::CONTENT_TYPE, "application/msgpack")
.body(
rmp_serde::to_vec_named(T::valid())
.expect("Failed to serialize parameters to msgpack"),
)
}
fn set_error_request(builder: RequestBuilder) -> RequestBuilder {
#[derive(Serialize, Default)]
struct ErrorData {
error_field0: i32,
error_field1: Option<String>,
}
builder
.header(reqwest::header::CONTENT_TYPE, "application/msgpack")
.body(
rmp_serde::to_vec(&ErrorData::default())
.expect("Failed to serialize parameters to msgpack"),
)
}
fn set_invalid_request(builder: RequestBuilder) -> RequestBuilder {
builder
.header(reqwest::header::CONTENT_TYPE, "application/msgpack")
.body(
rmp_serde::to_vec_named(T::invalid())
.expect("Failed to serialize parameters to msgpack"),
)
}
}
impl<T: ValidTestParameter + Serialize> ValidTest for MsgPackRaw<T> {
const ERROR_STATUS_CODE: StatusCode = StatusCode::UNPROCESSABLE_ENTITY;
fn set_valid_request(builder: RequestBuilder) -> RequestBuilder {
builder
.header(reqwest::header::CONTENT_TYPE, "application/msgpack")
.body(
rmp_serde::to_vec(T::valid())
.expect("Failed to serialize parameters to msgpack"),
)
}
fn set_error_request(builder: RequestBuilder) -> RequestBuilder {
#[derive(Serialize, Default)]
struct ErrorData {
error_field0: i32,
error_field1: Option<String>,
}
builder
.header(reqwest::header::CONTENT_TYPE, "application/msgpack")
.body(
rmp_serde::to_vec(&ErrorData::default())
.expect("Failed to serialize parameters to msgpack"),
)
}
fn set_invalid_request(builder: RequestBuilder) -> RequestBuilder {
builder
.header(reqwest::header::CONTENT_TYPE, "application/msgpack")
.body(
rmp_serde::to_vec(T::invalid())
.expect("Failed to serialize parameters to msgpack"),
)
}
}
}