# Rust Functions

## Dependencies

An AssemblyLift function written in Rust is a [library crate](https://learning-rust.github.io/docs/a4.cargo,crates_and_basic_project_structure.html). It requires several crates to be imported, which provide the necessary plumbing to make our Rust crate an AssemblyLift function.&#x20;

When making a new function via `asml make` the generated `Cargo.toml` should have included these crates. However, you should also ensure that you are using the latest patch version of each.

{% code title="Cargo.toml" %}

```
[dependencies]
asml_core = { version = "0.2", package = "assemblylift-core-guest" }
assemblylift_core_io_guest = { version = "0.3", package = "assemblylift-core-io-guest" }
asml_awslambda = { version = "0.3", package = "assemblylift-awslambda-guest" }
```

{% endcode %}

The *core* crate provides the `GuestCore` trait, which defines an interface for communicating with the cloud runtime (logging, low-level success/error response, etc). This trait is implemented by `AwsLambdaClient` which is provided by the *awslambda* crate.

The *core-io* crate provides the IO system; this is AssemblyLift's `Future` execution system supporting async/await.

## Handler Definition

The *awslambda* crate provides a macro called `handler!` which wraps up all the details of initializing the module, and provides a concise entry point for our function.

The `handler!` macro exports a function called `handler` which provides the entry-point from the runtime host.&#x20;

{% hint style="warning" %}
Only one handler can be defined per function. Calling the handler! macro more than once will produce a compiler error.
{% endhint %}

{% code title="lib.rs" %}

```rust
extern crate asml_awslambda;

use asml_core::GuestCore;
use asml_awslambda::{AwsLambdaClient, LambdaContext};

// Input must implement serde Deserialize
type Input = ();

handler!(context: LambdaContext<Input>, async {
    // function code
    // supports .await
})
```

{% endcode %}

The `extern crate` statement is required to bring `static`s & `extern`s into scope which are defined in the crate.

The `context` value is accessible from within the function closure, and provides access to details of the function invocation, include the input payload and authorization data.

## Writing an HTTP Function

If your function has an HTTP API, your function input and output must be compatible with the providers' API Gateway (Amazon API Gateway by default).

The crate [`assemblylift_awslambda_guest`](https://docs.rs/assemblylift-awslambda-guest/0.3.0/assemblylift_awslambda_guest/) includes structs & macros for working with Amazon API Gateway.

{% code title="lib.rs" %}

```rust
extern crate asml_awslambda;

use asml_core::GuestCore;
use asml_awslambda::{ApiGatewayEvent, AwsLambdaClient, LambdaContext};

handler!(context: LambdaContext<ApiGatewayEvent>, async {
    let event: ApiGatewayEvent = context.event;
    
    // Do something with the event
    
    if (function_success) {
        let response = "OK"; // This can be any serde::Serialize-able value
        http_ok!(response);
    } else {
        http_error!("There was an error!");
    }
})
```

{% endcode %}
