1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use crate::{
bindings::QueueMessage,
rpc::{typed_data::Data, TypedData},
util::convert_from,
};
use chrono::{DateTime, Utc};
use std::collections::HashMap;
const ID_KEY: &str = "Id";
const DEQUEUE_COUNT_KEY: &str = "DequeueCount";
const EXPIRATION_TIME_KEY: &str = "ExpirationTime";
const INSERTION_TIME_KEY: &str = "InsertionTime";
const NEXT_VISIBLE_TIME_KEY: &str = "NextVisibleTime";
const POP_RECEIPT_KEY: &str = "PopReceipt";
#[derive(Debug)]
pub struct QueueTrigger {
pub message: QueueMessage,
pub id: String,
pub dequeue_count: u32,
pub expiration_time: DateTime<Utc>,
pub insertion_time: DateTime<Utc>,
pub next_visible_time: DateTime<Utc>,
pub pop_receipt: String,
}
impl QueueTrigger {
#[doc(hidden)]
pub fn new(data: TypedData, mut metadata: HashMap<String, TypedData>) -> Self {
QueueTrigger {
message: data.into(),
id: metadata
.remove(ID_KEY)
.map(|data| match data.data {
Some(Data::String(s)) => s,
_ => panic!("expected a string for message id"),
})
.expect("expected a message id"),
dequeue_count: convert_from(
metadata
.get(DEQUEUE_COUNT_KEY)
.expect("expected a dequeue count"),
)
.expect("failed to convert dequeue count"),
expiration_time: convert_from(
metadata
.get(EXPIRATION_TIME_KEY)
.expect("expected an expiration time"),
)
.expect("failed to convert expiration time"),
insertion_time: convert_from(
metadata
.get(INSERTION_TIME_KEY)
.expect("expected an insertion time"),
)
.expect("failed to convert insertion time"),
next_visible_time: convert_from(
metadata
.get(NEXT_VISIBLE_TIME_KEY)
.expect("expected a next visible time"),
)
.expect("failed to convert next visible time"),
pop_receipt: metadata
.remove(POP_RECEIPT_KEY)
.map(|data| match data.data {
Some(Data::String(s)) => s,
_ => panic!("expected a string for pop receipt"),
})
.expect("expected a pop receipt"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_constructs() {
const ID: &'static str = "12345";
const DEQUEUE_COUNT: u32 = 101;
const POP_RECEIPT: &'static str = "pop!";
const MESSAGE: &'static str = "\"hello world\"";
let now = Utc::now();
let data = TypedData {
data: Some(Data::Json(MESSAGE.to_string())),
};
let mut metadata = HashMap::new();
metadata.insert(
ID_KEY.to_string(),
TypedData {
data: Some(Data::String(ID.to_string())),
},
);
metadata.insert(
DEQUEUE_COUNT_KEY.to_string(),
TypedData {
data: Some(Data::Json(DEQUEUE_COUNT.to_string())),
},
);
metadata.insert(
EXPIRATION_TIME_KEY.to_string(),
TypedData {
data: Some(Data::String(now.to_rfc3339())),
},
);
metadata.insert(
INSERTION_TIME_KEY.to_string(),
TypedData {
data: Some(Data::String(now.to_rfc3339())),
},
);
metadata.insert(
NEXT_VISIBLE_TIME_KEY.to_string(),
TypedData {
data: Some(Data::Json("\"".to_string() + &now.to_rfc3339() + "\"")),
},
);
metadata.insert(
POP_RECEIPT_KEY.to_string(),
TypedData {
data: Some(Data::String(POP_RECEIPT.to_string())),
},
);
let trigger = QueueTrigger::new(data, metadata);
assert_eq!(trigger.id, ID);
assert_eq!(trigger.dequeue_count, DEQUEUE_COUNT);
assert_eq!(trigger.expiration_time.to_rfc3339(), now.to_rfc3339());
assert_eq!(trigger.insertion_time.to_rfc3339(), now.to_rfc3339());
assert_eq!(trigger.next_visible_time.to_rfc3339(), now.to_rfc3339());
assert_eq!(trigger.pop_receipt, POP_RECEIPT);
assert_eq!(trigger.message.as_str().unwrap(), MESSAGE);
}
}