4 releases (2 breaking)
new 0.3.1 | Mar 21, 2025 |
---|---|
0.3.0 | Mar 7, 2025 |
0.2.1 | Dec 20, 2024 |
0.2.0 |
|
0.1.0 | Dec 20, 2024 |
#586 in Web programming
1,242 downloads per month
9KB
62 lines
IntoResponse
IntoResponse is a Rust crate that provides utilities for deriving and implementing the IntoResponse
trait for custom types. It offers a convenient way to convert your custom types into HTTP responses with minimal boilerplate.
Features
- Derive macro for the
IntoResponse
trait - Customizable response handling logic
- Support for common response types (e.g., JSON)
- Custom status code support using the
#[into_response(status = ...)]
attribute - Automatic serialization constraints for generic types
Usage
Add into_response
to your Cargo.toml
:
[dependencies]
into_response = "0.3"
Examples
Default Response
use into_response::IntoResponse;
#[derive(IntoResponse)]
struct MyResponse {
message: String,
}
fn main() {
let response = MyResponse {
message: "Hello, world!".to_string(),
};
// By default, the HTTP status is axum::http::StatusCode::OK.
let response = response.into_response();
assert_eq!(response.status(), axum::http::StatusCode::OK);
}
Custom Status Code
You can specify a custom HTTP status code using the #[into_response(status = ...)]
attribute:
use into_response::IntoResponse;
#[derive(IntoResponse)]
#[into_response(status = 201)]
struct MyResponse {
message: String,
}
fn main() {
let response = MyResponse {
message: "Created successfully".to_string(),
};
let response = response.into_response();
// The HTTP status will be axum::http::StatusCode::CREATED.
assert_eq!(response.status(), axum::http::StatusCode::CREATED);
}
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
Acknowledgements
Special thanks to the Rust community for their contributions and support.
Dependencies
~0.3–0.9MB
~20K SLoC