Skip to content

Commit a1551d5

Browse files
author
David Barsky
committed
Introduce Skeptic for testing the README.md
This change introduces: - Skeptic (https://github.com/budziq/rust-skeptic) for testing the README.md - Remove #[macro_use], allowing for macros to be imported without any special syntax. - Introduce a minimum Rust version of 1.30 for examples. Signed-off-by: David Barsky <[email protected]>
1 parent ee15e6a commit a1551d5

File tree

9 files changed

+189
-50
lines changed

9 files changed

+189
-50
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target
2+
lambda-runtime/libtest.rmeta

Cargo.lock

Lines changed: 106 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
members = [
33
"lambda-runtime-client",
44
"lambda-runtime"
5-
]
5+
]

README.md

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
1-
# Rust runtime for AWS Lambda
1+
# Rust Runtime for AWS Lambda
22

3-
This package makes it easy to run AWS Lambda Functions written in Rust. This workspace includes multiple crates:
4-
* **`lambda-runtime-client`** is a client SDK for the Lambda runtime APIs
5-
* **`lambda-runtime`** is a library that makes it easy to write Lambda functions in rust as executables
3+
This package makes it easy to run AWS Lambda Functions written in Rust. This workspace includes two crates:
4+
5+
* **`lambda-runtime-client`** is a client SDK for the Lambda Runtime APIs. You probably don't need to use this crate directly!
6+
* **`lambda-runtime`** is a library that makes it easy to write Lambda functions in Rust.
67

78
## Example function
8-
The code below creates a simple function that receives an event with a `firstName` property and returns a hello world message for the given first name.
99

