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
use super::Event;
use chrono::prelude::*;
use serde_json;
use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CloudEvent {
pub cloud_events_version: String,
pub event_type: String,
pub event_type_version: String,
pub source: String,
pub event_id: String,
pub event_time: DateTime<Utc>,
pub content_type: String,
pub data: serde_json::Value,
}
impl<E> From<E> for CloudEvent
where
E: Event,
{
fn from(source: E) -> Self {
let raw_data = serde_json::to_string(&source).unwrap();
CloudEvent {
cloud_events_version: "0.1".to_owned(),
event_type: source.event_type().to_owned(),
event_type_version: source.event_type_version().to_owned(),
source: source.event_source().to_owned(),
event_id: Uuid::new_v4().hyphenated().to_string(),
event_time: Utc::now(),
content_type: "application/json".to_owned(),
data: serde_json::from_str(&raw_data).unwrap(),
}
}
}