GithubHelp home page GithubHelp logo

actix-responder-macro's Introduction

actix-responder-macro

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.

#[actix_responder(meta_attr = "builder(default)")]
#[derive(TypedBuilder, Serialize, Deserialize)]
pub struct SuccessResp {
    success: bool,
}

From this

#[get("/health_check")]
pub async fn health_check() -> impl Responder {
    HttpResponse::Ok()
    .set_header(header::CONTENT_TYPE, mime::APPLICATION_JSON)
    .json(SuccessResp { success: true })
}

to this

#[get("/health_check")]
pub async fn health_check() -> SuccessResp {
    SuccessResp::builder()
    .success(true)
    .content_type(mime::APPLICATION_JSON::to_string())
    .build()
}

A more complicated example with setting default values

extern crate actix_responder_macro;
extern crate mime;
extern crate typed_builder;

use actix_responder_macro::actix_responder;
use actix_web::http::StatusCode;
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

#[actix_responder(
    status_attr = "builder(default = StatusCode::INTERNAL_SERVER_ERROR)",
    content_attr = "builder(default = mime::IMAGE_BMP.to_string())"
)]
#[derive(Serialize, Deserialize, Debug, TypedBuilder, Default)]
pub struct ImageResp {...}

actix-responder-macro's People

Contributors

petar-dambovaliev avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.