10-
```rust
11-
#[macro_use]
10+
The code below creates a simple function that receives an event with a `greeting` and `name` field and returns a `GreetingResponse` message for the given name and greeting. Notice: to run these examples, we require a minimum Rust version of 1.30.
11+
12+
```rust,no_run
1213
extern crate lambda_runtime as lambda;
13-
#[macro_use]
1414
extern crate serde_derive;
15-
#[macro_use]
1615
extern crate log;
1716
extern crate simple_logger;
1817
19-
use lambda::error::HandlerError;
20-
18+
use serde_derive::{Serialize, Deserialize};
19+
use lambda::{lambda, Context, error::HandlerError};
20+
use log::error;
2121
use std::error::Error;
2222
23-
#[derive(Deserialize, Clone)]
24-
struct CustomEvent {
25-
#[serde(rename = "firstName")]
26-
first_name: String,
23+
#[derive(Serialize, Deserialize)]
24+
struct GreetingEvent {
25+
greeting: String,
26+
name: String,
2727
}
2828
29-
#[derive(Serialize, Clone)]
30-
struct CustomOutput {
29+
#[derive(Serialize, Deserialize)]
30+
struct GreetingResponse {
3131
message: String,
3232
}
3333
3434
fn main() -> Result<(), Box<dyn Error>> {
35-
simple_logger::init_with_level(log::Level::Info)?;
35+
simple_logger::init_with_level(log::Level::Debug).unwrap();
3636
lambda!(my_handler);
3737
3838
Ok(())
3939
}
4040
41-
fn my_handler(e: CustomEvent, c: lambda::Context) -> Result<CustomOutput, HandlerError> {
42-
if e.first_name == "" {
43-
error!("Empty first name in request {}", c.aws_request_id);
44-
return Err(c.new_error("Empty first name"));
41+
fn my_handler(event: GreetingEvent, ctx: Context) -> Result<GreetingResponse, HandlerError> {
42+
if event.name == "" {
43+
error!("Empty name in request {}", ctx.aws_request_id);
44+
return Err(ctx.new_error("Empty name"));
4545
}
4646
47-
Ok(CustomOutput {
48-
message: format!("Hello, {}!", e.first_name),
47+
Ok(GreetingResponse {
48+
message: format!("{}, {}!", event.greeting, event.name),
4949
})
5050
}
5151
```
5252

53-
The code above is the same as the [basic example](https://github.com/awslabs/aws-lambda-rust-runtime/tree/master/lambda-runtime/examples/basic.rs) in the `lambda-runtime` crate. To deploy the basic sample as a Lambda function, we first build it with `cargo`. Remember that AWS Lambda uses Amazon Linux so you need to target your executable for an `x86_64-linux` platform.
53+
The code above is the same as the [basic example](https://github.com/awslabs/aws-lambda-rust-runtime/tree/master/lambda-runtime/examples/basic.rs) in the `lambda-runtime` crate. To deploy the basic sample as a Lambda function, we first build it with `cargo`. Since Lambda uses Amazon Linux, you'll need to target your executable for an `x86_64-linux` platform.
5454

5555
```bash
5656
$ cargo build -p lambda_runtime --example basic --release
@@ -59,10 +59,10 @@ $ cargo build -p lambda_runtime --example basic --release
5959
For a custom runtime, AWS Lambda looks for an executable called `boostrap` in the deployment package zip. Rename the generated `basic` executable to `bootstrap` and add it to a zip archive.
6060

6161
```bash
62-
$ cp ./target/release/examples/basic ./bootstrap && zip rust.zip bootstrap && rm bootstrap
62+
$ cp ./target/release/examples/basic ./bootstrap && zip lambda.zip bootstrap && rm bootstrap
6363
```
6464

65-
Now that we have a deployment package (`rust.zip`), we can use the [AWS CLI](https://aws.amazon.com/cli/) to create a new Lambda function. Make sure to replace the execution role with an existing role in your account.
65+
Now that we have a deployment package (`lambda.zip`), we can use the [AWS CLI](https://aws.amazon.com/cli/) to create a new Lambda function. Make sure to replace the execution role with an existing role in your account!
6666

6767
```bash
6868
$ aws lambda create-function --function-name rustTest \
@@ -84,35 +84,59 @@ $ cat output.json # Prints: {"message":"Hello, world!"}
8484
```
8585

8686
## lambda-runtime-client
87+
8788
Defines the `RuntimeClient` trait and provides its `HttpRuntimeClient` implementation. The client fetches events and returns output as `Vec<u8>`.
8889

8990
For error reporting to the runtime APIs the library defines the `RuntimeApiError` trait and the `ErrorResponse` object. Custom errors for the APIs should implement the `to_response() -> ErrorResponse` method of the `RuntimeApiError` trait.
9091

9192
## lambda-runtime
93+
9294
This library makes it easy to create Rust executables for AWS lambda. The library defines a `lambda!()` macro. Call the `lambda!()` macro from your main method with a function that matches the `Handler` type:
93-
```rust
95+
96+
```rust,ignore
9497
pub type Handler<E, O> = fn(E, Context) -> Result<O, error::HandlerError>;
9598
```
9699

97100
Optionally, you can pass your own instance of Tokio runtime to the `lambda!()` macro. See our [`with_custom_runtime.rs` example](https://github.com/awslabs/aws-lambda-rust-runtime/tree/master/lambda-runtime/examples/with_custom_runtime.rs)
98101

99102
## Custom event objects
100103

101-
To serialize and de-serialize events and responses we use the [`serde_json`](https://crates.io/crates/serde_json) library. To receive custom events, simply annotate your structure with Serde's macros:
104+
To serialize and deserialize events and responses, we suggest using the use the [`serde`](https://github.com/serde-rs/serde) library. To receive custom events, annotate your structure with Serde's macros:
102105

103106
```rust
104107
extern crate serde;
105-
#[macro_use]
106108
extern crate serde_derive;
107109
extern crate serde_json;
108110

109-
#[derive(Deserialize)]
110-
pub struct MyEvent {
111-
pub records: Vec<String>,
111+
use serde_derive::{Serialize, Deserialize};
112+
use serde_json::json;
113+
use std::error::Error;
114+
115+
#[derive(Serialize, Deserialize)]
116+
pub struct NewIceCreamEvent {
117+
pub flavors: Vec<String>,
112118
}
113119

114-
#[derive(Serialize)]
115-
pub struct MyEventResponse {
116-
pub message: String,
120+
#[derive(Serialize, Deserialize)]
121+
pub struct NewIceCreamResponse {
122+
pub flavors_added_count: usize,
123+
}
124+
125+
fn main() -> Result<(), Box<Error>> {
126+
let flavors = json!({
127+
"flavors": [
128+
"Nocciola",
129+
"抹茶",
130+
"आम"
131+
]
132+
});
133+
134+
let event: NewIceCreamEvent = serde_json::from_value(flavors)?;
135+
let response = NewIceCreamResponse {
136+
flavors_added_count: event.flavors.len(),
137+
};
138+
serde_json::to_string(&response)?;
139+
140+
Ok(())
117141
}
118142
```

lambda-runtime/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ authors = ["Stefano Buliani", "David Barsky"]
55
description = "Rust runtime for AWS Lambda"
66
keywords = ["AWS", "Lambda", "Runtime", "Rust"]
77
license = "Apache-2.0"
8+
build = "build.rs"
89

910
[dependencies]
1011
serde = "^1"
@@ -21,4 +22,8 @@ simple_logger = "^1"
2122
[dev-dependencies]
2223
hyper-tls = "^0.3"
2324
rusoto_core = "^0.35"
24-
rusoto_dynamodb = "^0.35"
25+
rusoto_dynamodb = "^0.35"
26+
skeptic = "^0.13"
27+
28+
[build-dependencies]
29+
skeptic = "^0.13"

lambda-runtime/build.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extern crate skeptic;
2+
3+
fn main() {
4+
// generates doc tests for `README.md`.
5+
skeptic::generate_doc_tests(&["./../README.md"]);
6+
}

lambda-runtime/examples/basic.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
#[macro_use]
21
extern crate lambda_runtime as lambda;
3-
#[macro_use]
42
extern crate serde_derive;
5-
#[macro_use]
63
extern crate log;
74
extern crate simple_logger;
85

9-
use lambda::error::HandlerError;
10-
6+
use serde_derive::{Serialize, Deserialize};
7+
use lambda::{lambda, error::HandlerError};
8+
use log::error;
119
use std::error::Error;
1210

13-
#[derive(Deserialize, Clone)]
11+
#[derive(Deserialize)]
1412
struct CustomEvent {
1513
#[serde(rename = "firstName")]
1614
first_name: String,
1715
}
1816

19-
#[derive(Serialize, Clone)]
17+
#[derive(Serialize)]
2018
struct CustomOutput {
2119
message: String,
2220
}

lambda-runtime/examples/with_custom_runtime.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
#[macro_use]
21
extern crate lambda_runtime as lambda;
3-
#[macro_use]
42
extern crate serde_derive;
5-
#[macro_use]
63
extern crate log;
74
extern crate simple_logger;
85
extern crate tokio;
96

10-
use lambda::error::HandlerError;
7+
use lambda::{lambda, error::HandlerError};
118
use tokio::runtime::Runtime;
12-
9+
use serde_derive::{Serialize, Deserialize};
10+
use log::error;
1311
use std::error::Error;
1412

1513
#[derive(Deserialize, Clone)]

lambda-runtime/tests/skeptic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include!(concat!(env!("OUT_DIR"), "/skeptic-tests.rs"));

0 commit comments

Comments
 (0)