Derive Macro tsukuyomi::IntoResponse
source · #[derive(IntoResponse)]
{
// Attributes available to this derive:
#[response]
}
Expand description
A procedural macro for deriving the implementation of IntoResponse
.
Examples
This macro has a parameter #[response(preset = "..")]
, which specifies
the path to a type that implements a trait Preset
:
use serde::Serialize;
#[derive(Debug, Serialize, IntoResponse)]
#[response(preset = "tsukuyomi::output::preset::Json")]
struct Post {
title: String,
text: String,
}
You can specify the additional trait bounds to type parameters
by using the parameter #[response(bound = "..")]
:
#[derive(Debug, IntoResponse)]
#[response(
preset = "tsukuyomi::output::preset::Json",
bound = "T: Serialize",
bound = "U: Serialize",
)]
struct CustomValue<T, U> {
t: T,
u: U,
}
Notes
- When
preset = ".."
is omitted for struct, a field in the specified struct is chosen and the the implementation ofIntoResponse
for its type is used. For example, the impls derived to the following types outputs eventually the same result as the implementation ofIntoResponse
forString
:#[derive(IntoResponse)] struct Foo(String); #[derive(IntoResponse)] struct Bar { inner: String, }
- When
preset = ".."
is omitted for enum, the same rule as struct is applied to each variant:#[derive(IntoResponse)] enum MyResponse { Text(String), Raw { response: Response<String> }, }
- Without specifying the preset, the number of fields in the struct
or the number of fields of each variant inside of the enum must be
at most one. This is because the field that implements
IntoResponse
cannot be determined if there are two or more fields in a struct or a variant:If you want to apply the derivation to complex enums, consider cutting each variant into one struct and specifying the preset explicitly as follows:ⓘ#[derive(IntoResponse)] enum ApiResponse { Text(String), Post { title: String, text: String }, }
#[derive(IntoResponse)] enum ApiResponse { Text(String), Post(Post), } #[derive(Debug, Serialize, IntoResponse)] #[response(preset = "tsukuyomi::output::preset::Json")] struct Post { title: String, text: String, }