Skip to content

Commit ca7bd7f

Browse files
committed
Merge branch 'release/v2.0.0'
2 parents 9f80b7f + eddf1dd commit ca7bd7f

File tree

10 files changed

+51
-98
lines changed

10 files changed

+51
-98
lines changed

Cargo.toml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "routerify-json-response"
3-
version = "1.1.0"
3+
version = "2.0.0"
44
description = "A Routerify utility library to generate JSON response."
55
homepage = "https://github.com/routerify/routerify-json-response"
66
repository = "https://github.com/routerify/routerify-json-response"
@@ -12,11 +12,12 @@ license = "MIT"
1212
edition = "2018"
1313

1414
[dependencies]
15-
hyper = "0.13"
16-
routerify = "1.1"
17-
serde = { version = "1.0", features = ["derive"] }
18-
serde_json = "1.0"
15+
http-body = "0.4"
16+
http = "0.2"
17+
serde = { version = "1", features = ["derive"] }
18+
serde_json = "1"
1919

2020
[dev-dependencies]
21-
tokio = { version = "0.2", features = ["full"] }
22-
stream-body = "0.1"
21+
tokio = { version = "1", features = ["full"] }
22+
hyper = { version = "0.14", features = ["server"] }
23+
routerify = "2"

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ In `Failed` case, It generates JSON response in the following format:
3030
[Docs](https://docs.rs/routerify-json-response)
3131

3232
## Install
33-
33+
3434
Add this to your `Cargo.toml`:
3535

3636
```toml
3737
[dependencies]
38-
routerify = "1.1"
39-
routerify-json-response = "1.1"
38+
routerify = "2"
39+
routerify-json-response = "2"
4040
```
4141

4242
## Example
@@ -48,7 +48,7 @@ use routerify_json_response::{json_failed_resp_with_message, json_success_resp};
4848
use routerify::{Router, RouterService};
4949
use std::net::SocketAddr;
5050

51-
async fn list_users_handler(_: Request<Body>) -> Result<Response<Body>, routerify::Error> {
51+
async fn list_users_handler(_: Request<Body>) -> Result<Response<Body>, routerify_json_response::Error> {
5252
// Fetch response data from somewhere.
5353
let users = ["Alice", "John"];
5454

@@ -57,7 +57,7 @@ async fn list_users_handler(_: Request<Body>) -> Result<Response<Body>, routerif
5757
json_success_resp(&users)
5858
}
5959

60-
async fn list_books_handler(_: Request<Body>) -> Result<Response<Body>, routerify::Error> {
60+
async fn list_books_handler(_: Request<Body>) -> Result<Response<Body>, routerify_json_response::Error> {
6161
// Generate a failed JSON response in the following format:
6262
// { "status": "failed", code: 500, data: "Internal Server Error: Couldn't fetch book list from database" }
6363
json_failed_resp_with_message(
@@ -67,7 +67,7 @@ async fn list_books_handler(_: Request<Body>) -> Result<Response<Body>, routerif
6767
}
6868

6969
// Create a router.
70-
fn router() -> Router<Body, routerify::Error> {
70+
fn router() -> Router<Body, routerify_json_response::Error> {
7171
Router::builder()
7272
// Attach the handlers.
7373
.get("/users", list_users_handler)

examples/example.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use routerify::{Router, RouterService};
44
use routerify_json_response::{json_failed_resp_with_message, json_success_resp};
55
use std::net::SocketAddr;
66

7-
async fn list_users_handler(_: Request<Body>) -> Result<Response<Body>, routerify::Error> {
7+
async fn list_users_handler(_: Request<Body>) -> routerify_json_response::Result<Response<Body>> {
88
// Fetch response data from somewhere.
99
let users = ["Alice", "John"];
1010

@@ -13,7 +13,7 @@ async fn list_users_handler(_: Request<Body>) -> Result<Response<Body>, routerif
1313
json_success_resp(&users)
1414
}
1515

16-
async fn list_books_handler(_: Request<Body>) -> Result<Response<Body>, routerify::Error> {
16+
async fn list_books_handler(_: Request<Body>) -> routerify_json_response::Result<Response<Body>> {
1717
// Generate a failed JSON response in the following format:
1818
// { "status": "failed", code: 500, data: "Internal Server Error: Couldn't fetch book list from database" }
1919
json_failed_resp_with_message(
@@ -23,7 +23,7 @@ async fn list_books_handler(_: Request<Body>) -> Result<Response<Body>, routerif
2323
}
2424

2525
// Create a router.
26-
fn router() -> Router<Body, routerify::Error> {
26+
fn router() -> Router<Body, routerify_json_response::Error> {
2727
Router::builder()
2828
// Attach the handlers.
2929
.get("/users", list_users_handler)

examples/test.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.

examples/test_stream_body.rs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use std::error::Error as StdError;
2+
3+
pub type Error = Box<dyn StdError + Send + Sync + 'static>;

src/failed_resp.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::gen_resp::gen_response;
2-
use hyper::{body::HttpBody, Response, StatusCode};
2+
use http::{Response, StatusCode};
3+
use http_body::Body as HttpBody;
34
use serde::Serialize;
45

56
const STATUS_FAILED: &'static str = "failed";
@@ -30,7 +31,7 @@ struct FailedResp {
3031
/// use hyper::{Body, Request, Response, StatusCode};
3132
/// use routerify_json_response::{json_failed_resp_with_message};
3233
///
33-
/// async fn list_books_handler(_: Request<Body>) -> Result<Response<Body>, routerify::Error> {
34+
/// async fn list_books_handler(_: Request<Body>) -> Result<Response<Body>, routerify_json_response::Error> {
3435
/// // Generate a failed JSON response in the following format:
3536
/// // { "status": "failed", code: 500, data: "Internal Server Error: Couldn't fetch book list from database" }
3637
/// json_failed_resp_with_message(
@@ -39,7 +40,7 @@ struct FailedResp {
3940
/// )
4041
/// }
4142
/// ```
42-
pub fn json_failed_resp_with_message<B, M>(code: StatusCode, message: M) -> routerify::Result<Response<B>>
43+
pub fn json_failed_resp_with_message<B, M>(code: StatusCode, message: M) -> crate::Result<Response<B>>
4344
where
4445
B: HttpBody + From<Vec<u8>> + Send + Sync + Unpin + 'static,
4546
M: Into<String>,
@@ -71,13 +72,13 @@ where
7172
/// use hyper::{Body, Request, Response, StatusCode};
7273
/// use routerify_json_response::{json_failed_resp};
7374
///
74-
/// async fn list_books_handler(_: Request<Body>) -> Result<Response<Body>, routerify::Error> {
75+
/// async fn list_books_handler(_: Request<Body>) -> Result<Response<Body>, routerify_json_response::Error> {
7576
/// // Generate a failed JSON response in the following format:
7677
/// // { "status": "failed", code: 500, data: "Internal Server Error" }
7778
/// json_failed_resp(StatusCode::INTERNAL_SERVER_ERROR)
7879
/// }
7980
/// ```
80-
pub fn json_failed_resp<B>(code: StatusCode) -> routerify::Result<Response<B>>
81+
pub fn json_failed_resp<B>(code: StatusCode) -> crate::Result<Response<B>>
8182
where
8283
B: HttpBody + From<Vec<u8>> + Send + Sync + Unpin + 'static,
8384
{

src/gen_resp.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
use hyper::{body::HttpBody, header, Response, StatusCode};
1+
use http::{header, Response, StatusCode};
2+
use http_body::Body as HttpBody;
23
use serde::Serialize;
34

4-
pub(crate) fn gen_response<B, D>(code: StatusCode, resp_data: &D) -> routerify::Result<Response<B>>
5+
pub(crate) fn gen_response<B, D>(code: StatusCode, resp_data: &D) -> crate::Result<Response<B>>
56
where
67
B: HttpBody + From<Vec<u8>> + Send + Sync + Unpin + 'static,
78
D: Serialize + Send + Sync + Unpin,
89
{
910
let json_resp_data = match serde_json::to_vec(&resp_data) {
1011
Ok(json_data) => json_data,
1112
Err(err) => {
12-
return Err(routerify::Error::new(format!(
13-
"routerify-json-response: Failed to convert the response data as JSON: {}",
14-
err
13+
return Err(Box::new(std::io::Error::new(
14+
std::io::ErrorKind::Other,
15+
format!(
16+
"routerify-json-response: Failed to convert the response data as JSON: {}",
17+
err
18+
),
1519
)));
1620
}
1721
};
@@ -27,9 +31,9 @@ where
2731

2832
match resp {
2933
Ok(resp) => Ok(resp),
30-
Err(err) => Err(routerify::Error::new(format!(
31-
"routerify-json-response: Failed to create response: {}",
32-
err
34+
Err(err) => Err(Box::new(std::io::Error::new(
35+
std::io::ErrorKind::Other,
36+
format!("routerify-json-response: Failed to create response: {}", err),
3337
))),
3438
}
3539
}

src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
//! use routerify::{Router, RouterService};
3030
//! use std::net::SocketAddr;
3131
//!
32-
//! async fn list_users_handler(_: Request<Body>) -> Result<Response<Body>, routerify::Error> {
32+
//! async fn list_users_handler(_: Request<Body>) -> Result<Response<Body>, routerify_json_response::Error> {
3333
//! // Fetch response data from somewhere.
3434
//! let users = ["Alice", "John"];
3535
//!
@@ -38,7 +38,7 @@
3838
//! json_success_resp(&users)
3939
//! }
4040
//!
41-
//! async fn list_books_handler(_: Request<Body>) -> Result<Response<Body>, routerify::Error> {
41+
//! async fn list_books_handler(_: Request<Body>) -> Result<Response<Body>, routerify_json_response::Error> {
4242
//! // Generate a failed JSON response in the following format:
4343
//! // { "status": "failed", code: 500, data: "Internal Server Error: Couldn't fetch book list from database" }
4444
//! json_failed_resp_with_message(
@@ -48,7 +48,7 @@
4848
//! }
4949
//!
5050
//! // Create a router.
51-
//! fn router() -> Router<Body, routerify::Error> {
51+
//! fn router() -> Router<Body, routerify_json_response::Error> {
5252
//! Router::builder()
5353
//! // Attach the handlers.
5454
//! .get("/users", list_users_handler)
@@ -77,9 +77,13 @@
7777
//! }
7878
//! ```
7979
80+
pub use error::Error;
8081
pub use failed_resp::{json_failed_resp, json_failed_resp_with_message};
8182
pub use success_resp::{json_success_resp, json_success_resp_with_code};
8283

84+
mod error;
8385
mod failed_resp;
8486
mod gen_resp;
8587
mod success_resp;
88+
89+
pub type Result<T> = std::result::Result<T, Error>;

src/success_resp.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::gen_resp::gen_response;
2-
use hyper::{body::HttpBody, Response, StatusCode};
2+
use http::{Response, StatusCode};
3+
use http_body::Body as HttpBody;
34
use serde::Serialize;
45

56
const STATUS_SUCCESS: &'static str = "success";
@@ -33,7 +34,7 @@ where
3334
/// use hyper::{Body, Request, Response, StatusCode};
3435
/// use routerify_json_response::{json_success_resp_with_code};
3536
///
36-
/// async fn list_users_handler(_: Request<Body>) -> Result<Response<Body>, routerify::Error> {
37+
/// async fn list_users_handler(_: Request<Body>) -> Result<Response<Body>, routerify_json_response::Error> {
3738
/// // Fetch response data from somewhere.
3839
/// let users = ["Alice", "John"];
3940
///
@@ -42,7 +43,7 @@ where
4243
/// json_success_resp_with_code(StatusCode::CREATED, &users)
4344
/// }
4445
/// ```
45-
pub fn json_success_resp_with_code<B, D>(code: StatusCode, data: &D) -> routerify::Result<Response<B>>
46+
pub fn json_success_resp_with_code<B, D>(code: StatusCode, data: &D) -> crate::Result<Response<B>>
4647
where
4748
B: HttpBody + From<Vec<u8>> + Send + Sync + Unpin + 'static,
4849
D: Serialize + Send + Sync + Unpin,
@@ -74,7 +75,7 @@ where
7475
/// use hyper::{Body, Request, Response, StatusCode};
7576
/// use routerify_json_response::{json_success_resp};
7677
///
77-
/// async fn list_users_handler(_: Request<Body>) -> Result<Response<Body>, routerify::Error> {
78+
/// async fn list_users_handler(_: Request<Body>) -> Result<Response<Body>, routerify_json_response::Error> {
7879
/// // Fetch response data from somewhere.
7980
/// let users = ["Alice", "John"];
8081
///
@@ -83,7 +84,7 @@ where
8384
/// json_success_resp(&users)
8485
/// }
8586
/// ```
86-
pub fn json_success_resp<B, D>(data: &D) -> routerify::Result<Response<B>>
87+
pub fn json_success_resp<B, D>(data: &D) -> crate::Result<Response<B>>
8788
where
8889
B: HttpBody + From<Vec<u8>> + Send + Sync + Unpin + 'static,
8990
D: Serialize + Send + Sync + Unpin,

0 commit comments

Comments
 (0)