Skip to content

Commit 0146698

Browse files
Sh4rKdavidbarsky
authored andcommitted
Migrate lambda-runtime to Rust 2018 (#39)
1 parent 70e7301 commit 0146698

File tree

8 files changed

+37
-50
lines changed

8 files changed

+37
-50
lines changed

lambda-runtime/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "lambda_runtime"
33
version = "0.1.0"
44
authors = ["Stefano Buliani", "David Barsky"]
5+
edition = "2018"
56
description = "Rust runtime for AWS Lambda"
67
keywords = ["AWS", "Lambda", "Runtime", "Rust"]
78
license = "Apache-2.0"

lambda-runtime/examples/basic.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
extern crate lambda_runtime as lambda;
2-
extern crate log;
3-
extern crate serde_derive;
4-
extern crate simple_logger;
1+
use std::error::Error;
52

6-
use lambda::{error::HandlerError, lambda};
7-
use log::error;
3+
use lambda_runtime::{error::HandlerError, lambda, Context};
4+
use log::{self, error};
85
use serde_derive::{Deserialize, Serialize};
9-
use std::error::Error;
6+
use simple_logger;
107

118
#[derive(Deserialize)]
129
struct CustomEvent {
@@ -26,7 +23,7 @@ fn main() -> Result<(), Box<dyn Error>> {
2623
Ok(())
2724
}
2825

29-
fn my_handler(e: CustomEvent, c: lambda::Context) -> Result<CustomOutput, HandlerError> {
26+
fn my_handler(e: CustomEvent, c: Context) -> Result<CustomOutput, HandlerError> {
3027
if e.first_name == "" {
3128
error!("Empty first name in request {}", c.aws_request_id);
3229
return Err(c.new_error("Empty first name"));

lambda-runtime/examples/with_custom_runtime.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
extern crate lambda_runtime as lambda;
2-
extern crate log;
3-
extern crate serde_derive;
4-
extern crate simple_logger;
5-
extern crate tokio;
6-
7-
use lambda::{error::HandlerError, lambda};
8-
use log::error;
9-
use serde_derive::{Deserialize, Serialize};
101
use std::error::Error;
2+
3+
use lambda_runtime::{error::HandlerError, lambda, Context};
4+
use log::{self, error};
5+
use serde_derive::{Deserialize, Serialize};
6+
use simple_logger;
117
use tokio::runtime::Runtime;
128

139
#[derive(Deserialize, Clone)]
@@ -30,7 +26,7 @@ fn main() -> Result<(), Box<dyn Error>> {
3026
Ok(())
3127
}
3228

33-
fn my_handler(e: CustomEvent, c: lambda::Context) -> Result<CustomOutput, HandlerError> {
29+
fn my_handler(e: CustomEvent, c: Context) -> Result<CustomOutput, HandlerError> {
3430
if e.first_name == "" {
3531
error!("Empty first name in request {}", c.aws_request_id);
3632
return Err(c.new_error("Empty first name"));

lambda-runtime/src/context.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use std::env;
22

3-
use chrono::Utc;
4-
53
use backtrace;
6-
use env as lambda_env;
7-
use error::HandlerError;
4+
use chrono::Utc;
85
use lambda_runtime_client;
96

7+
use crate::env as lambda_env;
8+
use crate::error::HandlerError;
9+
1010
/// The Lambda function execution context. The values in this struct
1111
/// are populated using the [Lambda environment variables](https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html)
1212
/// and the headers returned by the poll request to the Runtime APIs.
@@ -110,7 +110,7 @@ impl Context {
110110
#[cfg(test)]
111111
pub(crate) mod tests {
112112
use super::*;
113-
use env::{self, ConfigProvider};
113+
use crate::env::{self, ConfigProvider};
114114
use std::{thread::sleep, time};
115115

116116
fn get_deadline(timeout_secs: i64) -> i64 {

lambda-runtime/src/env.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::env;
22

3-
use error::RuntimeError;
3+
use crate::error::RuntimeError;
44

55
/// The name of the environment variable in the Lambda execution
66
/// environment for the Runtime APIs endpoint. The value of this
@@ -88,8 +88,8 @@ impl ConfigProvider for EnvConfigProvider {
8888

8989
#[cfg(test)]
9090
pub(crate) mod tests {
91-
use env::*;
92-
use error;
91+
use crate::env::*;
92+
use crate::error;
9393
use std::{env, error::Error};
9494

9595
pub(crate) struct MockConfigProvider {
@@ -146,7 +146,7 @@ pub(crate) mod tests {
146146
unset_env_vars();
147147
set_endpoint_env_var();
148148
set_lambda_env_vars();
149-
let config_provider: &ConfigProvider = &EnvConfigProvider {};
149+
let config_provider: &dyn ConfigProvider = &EnvConfigProvider {};
150150
let env_settings = config_provider.get_function_settings();
151151
assert_eq!(
152152
env_settings.is_err(),

lambda-runtime/src/error.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl error::RuntimeApiError for RuntimeError {
7777
}
7878

7979
impl fmt::Display for RuntimeError {
80-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8181
write!(f, "{}", self.msg)
8282
}
8383
}
@@ -88,7 +88,7 @@ impl Error for RuntimeError {
8888
&self.msg
8989
}
9090

91-
fn cause(&self) -> Option<&Error> {
91+
fn cause(&self) -> Option<&dyn Error> {
9292
// Generic error, underlying cause isn't tracked.
9393
None
9494
}
@@ -125,7 +125,7 @@ pub struct HandlerError {
125125
}
126126

127127
impl fmt::Display for HandlerError {
128-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
128+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
129129
write!(f, "{}", self.msg)
130130
}
131131
}
@@ -136,7 +136,7 @@ impl Error for HandlerError {
136136
&self.msg
137137
}
138138

139-
fn cause(&self) -> Option<&Error> {
139+
fn cause(&self) -> Option<&dyn Error> {
140140
// Generic error, underlying cause isn't tracked.
141141
None
142142
}

lambda-runtime/src/lib.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,11 @@
4242
#[macro_use]
4343
extern crate log;
4444

45-
extern crate backtrace;
46-
extern crate chrono;
47-
extern crate lambda_runtime_client;
48-
extern crate serde;
49-
extern crate serde_json;
50-
extern crate tokio;
51-
5245
mod context;
5346
mod env;
5447
pub mod error;
55-
pub use error::HandlerError;
5648
mod runtime;
5749

58-
pub use context::*;
59-
pub use runtime::*;
50+
pub use crate::context::*;
51+
pub use crate::error::HandlerError;
52+
pub use crate::runtime::*;

lambda-runtime/src/runtime.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use std::{error::Error, marker::PhantomData, result};
22

3+
use lambda_runtime_client::RuntimeClient;
34
use serde;
45
use serde_json;
5-
6-
use context::Context;
7-
use env::{ConfigProvider, EnvConfigProvider, FunctionSettings};
8-
use error::{HandlerError, RuntimeError};
9-
use lambda_runtime_client::RuntimeClient;
106
use tokio::runtime::Runtime as TokioRuntime;
117

8+
use crate::context::Context;
9+
use crate::env::{ConfigProvider, EnvConfigProvider, FunctionSettings};
10+
use crate::error::{HandlerError, RuntimeError};
11+
1212
const MAX_RETRIES: i8 = 3;
1313

1414
/// Functions acting as a handler must conform to this type.
@@ -301,7 +301,7 @@ where
301301

302302
(ev, handler_ctx)
303303
}
304-
Err(mut e) => {
304+
Err(e) => {
305305
error!("Could not parse event to type: {}", e);
306306
let mut runtime_err = RuntimeError::from(e);
307307
runtime_err.request_id = Option::from(invocation_ctx.aws_request_id);
@@ -317,13 +317,13 @@ where
317317
#[cfg(test)]
318318
pub(crate) mod tests {
319319
use super::*;
320-
use context;
321-
use env;
320+
use crate::context;
321+
use crate::env;
322322
use lambda_runtime_client::RuntimeClient;
323323

324324
#[test]
325325
fn runtime_invokes_handler() {
326-
let config: &env::ConfigProvider = &env::tests::MockConfigProvider { error: false };
326+
let config: &dyn env::ConfigProvider = &env::tests::MockConfigProvider { error: false };
327327
let client = RuntimeClient::new(
328328
config
329329
.get_runtime_api_endpoint()

0 commit comments

Comments
 (0)