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
use crate::errors::*;
use std::default;
use std::fmt;
use url::Url;
#[derive(Debug, Clone, PartialOrd, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct EventOriginUri {
pub scheme: String,
pub host: String,
pub port: Option<u16>,
pub path: Vec<String>,
}
impl EventOriginUri {
pub fn parse(url: &str) -> Result<Self> {
match Url::parse(url) {
Ok(r) => {
let host = r
.host_str()
.ok_or_else(|| Error::from("EventOriginUri Parse Error: Missing host"))?;
Ok(Self {
scheme: r.scheme().to_string(),
host: host.to_string(),
port: r.port(),
path: r.path().split('/').map(String::from).collect(),
})
}
Err(e) => Err(e.into()),
}
}
pub fn scheme(&self) -> &str {
&self.scheme
}
pub fn host(&self) -> &str {
&self.host
}
pub fn port(&self) -> &Option<u16> {
&self.port
}
pub fn path(&self) -> &Vec<String> {
&self.path
}
pub fn host_port(&self) -> String {
if let Some(port) = self.port() {
format!("{}:{}", self.host(), port)
} else {
self.host().to_string()
}
}
}
impl fmt::Display for EventOriginUri {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let r = write!(f, "{}://{}", self.scheme, self.host);
if let Some(port) = self.port {
write!(f, ":{}", port)?;
}
write!(f, "/{}", self.path.join("/"))?;
r
}
}
impl default::Default for EventOriginUri {
fn default() -> Self {
Self {
scheme: "tremor-script".to_string(),
host: "localhost".to_string(),
port: None,
path: vec![String::default()],
}
}
}
#[derive(Debug, Default, Clone, PartialOrd, PartialEq, Eq, Hash, Serialize)]
pub struct EventContext {
at: u64,
pub origin_uri: Option<EventOriginUri>,
}
impl EventContext {
pub fn new(ingest_ns: u64, origin_uri: Option<EventOriginUri>) -> Self {
Self {
at: ingest_ns,
origin_uri,
}
}
pub fn ingest_ns(&self) -> u64 {
self.at
}
pub fn origin_uri(&self) -> &Option<EventOriginUri> {
&self.origin_uri
}
}