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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use serde::{ser::Error as SerializeError, Serialize, Serializer};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
#[derive(Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Severity {
Info,
Warning,
Error,
Critical,
}
#[derive(Serialize)]
pub struct Link {
pub href: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub text: Option<String>,
}
pub type Links = Vec<Link>;
#[derive(Serialize)]
pub struct Image {
pub src: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub href: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub alt: Option<String>,
}
pub type Images = Vec<Image>;
#[derive(Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Action {
Trigger,
Acknowledge,
Resolve,
}
#[derive(Serialize)]
pub struct ChangePayload<T: Serialize> {
pub summary: String,
#[serde(serialize_with = "datetime_to_iso8601")]
pub timestamp: OffsetDateTime,
#[serde(skip_serializing_if = "Option::is_none")]
pub source: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub custom_details: Option<T>,
}
#[derive(Serialize)]
pub struct Change<T: Serialize> {
pub payload: ChangePayload<T>,
#[serde(skip_serializing_if = "Option::is_none")]
pub links: Option<Links>,
}
#[derive(Serialize)]
pub struct AlertTriggerPayload<T: Serialize> {
pub severity: Severity,
pub summary: String,
pub source: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(serialize_with = "optional_datetime_to_iso8601")]
pub timestamp: Option<OffsetDateTime>,
#[serde(skip_serializing_if = "Option::is_none")]
pub component: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub group: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub class: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub custom_details: Option<T>,
}
#[derive(Serialize)]
pub struct AlertTrigger<T: Serialize> {
pub payload: AlertTriggerPayload<T>,
#[serde(skip_serializing_if = "Option::is_none")]
pub dedup_key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub images: Option<Images>,
#[serde(skip_serializing_if = "Option::is_none")]
pub links: Option<Links>,
#[serde(skip_serializing_if = "Option::is_none")]
pub client: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub client_url: Option<String>,
}
#[derive(Serialize)]
pub struct AlertAcknowledge {
pub dedup_key: String,
}
#[derive(Serialize)]
pub struct AlertResolve {
pub dedup_key: String,
}
pub enum Event<T: Serialize> {
Change(Change<T>),
AlertTrigger(AlertTrigger<T>),
AlertAcknowledge(AlertAcknowledge),
AlertResolve(AlertResolve),
}
fn optional_datetime_to_iso8601<S>(
od: &Option<OffsetDateTime>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match od.as_ref() {
Some(d) => datetime_to_iso8601(d, serializer),
None => serializer.serialize_none(),
}
}
fn datetime_to_iso8601<S>(d: &OffsetDateTime, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match d.format(&Rfc3339) {
Ok(formatted) => serializer.serialize_str(formatted.as_str()),
Err(e) => Err(SerializeError::custom(format!("{}", e))),
}
}
#[cfg(test)]
mod test {
use super::*;
use time::macros::date;
#[test]
fn test_serialization_pads() {
let change = Change {
payload: ChangePayload::<()> {
summary: "Testing timestamp serialization".to_owned(),
timestamp: date!(2021 - 05 - 30).midnight().assume_utc(),
source: None,
custom_details: None,
},
links: None,
};
assert_eq!("{\"payload\":{\"summary\":\"Testing timestamp serialization\",\"timestamp\":\"2021-05-30T00:00:00Z\"}}", serde_json::to_string(&change).unwrap());
}
}