Skip to content

Commit 838d130

Browse files
softpropsdavidbarsky
authored andcommitted
Add parity to the lambda! macro to support handler closure impl (#44)
1 parent 3b7ce41 commit 838d130

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

lambda-http/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@
2525
//! ))
2626
//! }
2727
//! ```
28+
//!
29+
//! You can also provide a closure directly to the `lambda!` macro
30+
//!
31+
//! ```rust,no_run
32+
//! use lambda_http::{lambda, Request, RequestExt};
33+
//!
34+
//! fn main() {
35+
//! lambda!(
36+
//! |request: Request, context| Ok(
37+
//! format!(
38+
//! "hello {}",
39+
//! request.query_string_parameters()
40+
//! .get("name")
41+
//! .unwrap_or_else(|| "stranger")
42+
//! )
43+
//! )
44+
//! )
45+
//! }
46+
//! ```
2847
2948
pub use http::{self, Response};
3049
use lambda_runtime::{self as lambda, error::HandlerError, Context};
@@ -87,6 +106,12 @@ where
87106
/// A macro for starting new handler's poll for API Gateway events
88107
#[macro_export]
89108
macro_rules! lambda {
109+
($handler:expr) => {
110+
$crate::start($handler, None)
111+
};
112+
($handler:expr, $runtime:expr) => {
113+
$crate::start($handler, Some($runtime))
114+
};
90115
($handler:ident) => {
91116
$crate::start($handler, None)
92117
};

lambda-runtime/src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,42 @@
3939
//! })
4040
//! }
4141
//! ```
42+
//!
43+
//! You can also provide a closure directly to the `lambda!` macro
44+
//!
45+
//! ```rust,no_run
46+
//! #[macro_use]
47+
//! extern crate serde_derive;
48+
//! #[macro_use]
49+
//! extern crate lambda_runtime;
50+
//!
51+
//! use lambda_runtime::{Context, error::HandlerError};
52+
//!
53+
//!
54+
//! #[derive(Deserialize, Clone)]
55+
//! struct CustomEvent {
56+
//! first_name: String,
57+
//! last_name: String,
58+
//! }
59+
//!
60+
//! #[derive(Serialize, Clone)]
61+
//! struct CustomOutput {
62+
//! message: String,
63+
//! }
64+
//!
65+
//! fn main() {
66+
//! lambda!(
67+
//! |e: CustomEvent, ctx: Context| {
68+
//! if e.first_name == "" {
69+
//! return Err(ctx.new_error("Missing first name!"));
70+
//! }
71+
//! Ok(CustomOutput{
72+
//! message: format!("Hello, {}!", e.first_name),
73+
//! })
74+
//! }
75+
//! );
76+
//! }
77+
//! ```
4278
#[macro_use]
4379
extern crate log;
4480

lambda-runtime/src/runtime.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ macro_rules! lambda {
5050
($handler:ident, $runtime:expr) => {
5151
$crate::start($handler, Some($runtime))
5252
};
53+
($handler:expr) => {
54+
$crate::start($handler, None)
55+
};
56+
($handler:expr, $runtime:expr) => {
57+
$crate::start($handler, Some($runtime))
58+
};
5359
}
5460

5561
/// Internal implementation of the start method that receives a config provider. This method

0 commit comments

Comments
 (0)