An attribute macro to transform
a handler response struct to an actix responder
.
Keeps flexibility while adding more type safety.
The actix_responder
adds an additional field to your struct which uses to set things
like content_type
and response_code
.
The meta_attr
allows for arbitrary copy paste to the meta field
the macro is adding to the struct.
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 the generated field
so it won't show up in the request response.
```rust
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 health_check() -> SuccessResp { SuccessResp::builder().success(true).build() } ```
A more complicated example with setting default values
```rust extern crate actixrespondermacro; extern crate typed_builder;
use actixrespondermacro::actixresponder; use actixweb::http::StatusCode; use serde::{Deserialize, Serialize}; use typed_builder::TypedBuilder;
default = SuccessRespMetadata{
status_code: Some(StatusCode::INTERNAL_SERVER_ERROR),
content_type: Some("image/bmp".to_string())
}
)"#)]
struct ImageResp {...} ```