1
- use crate :: { Config , Error } ;
1
+ use crate :: { Error , RefConfig } ;
2
2
use base64:: prelude:: * ;
3
3
use bytes:: Bytes ;
4
4
use http:: { HeaderMap , HeaderValue , StatusCode } ;
@@ -97,7 +97,7 @@ pub struct CognitoIdentity {
97
97
/// are populated using the [Lambda environment variables](https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html)
98
98
/// and [the headers returned by the poll request to the Runtime APIs](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html#runtimes-api-next).
99
99
#[ non_exhaustive]
100
- #[ derive( Clone , Debug , Eq , PartialEq , Default , Serialize , Deserialize ) ]
100
+ #[ derive( Clone , Debug , Deserialize , Eq , PartialEq , Serialize ) ]
101
101
pub struct Context {
102
102
/// The AWS request ID generated by the Lambda service.
103
103
pub request_id : String ,
@@ -117,12 +117,14 @@ pub struct Context {
117
117
/// Lambda function configuration from the local environment variables.
118
118
/// Includes information such as the function name, memory allocation,
119
119
/// version, and log streams.
120
- pub env_config : Config ,
120
+ pub env_config : RefConfig ,
121
121
}
122
122
123
- impl TryFrom < HeaderMap > for Context {
123
+ impl TryFrom < ( RefConfig , HeaderMap ) > for Context {
124
124
type Error = Error ;
125
- fn try_from ( headers : HeaderMap ) -> Result < Self , Self :: Error > {
125
+ fn try_from ( data : ( RefConfig , HeaderMap ) ) -> Result < Self , Self :: Error > {
126
+ let env_config = data. 0 ;
127
+ let headers = data. 1 ;
126
128
let client_context: Option < ClientContext > = if let Some ( value) = headers. get ( "lambda-runtime-client-context" ) {
127
129
serde_json:: from_str ( value. to_str ( ) ?) ?
128
130
} else {
@@ -158,13 +160,20 @@ impl TryFrom<HeaderMap> for Context {
158
160
. map ( |v| String :: from_utf8_lossy ( v. as_bytes ( ) ) . to_string ( ) ) ,
159
161
client_context,
160
162
identity,
161
- .. Default :: default ( )
163
+ env_config ,
162
164
} ;
163
165
164
166
Ok ( ctx)
165
167
}
166
168
}
167
169
170
+ impl Context {
171
+ /// The execution deadline for the current invocation.
172
+ pub fn deadline ( & self ) -> SystemTime {
173
+ SystemTime :: UNIX_EPOCH + Duration :: from_millis ( self . deadline )
174
+ }
175
+ }
176
+
168
177
/// Incoming Lambda request containing the event payload and context.
169
178
#[ derive( Clone , Debug ) ]
170
179
pub struct LambdaEvent < T > {
@@ -273,6 +282,8 @@ where
273
282
#[ cfg( test) ]
274
283
mod test {
275
284
use super :: * ;
285
+ use crate :: Config ;
286
+ use std:: sync:: Arc ;
276
287
277
288
#[ test]
278
289
fn round_trip_lambda_error ( ) {
@@ -292,6 +303,8 @@ mod test {
292
303
293
304
#[ test]
294
305
fn context_with_expected_values_and_types_resolves ( ) {
306
+ let config = Arc :: new ( Config :: default ( ) ) ;
307
+
295
308
let mut headers = HeaderMap :: new ( ) ;
296
309
headers. insert ( "lambda-runtime-aws-request-id" , HeaderValue :: from_static ( "my-id" ) ) ;
297
310
headers. insert ( "lambda-runtime-deadline-ms" , HeaderValue :: from_static ( "123" ) ) ;
@@ -300,16 +313,18 @@ mod test {
300
313
HeaderValue :: from_static ( "arn::myarn" ) ,
301
314
) ;
302
315
headers. insert ( "lambda-runtime-trace-id" , HeaderValue :: from_static ( "arn::myarn" ) ) ;
303
- let tried = Context :: try_from ( headers) ;
316
+ let tried = Context :: try_from ( ( config , headers) ) ;
304
317
assert ! ( tried. is_ok( ) ) ;
305
318
}
306
319
307
320
#[ test]
308
321
fn context_with_certain_missing_headers_still_resolves ( ) {
322
+ let config = Arc :: new ( Config :: default ( ) ) ;
323
+
309
324
let mut headers = HeaderMap :: new ( ) ;
310
325
headers. insert ( "lambda-runtime-aws-request-id" , HeaderValue :: from_static ( "my-id" ) ) ;
311
326
headers. insert ( "lambda-runtime-deadline-ms" , HeaderValue :: from_static ( "123" ) ) ;
312
- let tried = Context :: try_from ( headers) ;
327
+ let tried = Context :: try_from ( ( config , headers) ) ;
313
328
assert ! ( tried. is_ok( ) ) ;
314
329
}
315
330
@@ -338,7 +353,9 @@ mod test {
338
353
"lambda-runtime-client-context" ,
339
354
HeaderValue :: from_str ( & client_context_str) . unwrap ( ) ,
340
355
) ;
341
- let tried = Context :: try_from ( headers) ;
356
+
357
+ let config = Arc :: new ( Config :: default ( ) ) ;
358
+ let tried = Context :: try_from ( ( config, headers) ) ;
342
359
assert ! ( tried. is_ok( ) ) ;
343
360
let tried = tried. unwrap ( ) ;
344
361
assert ! ( tried. client_context. is_some( ) ) ;
@@ -347,17 +364,20 @@ mod test {
347
364
348
365
#[ test]
349
366
fn context_with_empty_client_context_resolves ( ) {
367
+ let config = Arc :: new ( Config :: default ( ) ) ;
350
368
let mut headers = HeaderMap :: new ( ) ;
351
369
headers. insert ( "lambda-runtime-aws-request-id" , HeaderValue :: from_static ( "my-id" ) ) ;
352
370
headers. insert ( "lambda-runtime-deadline-ms" , HeaderValue :: from_static ( "123" ) ) ;
353
371
headers. insert ( "lambda-runtime-client-context" , HeaderValue :: from_static ( "{}" ) ) ;
354
- let tried = Context :: try_from ( headers) ;
372
+ let tried = Context :: try_from ( ( config , headers) ) ;
355
373
assert ! ( tried. is_ok( ) ) ;
356
374
assert ! ( tried. unwrap( ) . client_context. is_some( ) ) ;
357
375
}
358
376
359
377
#[ test]
360
378
fn context_with_identity_resolves ( ) {
379
+ let config = Arc :: new ( Config :: default ( ) ) ;
380
+
361
381
let cognito_identity = CognitoIdentity {
362
382
identity_id : String :: new ( ) ,
363
383
identity_pool_id : String :: new ( ) ,
@@ -370,7 +390,7 @@ mod test {
370
390
"lambda-runtime-cognito-identity" ,
371
391
HeaderValue :: from_str ( & cognito_identity_str) . unwrap ( ) ,
372
392
) ;
373
- let tried = Context :: try_from ( headers) ;
393
+ let tried = Context :: try_from ( ( config , headers) ) ;
374
394
assert ! ( tried. is_ok( ) ) ;
375
395
let tried = tried. unwrap ( ) ;
376
396
assert ! ( tried. identity. is_some( ) ) ;
@@ -379,6 +399,8 @@ mod test {
379
399
380
400
#[ test]
381
401
fn context_with_bad_deadline_type_is_err ( ) {
402
+ let config = Arc :: new ( Config :: default ( ) ) ;
403
+
382
404
let mut headers = HeaderMap :: new ( ) ;
383
405
headers. insert ( "lambda-runtime-aws-request-id" , HeaderValue :: from_static ( "my-id" ) ) ;
384
406
headers. insert (
@@ -390,86 +412,81 @@ mod test {
390
412
HeaderValue :: from_static ( "arn::myarn" ) ,
391
413
) ;
392
414
headers. insert ( "lambda-runtime-trace-id" , HeaderValue :: from_static ( "arn::myarn" ) ) ;
393
- let tried = Context :: try_from ( headers) ;
415
+ let tried = Context :: try_from ( ( config , headers) ) ;
394
416
assert ! ( tried. is_err( ) ) ;
395
417
}
396
418
397
419
#[ test]
398
420
fn context_with_bad_client_context_is_err ( ) {
421
+ let config = Arc :: new ( Config :: default ( ) ) ;
422
+
399
423
let mut headers = HeaderMap :: new ( ) ;
400
424
headers. insert ( "lambda-runtime-aws-request-id" , HeaderValue :: from_static ( "my-id" ) ) ;
401
425
headers. insert ( "lambda-runtime-deadline-ms" , HeaderValue :: from_static ( "123" ) ) ;
402
426
headers. insert (
403
427
"lambda-runtime-client-context" ,
404
428
HeaderValue :: from_static ( "BAD-Type,not JSON" ) ,
405
429
) ;
406
- let tried = Context :: try_from ( headers) ;
430
+ let tried = Context :: try_from ( ( config , headers) ) ;
407
431
assert ! ( tried. is_err( ) ) ;
408
432
}
409
433
410
434
#[ test]
411
435
fn context_with_empty_identity_is_err ( ) {
436
+ let config = Arc :: new ( Config :: default ( ) ) ;
437
+
412
438
let mut headers = HeaderMap :: new ( ) ;
413
439
headers. insert ( "lambda-runtime-aws-request-id" , HeaderValue :: from_static ( "my-id" ) ) ;
414
440
headers. insert ( "lambda-runtime-deadline-ms" , HeaderValue :: from_static ( "123" ) ) ;
415
441
headers. insert ( "lambda-runtime-cognito-identity" , HeaderValue :: from_static ( "{}" ) ) ;
416
- let tried = Context :: try_from ( headers) ;
442
+ let tried = Context :: try_from ( ( config , headers) ) ;
417
443
assert ! ( tried. is_err( ) ) ;
418
444
}
419
445
420
446
#[ test]
421
447
fn context_with_bad_identity_is_err ( ) {
448
+ let config = Arc :: new ( Config :: default ( ) ) ;
449
+
422
450
let mut headers = HeaderMap :: new ( ) ;
423
451
headers. insert ( "lambda-runtime-aws-request-id" , HeaderValue :: from_static ( "my-id" ) ) ;
424
452
headers. insert ( "lambda-runtime-deadline-ms" , HeaderValue :: from_static ( "123" ) ) ;
425
453
headers. insert (
426
454
"lambda-runtime-cognito-identity" ,
427
455
HeaderValue :: from_static ( "BAD-Type,not JSON" ) ,
428
456
) ;
429
- let tried = Context :: try_from ( headers) ;
457
+ let tried = Context :: try_from ( ( config , headers) ) ;
430
458
assert ! ( tried. is_err( ) ) ;
431
459
}
432
460
433
461
#[ test]
434
462
#[ should_panic]
435
463
#[ allow( unused_must_use) ]
436
464
fn context_with_missing_request_id_should_panic ( ) {
465
+ let config = Arc :: new ( Config :: default ( ) ) ;
466
+
437
467
let mut headers = HeaderMap :: new ( ) ;
438
468
headers. insert ( "lambda-runtime-aws-request-id" , HeaderValue :: from_static ( "my-id" ) ) ;
439
469
headers. insert (
440
470
"lambda-runtime-invoked-function-arn" ,
441
471
HeaderValue :: from_static ( "arn::myarn" ) ,
442
472
) ;
443
473
headers. insert ( "lambda-runtime-trace-id" , HeaderValue :: from_static ( "arn::myarn" ) ) ;
444
- Context :: try_from ( headers) ;
474
+ Context :: try_from ( ( config , headers) ) ;
445
475
}
446
476
447
477
#[ test]
448
478
#[ should_panic]
449
479
#[ allow( unused_must_use) ]
450
480
fn context_with_missing_deadline_should_panic ( ) {
481
+ let config = Arc :: new ( Config :: default ( ) ) ;
482
+
451
483
let mut headers = HeaderMap :: new ( ) ;
452
484
headers. insert ( "lambda-runtime-deadline-ms" , HeaderValue :: from_static ( "123" ) ) ;
453
485
headers. insert (
454
486
"lambda-runtime-invoked-function-arn" ,
455
487
HeaderValue :: from_static ( "arn::myarn" ) ,
456
488
) ;
457
489
headers. insert ( "lambda-runtime-trace-id" , HeaderValue :: from_static ( "arn::myarn" ) ) ;
458
- Context :: try_from ( headers) ;
459
- }
460
- }
461
-
462
- impl Context {
463
- /// Add environment details to the context by setting `env_config`.
464
- pub fn with_config ( self , config : & Config ) -> Self {
465
- Self {
466
- env_config : config. clone ( ) ,
467
- ..self
468
- }
469
- }
470
-
471
- /// The execution deadline for the current invocation.
472
- pub fn deadline ( & self ) -> SystemTime {
473
- SystemTime :: UNIX_EPOCH + Duration :: from_millis ( self . deadline )
490
+ Context :: try_from ( ( config, headers) ) ;
474
491
}
475
492
}
0 commit comments