|
| 1 | +use serde::Deserialize; |
| 2 | + |
| 3 | +/// Request tracing information |
| 4 | +#[derive(Debug, Deserialize)] |
| 5 | +#[serde(rename_all = "camelCase")] |
| 6 | +pub struct Tracing { |
| 7 | + /// The type of tracing exposed to the extension |
| 8 | + pub r#type: String, |
| 9 | + /// The span value |
| 10 | + pub value: String, |
| 11 | +} |
| 12 | +/// Event received when there is a new Lambda invocation. |
| 13 | +#[derive(Debug, Deserialize)] |
| 14 | +#[serde(rename_all = "camelCase")] |
| 15 | +pub struct InvokeEvent { |
| 16 | + /// The time that the function times out |
| 17 | + pub deadline_ms: u64, |
| 18 | + /// The ID assigned to the Lambda request |
| 19 | + pub request_id: String, |
| 20 | + /// The function's Amazon Resource Name |
| 21 | + pub invoked_function_arn: String, |
| 22 | + /// The request tracing information |
| 23 | + pub tracing: Tracing, |
| 24 | +} |
| 25 | + |
| 26 | +/// Event received when a Lambda function shuts down. |
| 27 | +#[derive(Debug, Deserialize)] |
| 28 | +#[serde(rename_all = "camelCase")] |
| 29 | +pub struct ShutdownEvent { |
| 30 | + /// The reason why the function terminates |
| 31 | + /// It can be SPINDOWN, TIMEOUT, or FAILURE |
| 32 | + pub shutdown_reason: String, |
| 33 | + /// The time that the function times out |
| 34 | + pub deadline_ms: u64, |
| 35 | +} |
| 36 | + |
| 37 | +/// Event that the extension receives in |
| 38 | +/// either the INVOKE or SHUTDOWN phase |
| 39 | +#[derive(Debug, Deserialize)] |
| 40 | +#[serde(rename_all = "UPPERCASE", tag = "eventType")] |
| 41 | +pub enum NextEvent { |
| 42 | + /// Payload when the event happens in the INVOKE phase |
| 43 | + Invoke(InvokeEvent), |
| 44 | + /// Payload when the event happens in the SHUTDOWN phase |
| 45 | + Shutdown(ShutdownEvent), |
| 46 | +} |
| 47 | + |
| 48 | +impl NextEvent { |
| 49 | + /// Return whether the event is a [`NextEvent::Invoke`] event or not |
| 50 | + pub fn is_invoke(&self) -> bool { |
| 51 | + matches!(self, NextEvent::Invoke(_)) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// Wrapper with information about the next |
| 56 | +/// event that the Lambda Runtime is going to process |
| 57 | +pub struct LambdaEvent { |
| 58 | + /// ID assigned to this extension by the Lambda Runtime |
| 59 | + pub extension_id: String, |
| 60 | + /// Next incoming event |
| 61 | + pub next: NextEvent, |
| 62 | +} |
| 63 | + |
| 64 | +impl LambdaEvent { |
| 65 | + pub(crate) fn new(ex_id: &str, next: NextEvent) -> LambdaEvent { |
| 66 | + LambdaEvent { |
| 67 | + extension_id: ex_id.into(), |
| 68 | + next, |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments