aws_lambda_events/
time_window.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5use crate::custom_serde::deserialize_lambda_map;
6
7/// `Window` is the object that captures the time window for the records in the event when using the tumbling windows feature
8/// Kinesis: https://docs.aws.amazon.com/lambda/latest/dg/with-kinesis.html#services-kinesis-windows
9/// DDB: https://docs.aws.amazon.com/lambda/latest/dg/with-ddb.html#services-ddb-windows
10#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
11#[serde(rename_all = "camelCase")]
12pub struct Window {
13    pub start: DateTime<Utc>,
14    pub end: DateTime<Utc>,
15}
16
17impl Default for Window {
18    fn default() -> Self {
19        Window {
20            start: Utc::now(),
21            end: Utc::now(),
22        }
23    }
24}
25
26/// `TimeWindowProperties` is the object that captures properties that relate to the tumbling windows feature
27/// Kinesis: https://docs.aws.amazon.com/lambda/latest/dg/with-kinesis.html#services-kinesis-windows
28/// DDB: https://docs.aws.amazon.com/lambda/latest/dg/with-ddb.html#services-ddb-windows
29#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
30#[serde(rename_all = "camelCase")]
31pub struct TimeWindowProperties {
32    /// Time window for the records in the event.
33    pub window: Window,
34    /// State being built up to this invoke in the time window.
35    #[serde(deserialize_with = "deserialize_lambda_map")]
36    #[serde(default)]
37    pub state: HashMap<String, String>,
38    /// Shard id of the records
39    #[serde(default)]
40    pub shard_id: Option<String>,
41    /// The event source ARN of the service that generated the event (eg. DynamoDB or Kinesis)
42    #[serde(default)]
43    #[serde(rename = "eventSourceARN")]
44    pub event_source_arn: Option<String>,
45    /// Set to true for the last invoke of the time window.
46    /// Subsequent invoke will start a new time window along with a fresh state.
47    pub is_final_invoke_for_window: bool,
48    /// Set to true if window is terminated prematurely.
49    /// Subsequent invoke will continue the same window with a fresh state.
50    pub is_window_terminated_early: bool,
51}
52
53/// `TimeWindowEventResponseProperties` is the object that captures response properties that relate to the tumbling windows feature
54/// Kinesis: https://docs.aws.amazon.com/lambda/latest/dg/with-kinesis.html#services-kinesis-windows
55/// DDB: https://docs.aws.amazon.com/lambda/latest/dg/with-ddb.html#services-ddb-windows
56#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
57#[serde(rename_all = "camelCase")]
58pub struct TimeWindowEventResponseProperties {
59    /// State being built up to this invoke in the time window.
60    #[serde(deserialize_with = "deserialize_lambda_map")]
61    #[serde(default)]
62    pub state: HashMap<String, String>,
63}
64
65#[cfg(test)]
66mod test {
67    use super::*;
68
69    #[test]
70    fn test_window_deserializer() {
71        let v = serde_json::json!({
72            "start": "2020-12-09T07:04:00Z",
73            "end": "2020-12-09T07:06:00Z",
74        });
75
76        let parsed: Window = serde_json::from_value(v).unwrap();
77        assert_eq!("2020-12-09T07:04:00+00:00", &parsed.start.to_rfc3339());
78        assert_eq!("2020-12-09T07:06:00+00:00", &parsed.end.to_rfc3339());
79    }
80}