An attribute macro to transform
a handler response struct to an actix responder
.
Keeps flexibility while adding more type safety.
The actix_responder
adds 2 additional fields to your struct
content_type
and status_code
.
The meta_attr
allows for arbitrary attributes to both fields.
status_attr
applies only to status_code
and content_attr
applies only to content_type
The reason for this is like in the example below, if you use
a crate like TypedBuilder
, you might want to apply options like
#[builder(default)]
to the generated field.
The macro always applies #[serde(skip)]
to both generated fields
so they won't show up in the request response.
```rust
pub struct SuccessResp { success: bool, } ```
From this
```rust
pub async fn healthcheck() -> impl Responder { HttpResponse::Ok() .setheader(header::CONTENTTYPE, mime::APPLICATIONJSON) .json(SuccessResp { success: true }) } ```
to this
```rust
pub async fn healthcheck() -> SuccessResp { SuccessResp::builder() .success(true) .contenttype(mime::APPLICATIONJSON::tostring()) .build() } ```
A more complicated example with setting default values
```rust extern crate actixrespondermacro; extern crate mime; extern crate typed_builder;
use actixrespondermacro::actixresponder; use actixweb::http::StatusCode; use serde::{Deserialize, Serialize}; use typed_builder::TypedBuilder;
status_attr = "builder(default = StatusCode::INTERNAL_SERVER_ERROR)",
content_attr = "builder(default = mime::IMAGE_BMP.to_string())"
)]
pub struct ImageResp {...} ```