Skip to content

Commit b1a8b87

Browse files
dglsparsonsecklf
andauthored
Change impl IntoResponse into Response<Body> (#1)
* Rework to allow merging api endpoints * Rework all examples and simplify structure * remove `IntoResponse` --------- Co-authored-by: Florentin / 珞辰 <[email protected]>
1 parent 9a65014 commit b1a8b87

File tree

19 files changed

+73
-139
lines changed

19 files changed

+73
-139
lines changed

examples/cron/Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/cron/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ serde = { version = "1.0.145", features = ["derive"] }
1212
serde_json = { version = "1.0.86", features = ["raw_value"] }
1313
serde_derive = "1.0.9"
1414
rand = "0.8.5"
15-
vercel_runtime = "0.1.4"
15+
vercel_runtime = { version = "0.1.4", path = "../../vercel_runtime" }
1616
slack-morphism = { version = "1.2.2", features = ["hyper"] }
1717

1818
[[bin]]

examples/cron/api/cron.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use slack_morphism::{errors::SlackClientError, prelude::*};
2-
use vercel_runtime::{
3-
lambda_http::{http::StatusCode, Response},
4-
run, Error, IntoResponse, Request,
5-
};
2+
use vercel_runtime::{run, Body, Error, Request, Response, StatusCode};
63

74
#[derive(Debug, Clone)]
85
pub struct SlackMessage {}
@@ -31,12 +28,12 @@ impl<T: SlackClientHttpConnector + Send + Sync> Lambda<'_, T> {
3128
self.slack.chat_post_message(&post_chat_req).await
3229
}
3330

34-
pub async fn handler(&self, _req: Request) -> Result<impl IntoResponse, Error> {
31+
pub async fn handler(&self, _req: Request) -> Result<Response<Body>, Error> {
3532
let message = SlackMessage {};
3633

3734
self.post_message(&message, "#general").await?;
3835

39-
let response = Response::builder().status(StatusCode::OK).body(())?;
36+
let response = Response::builder().status(StatusCode::OK).body(().into())?;
4037
Ok(response)
4138
}
4239
}

examples/merged/Cargo.lock

-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/merged/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ serde = { version = "1.0.145", features = ["derive"] }
1111
serde_json = { version = "1.0.86", features = ["raw_value"] }
1212
serde_derive = "1.0.9"
1313
rand = "0.8.5"
14-
vercel_runtime = "0.1.4"
14+
vercel_runtime = { version = "0.1.4", path = "../../vercel_runtime" }
1515
url = "2.3.1"
1616

1717
[lib]

examples/merged/api/bar/baz.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
use runtime_demo::choose_starter;
22
use serde_json::json;
3-
use vercel_runtime::{
4-
lambda_http::{http::StatusCode, Response},
5-
Error, IntoResponse, Request,
6-
};
3+
use vercel_runtime::{Body, Error, Request, Response, StatusCode};
74

