4 releases
0.2.0 | Aug 26, 2024 |
---|---|
0.1.2 | Feb 28, 2024 |
0.1.1 | Feb 28, 2024 |
0.1.0 | Feb 28, 2024 |
#7 in #status-code
35 downloads per month
11KB
193 lines
axum-enum-response
MSRV: 1.65.0
Easily create axum::http::Response's from Enums!
Example Usage
#[derive(serde::Serialize)]
struct SomeData {
meow: String,
}
enum ErrorResponse {
#[status_code(UNAUTHORIZED)]
Unauthorized, // 401, empty body
#[status_code(OK)]
#[body("hello"=>"world")]
Ok, // 200, body = {"hello": "world"}
#[status_code(FORBIDDEN)]
#[body("mew")]
Forbidden, // 403, body = {"error": "mew"}
#[status_code(INTERNAL_SERVER_ERROR)]
FromUtf8Error(#[from] FromUtf8Error), // 500, body = {"error": FromUtf8Error::to_string()}
#[status_code(INTERNAL_SERVER_ERROR)]
InternalServerError(#[key("awwa")] String), // 500, body = {"awwa": STRING}
}
lib.rs
:
Easily create axum::http::Response's from Enums! MSRV: 1.65.0
Example Usage
#[derive(axum_enum_response::EnumIntoResponse)]
enum ErrorResponse {
#[status_code(UNAUTHORIZED)]
Unauthorized, // 401, empty body
#[status_code(OK)]
#[body("hello"=>"world")]
Ok, // 200, body = {"hello": "world"}
#[status_code(FORBIDDEN)]
#[body("mew")]
Forbidden, // 403, body = {"error": "mew"}
#[status_code(INTERNAL_SERVER_ERROR)]
FromUtf8Error(#[from] FromUtf8Error), // 500, body = {"error": FromUtf8Error::to_string()}
#[status_code(INTERNAL_SERVER_ERROR)]
InternalServerError(#[key("awwa")] String), // 500, body = {"awwa": STRING}
}
You can also use any struct that implements serde::Serialize
as a field like this:
#[derive(serde::Serialize)]
struct SomeData {
meow: String,
}
#[derive(axum_enum_response::EnumIntoResponse)]
enum ErrorResponse {
#[status_code(BAD_REQUEST)]
BadRequest(SomeData), // 400, body = {"meow": STRING}
}
Dependencies
~215–650KB
~16K SLoC