Macro crowbar::lambda [] [src]

macro_rules! lambda {
    (@module ($module:ident, $py2:ident, $py3:ident)
     @handlers ($($handler:expr => $target:expr),*)) => { ... };
    (crate $module:tt { $($handler:expr => $target:expr),* }) => { ... };
    (crate $module:tt { $($handler:expr => $target:expr,)* }) => { ... };
    ($($handler:expr => $target:expr),*) => { ... };
    ($($handler:expr => $target:expr,)*) => { ... };
    ($f:expr) => { ... };
}

Macro to wrap a Lambda function handler.

Lambda functions accept two arguments (the event, a serde_json Value, and the context, a LambdaContext) and returns a value (a serde_json Value). The function signature should look like:

Be careful when using this code, it's not being tested!
fn handler(event: Value, context: LambdaContext) -> LambdaResult

To use this macro, you need to macro_use both crowbar and cpython, because crowbar references multiple cpython macros.

Be careful when using this code, it's not being tested!
#[macro_use(lambda)]
extern crate crowbar;
#[macro_use]
extern crate cpython;

Examples

You can wrap a closure with lambda!:

lambda!(|event, context| {
    println!("hello!");
    Ok(event)
});

You can also define a named function:

use crowbar::{Value, LambdaContext, LambdaResult};

fn my_handler(event: Value, context: LambdaContext) -> LambdaResult {
    println!("hello!");
    Ok(event)
}

lambda!(my_handler);

Multiple handlers

You can define multiple handlers in the same module in a way similar to match:

lambda! {
    "one" => |event, context| { Ok("one") },
    "two" => |event, context| { Ok(2) },
};

Changing the dynamic library name

If you need to change the name of the built dynamic library, you first need to change the [lib] section in Cargo.toml:

[lib]
name = "kappa"
crate-type = ["cdylib"]

You then also need to change the names of the library symbols, which you can do by extending upon the multiple handler version of lambda!:

lambda! {
    crate (libkappa, initlibkappa, PyInit_libkappa) {
        "handler" => |event, context| { Ok("hi from libkappa") }
    }
};