1717//! your function's execution path.
1818//!
1919//! ```rust,no_run
20- //! use lambda_http::{handler, lambda_runtime::{self, Error} };
20+ //! use lambda_http::{service_fn, Error};
2121//!
2222//! #[tokio::main]
2323//! async fn main() -> Result<(), Error> {
2424//! // initialize dependencies once here for the lifetime of your
2525//! // lambda task
26- //! lambda_runtime ::run(handler (|request, context | async { Ok("👋 world!") })).await?;
26+ //! lambda_http ::run(service_fn (|request| async { Ok("👋 world!") })).await?;
2727//! Ok(())
2828//! }
2929//! ```
3434//! with the [`RequestExt`](trait.RequestExt.html) trait.
3535//!
3636//! ```rust,no_run
37- //! use lambda_http::{handler, lambda_runtime::{self, Context, Error} , IntoResponse, Request, RequestExt};
37+ //! use lambda_http::{service_fn, Error, IntoResponse, Request, RequestExt};
3838//!
3939//! #[tokio::main]
4040//! async fn main() -> Result<(), Error> {
41- //! lambda_runtime ::run(handler (hello)).await?;
41+ //! lambda_http ::run(service_fn (hello)).await?;
4242//! Ok(())
4343//! }
4444//!
4545//! async fn hello(
46- //! request: Request,
47- //! _: Context
46+ //! request: Request
4847//! ) -> Result<impl IntoResponse, Error> {
4948//! Ok(format!(
5049//! "hello {}",
6261extern crate maplit;
6362
6463pub use http:: { self , Response } ;
65- pub use lambda_runtime:: { self , Context } ;
66- use lambda_runtime:: {
67- tower:: util:: { service_fn, ServiceFn } ,
68- Error , LambdaEvent , Service ,
69- } ;
64+ pub use lambda_runtime:: { self , tower:: util:: service_fn, Context , Error } ;
65+ use lambda_runtime:: { LambdaEvent , Service } ;
7066
7167mod body;
7268pub mod ext;
@@ -88,27 +84,10 @@ use std::{
8884/// Type alias for `http::Request`s with a fixed [`Body`](enum.Body.html) type
8985pub type Request = http:: Request < Body > ;
9086
91- /// Wraps a function that takes 2 arguments into one that only takes a [`LambdaEvent`]
92- fn handler_wrapper < A , Fut > ( f : impl Fn ( A , Context ) -> Fut ) -> impl Fn ( LambdaEvent < A > ) -> Fut {
93- move |req| f ( req. event , req. context )
94- }
95-
96- /// Adapts a [`Service`] into another [`Service`].
97- pub fn handler < ' a , R , Fut > (
98- f : impl Fn ( Request , Context ) -> Fut ,
99- ) -> Adapter < ' a , R , ServiceFn < impl Fn ( LambdaEvent < Request > ) -> Fut > >
100- where
101- R : IntoResponse ,
102- Fut : Future < Output = Result < R , Error > > ,
103- {
104- Adapter {
105- service : service_fn ( handler_wrapper ( f) ) ,
106- _phantom_data : PhantomData ,
107- }
108- }
109-
110- #[ doc( hidden) ]
111- pub struct TransformResponse < ' a , R , E > {
87+ /// Future that will convert an [`IntoResponse`] into an actual [`LambdaResponse`]
88+ ///
89+ /// This is used by the `Adapter` wrapper and is completely internal to the `lambda_http::run` function.
90+ struct TransformResponse < ' a , R , E > {
11291 request_origin : RequestOrigin ,
11392 fut : Pin < Box < dyn Future < Output = Result < R , E > > + Send + ' a > > ,
11493}
@@ -129,15 +108,31 @@ where
129108 }
130109}
131110
132- #[ doc( hidden) ]
133- pub struct Adapter < ' a , R , S > {
111+ /// Wraps a `Service<Request>` in a `Service<LambdaEvent<Request>>`
112+ ///
113+ /// This is completely internal to the `lambda_http::run` function.
114+ struct Adapter < ' a , R , S > {
134115 service : S ,
135116 _phantom_data : PhantomData < & ' a R > ,
136117}
137118
119+ impl < ' a , R , S > From < S > for Adapter < ' a , R , S >
120+ where
121+ S : Service < Request , Response = R , Error = Error > + Send ,
122+ S :: Future : Send + ' a ,
123+ R : IntoResponse ,
124+ {
125+ fn from ( service : S ) -> Self {
126+ Adapter {
127+ service,
128+ _phantom_data : PhantomData ,
129+ }
130+ }
131+ }
132+
138133impl < ' a , R , S > Service < LambdaEvent < LambdaRequest < ' a > > > for Adapter < ' a , R , S >
139134where
140- S : Service < LambdaEvent < Request > , Response = R , Error = Error > + Send ,
135+ S : Service < Request , Response = R , Error = Error > + Send ,
141136 S :: Future : Send + ' a ,
142137 R : IntoResponse ,
143138{
@@ -151,7 +146,22 @@ where
151146
152147 fn call ( & mut self , req : LambdaEvent < LambdaRequest < ' a > > ) -> Self :: Future {
153148 let request_origin = req. event . request_origin ( ) ;
154- let fut = Box :: pin ( self . service . call ( LambdaEvent :: new ( req. event . into ( ) , req. context ) ) ) ;
149+ let event: Request = req. event . into ( ) ;
150+ let fut = Box :: pin ( self . service . call ( event. with_lambda_context ( req. context ) ) ) ;
155151 TransformResponse { request_origin, fut }
156152 }
157153}
154+
155+ /// Starts the Lambda Rust runtime and begins polling for events on the [Lambda
156+ /// Runtime APIs](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html).
157+ ///
158+ /// This takes care of transforming the LambdaEvent into a [`Request`] and then
159+ /// converting the result into a [`LambdaResponse`].
160+ pub async fn run < ' a , S , R > ( handler : S ) -> Result < ( ) , Error >
161+ where
162+ S : Service < Request , Response = R , Error = Error > + Send ,
163+ S :: Future : Send + ' a ,
164+ R : IntoResponse ,
165+ {
166+ lambda_runtime:: run ( Adapter :: from ( handler) ) . await
167+ }
0 commit comments