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
use std::time::Duration;
#[derive(Debug, PartialEq)]
pub struct TelemetryConfig {
i_key: String,
endpoint: String,
interval: Duration,
}
impl TelemetryConfig {
pub fn new(i_key: String) -> Self {
TelemetryConfig::builder().i_key(i_key).build()
}
pub fn builder() -> DefaultTelemetryConfigBuilder {
DefaultTelemetryConfigBuilder::default()
}
pub fn i_key(&self) -> &str {
&self.i_key
}
pub fn endpoint(&self) -> &str {
&self.endpoint
}
pub fn interval(&self) -> Duration {
self.interval
}
}
#[derive(Default)]
pub struct DefaultTelemetryConfigBuilder;
impl DefaultTelemetryConfigBuilder {
pub fn i_key<I>(self, i_key: I) -> TelemetryConfigBuilder
where
I: Into<String>,
{
TelemetryConfigBuilder {
i_key: i_key.into(),
endpoint: "https://dc.services.visualstudio.com/v2/track".into(),
interval: Duration::from_secs(2),
}
}
}
pub struct TelemetryConfigBuilder {
i_key: String,
endpoint: String,
interval: Duration,
}
impl TelemetryConfigBuilder {
pub fn i_key<I>(mut self, i_key: I) -> Self
where
I: Into<String>,
{
self.i_key = i_key.into();
self
}
pub fn endpoint<E>(mut self, endpoint: E) -> Self
where
E: Into<String>,
{
self.endpoint = endpoint.into();
self
}
pub fn interval(mut self, interval: Duration) -> Self {
self.interval = interval;
self
}
pub fn build(self) -> TelemetryConfig {
TelemetryConfig {
i_key: self.i_key,
endpoint: self.endpoint,
interval: self.interval,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_creates_config_with_default_values() {
let config = TelemetryConfig::new("instrumentation key".into());
assert_eq!(
TelemetryConfig {
i_key: "instrumentation key".into(),
endpoint: "https://dc.services.visualstudio.com/v2/track".into(),
interval: Duration::from_secs(2)
},
config
)
}
#[test]
fn it_builds_config_with_custom_parameters() {
let config = TelemetryConfig::builder()
.i_key("instrumentation key")
.endpoint("https://google.com")
.interval(Duration::from_micros(100))
.build();
assert_eq!(
TelemetryConfig {
i_key: "instrumentation key".into(),
endpoint: "https://google.com".into(),
interval: Duration::from_micros(100)
},
config
);
}
}