8-
pub async fn handler(_req: Request) -> Result<impl IntoResponse, Error> {
5+
pub async fn handler(_req: Request) -> Result<Response<Body>, Error> {
96
dbg!(_req);
107
let starter = choose_starter();
118
let response = Response::builder()
@@ -15,7 +12,8 @@ pub async fn handler(_req: Request) -> Result<impl IntoResponse, Error> {
1512
json!({
1613
"message": format!("I choose you, {}!", starter),
1714
})
18-
.to_string(),
15+
.to_string()
16+
.into(),
1917
)?;
2018

2119
Ok(response)

examples/merged/api/foo.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
use runtime_demo::choose_starter;
22
use serde_json::json;
3-
use vercel_runtime::{
4-
lambda_http::{http::StatusCode, Response},
5-
Error, IntoResponse, Request,
6-
};
3+
use vercel_runtime::{Body, Error, Request, Response, StatusCode};
74

8-
pub async fn handler(_req: Request) -> Result<impl IntoResponse, Error> {
5+
pub async fn handler(_req: Request) -> Result<Response<Body>, Error> {
96
dbg!(_req);
107
let starter = choose_starter();
118
let response = Response::builder()
@@ -15,7 +12,8 @@ pub async fn handler(_req: Request) -> Result<impl IntoResponse, Error> {
1512
json!({
1613
"message": format!("I choose you, {}!", starter),
1714
})
18-
.to_string(),
15+
.to_string()
16+
.into(),
1917
)?;
2018

2119
Ok(response)

examples/merged/api/index.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use vercel_runtime::{run, Error, IntoResponse, Request};
1+
use vercel_runtime::{run, Body, Error, Request, Response};
22

33
#[path = "../api/bar/baz.rs"]
44
mod api_bar_baz;
55

66
#[path = "../api/foo.rs"]
77
mod api_foo;
88

9-
async fn process_request(request: Request) -> Result<impl IntoResponse, Error> {
9+
async fn process_request(request: Request) -> Result<Response<Body>, Error> {
1010
let path = request.uri().path();
1111

1212
match path {

examples/nextjs/Cargo.lock

+3-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/nextjs/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ serde = { version = "1.0.145", features = ["derive"] }
1212
serde_json = { version = "1.0.86", features = ["raw_value"] }
1313
serde_derive = "1.0.9"
1414
rand = "0.8.5"
15-
vercel_runtime = "0.1.4"
15+
vercel_runtime = { version = "0.1.4", path = "../../vercel_runtime" }
1616
oorandom = "11.1.3"
1717

1818
[[bin]]

examples/nextjs/api/rust.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
use serde_json::json;
22
use std::time::Instant;
3-
use vercel_runtime::{
4-
lambda_http::{http::StatusCode, Error as LambdaError, Response},
5-
run, Error, IntoResponse, Request,
6-
};
3+
use vercel_runtime::{run, Body, Error, Request, Response, StatusCode};
74

85
#[tokio::main]
9-
async fn main() -> Result<(), LambdaError> {
6+
async fn main() -> Result<(), Error> {
107
run(handler).await
118
}
129

13-
pub async fn handler(_req: Request) -> Result<impl IntoResponse, Error> {
10+
pub async fn handler(_req: Request) -> Result<Response<Body>, Error> {
1411
let start = Instant::now();
1512

1613
let seed = 42;
@@ -44,7 +41,7 @@ pub async fn handler(_req: Request) -> Result<impl IntoResponse, Error> {
4441
"time": format!("{:.2?}", duration),
4542
"pi": pi
4643
})
47-
.to_string(),
44+
.into(),
4845
)?;
4946

5047
Ok(response)

examples/simple/Cargo.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/simple/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ serde = { version = "1.0.145", features = ["derive"] }
1111
serde_json = { version = "1.0.86", features = ["raw_value"] }
1212
serde_derive = "1.0.9"
1313
rand = "0.8.5"
14-
vercel_runtime = "0.1.4"
14+
vercel_runtime = { version = "0.1.4", path = "../../vercel_runtime" }
1515

1616
[lib]
1717
path = "src-rs/lib.rs"

examples/simple/api/complex.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use runtime_demo::choose_starter;
22
use serde_json::json;
33
use vercel_runtime::{
4-
lambda_http::{http::StatusCode, service_fn, tower::ServiceBuilder, Response},
5-
lambda_runtime, process_request, process_response, Error, IntoResponse, Request,
4+
process_request, process_response, run_service, service_fn, Body, Error, Request, Response,
5+
ServiceBuilder, StatusCode,
66
};
77

88
#[tokio::main]
@@ -19,10 +19,10 @@ async fn main() -> Result<(), Error> {
1919
.map_response(process_response)
2020
.service(service_fn(handler));
2121

22-
lambda_runtime::run(handler).await
22+
run_service(handler).await
2323
}
2424

25-
pub async fn handler(_req: Request) -> Result<impl IntoResponse, Error> {
25+
pub async fn handler(_req: Request) -> Result<Response<Body>, Error> {
2626
tracing::info!("Choosing a starter Pokemon");
2727
let starter = choose_starter();
2828

@@ -33,7 +33,7 @@ pub async fn handler(_req: Request) -> Result<impl IntoResponse, Error> {
3333
json!({
3434
"message": format!("I choose you, {}!", starter),
3535
})
36-
.to_string(),
36+
.into(),
3737
)?;
3838

3939
Ok(response)

examples/simple/api/simple.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
use runtime_demo::choose_starter;
22
use serde_json::json;
3-
use vercel_runtime::{
4-
lambda_http::{http::StatusCode, Response},
5-
run, Error, IntoResponse, Request,
6-
};
3+
use vercel_runtime::{run, Body, Error, Request, Response, StatusCode};
74

85
#[tokio::main]
96
async fn main() -> Result<(), Error> {
107
run(handler).await
118
}
129

13-
pub async fn handler(_req: Request) -> Result<impl IntoResponse, Error> {
10+
pub async fn handler(_req: Request) -> Result<Response<Body>, Error> {
1411
let starter = choose_starter();
12+
1513
let response = Response::builder()
1614
.status(StatusCode::OK)
1715
.header("Content-Type", "application/json")
1816
.body(
1917
json!({
2018
"message": format!("I choose you, {}!", starter),
2119
})
22-
.to_string(),
20+
.into(),
2321
)?;
2422

2523
Ok(response)

vercel_runtime/src/body.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Provides a Vercel Lambda oriented request and response body entity interface
22
use base64::display::Base64Display;
33
use serde::ser::{Error as SerError, Serialize, Serializer};
4+
use serde_json::Value;
45
use std::{borrow::Cow, ops::Deref, str};
56

67
/// Representation of http request and response bodies as supported
@@ -52,22 +53,17 @@ use std::{borrow::Cow, ops::Deref, str};
5253
/// _ => false
5354
/// })
5455
/// ```
55-
#[derive(Debug, PartialEq)]
56+
#[derive(Default, Debug, PartialEq)]
5657
pub enum Body {
5758
/// An empty body
59+
#[default]
5860
Empty,
5961
/// A body containing string data
6062
Text(String),
6163
/// A body containing binary data
6264
Binary(Vec<u8>),
6365
}
6466

65-
impl Default for Body {
66-
fn default() -> Self {
67-
Body::Empty
68-
}
69-
}
70-
7167
impl From<()> for Body {
7268
fn from(_: ()) -> Self {
7369
Body::Empty
@@ -149,6 +145,12 @@ impl From<Body> for Vec<u8> {
149145
}
150146
}
151147

148+
impl From<Value> for Body {
149+
fn from(v: Value) -> Self {
150+
Body::Text(v.to_string())
151+
}
152+
}
153+
152154
impl<'a> From<&'a [u8]> for Body {
153155
fn from(b: &'a [u8]) -> Self {
154156
Body::Binary(b.to_vec())

vercel_runtime/src/lib.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
pub mod body;
2-
pub mod request;
3-
pub mod response;
4-
use body::Body;
5-
pub use lambda_http;
6-
use lambda_http::{service_fn, tower::ServiceBuilder};
7-
pub use lambda_runtime;
1+
mod body;
2+
mod request;
3+
mod response;
84
use lambda_runtime::LambdaEvent;
95
use request::{VercelEvent, VercelRequest};
10-
pub use response::IntoResponse;
11-
use response::VercelResponse;
6+
use response::EventResponse;
127
use std::future::Future;
138
use tracing::{debug, error};
149

1510
pub type Request = lambda_http::http::Request<Body>;
1611
pub type Error = lambda_http::Error;
12+
pub type Event<'a> = LambdaEvent<VercelEvent<'a>>;
13+
pub use body::Body;
14+
pub use lambda_http::http::StatusCode;
15+
pub use lambda_http::service_fn;
16+
pub use lambda_http::tower::ServiceBuilder;
17+
pub use lambda_http::Response;
18+
pub use lambda_runtime::run as run_service;
1719

18-
pub async fn run<T: FnMut(Request) -> F, F: Future<Output = Result<impl IntoResponse, Error>>>(
20+
pub async fn run<T: FnMut(Request) -> F, F: Future<Output = Result<Response<Body>, Error>>>(
1921
f: T,
2022
) -> Result<(), Error> {
2123
let handler = ServiceBuilder::new()
@@ -26,8 +28,8 @@ pub async fn run<T: FnMut(Request) -> F, F: Future<Output = Result<impl IntoResp
2628
lambda_runtime::run(handler).await
2729
}
2830

29-
pub fn process_request(lambda_event: LambdaEvent<VercelEvent>) -> lambda_http::http::Request<Body> {
30-
let (event, _context) = lambda_event.into_parts();
31+
pub fn process_request(event: Event) -> Request {
32+
let (event, _context) = event.into_parts();
3133
let parse_result = serde_json::from_str::<VercelRequest>(&event.body);
3234

3335
match parse_result {
@@ -44,6 +46,6 @@ pub fn process_request(lambda_event: LambdaEvent<VercelEvent>) -> lambda_http::h
4446
}
4547
}
4648

47-
pub fn process_response(response: impl IntoResponse) -> VercelResponse {
48-
VercelResponse::from(response.into_response())
49+
pub fn process_response(response: Response<Body>) -> EventResponse {
50+
EventResponse::from(response)
4951
}

vercel_runtime/src/request.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct VercelEvent<'a> {
2626
#[allow(dead_code)]
2727
#[serde(rename = "Action")]
2828
action: Cow<'a, str>,
29-
pub body: Cow<'a, str>,
29+
pub(crate) body: Cow<'a, str>,
3030
}
3131

3232
fn deserialize_method<'de, D>(deserializer: D) -> Result<Method, D::Error>

0 commit comments

Comments
 (0)