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
use serde::{Deserialize, Serialize};
use crate::helpers::mandatory;
use crate::misc::OwningApplication;
use crate::Error;
use super::*;
#[derive(Debug, Clone, Serialize)]
pub struct SubscriptionInput {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<SubscriptionId>,
pub owning_application: OwningApplication,
pub event_types: EventTypeNames,
#[serde(skip_serializing_if = "Option::is_none")]
pub consumer_group: Option<ConsumerGroup>,
pub read_from: ReadFrom,
#[serde(skip_serializing_if = "Option::is_none")]
pub initial_cursors: Option<Vec<EventTypeCursor>>,
pub authorization: SubscriptionAuthorization,
}
impl SubscriptionInput {
pub fn builder() -> subscription_input::SubscriptionInputBuilder {
subscription_input::SubscriptionInputBuilder::default()
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReadFrom {
Begin,
End,
Cursors,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct SubscriptionInputBuilder {
pub id: Option<SubscriptionId>,
pub owning_application: Option<OwningApplication>,
pub event_types: Option<EventTypeNames>,
pub consumer_group: Option<ConsumerGroup>,
pub read_from: Option<ReadFrom>,
pub initial_cursors: Option<Vec<EventTypeCursor>>,
pub authorization: Option<SubscriptionAuthorization>,
}
impl SubscriptionInputBuilder {
pub fn id<T: Into<SubscriptionId>>(mut self, id: T) -> Self {
self.id = Some(id.into());
self
}
pub fn owning_application<T: Into<OwningApplication>>(mut self, owning_application: T) -> Self {
self.owning_application = Some(owning_application.into());
self
}
pub fn event_types<T: Into<EventTypeNames>>(mut self, event_types: T) -> Self {
self.event_types = Some(event_types.into());
self
}
pub fn consumer_group<T: Into<ConsumerGroup>>(mut self, consumer_group: T) -> Self {
self.consumer_group = Some(consumer_group.into());
self
}
pub fn read_from(mut self, read_from: ReadFrom) -> Self {
self.read_from = Some(read_from);
self
}
pub fn initial_cursors(mut self, initial_cursors: Vec<EventTypeCursor>) -> Self {
self.initial_cursors = Some(initial_cursors);
self
}
pub fn authorization<T: Into<SubscriptionAuthorization>>(mut self, authorization: T) -> Self {
self.authorization = Some(authorization.into());
self
}
pub fn finish_for_create(self) -> Result<SubscriptionInput, Error> {
self.make(true)
}
pub fn finish_for_update(self) -> Result<SubscriptionInput, Error> {
self.make(false)
}
fn make(self, for_create: bool) -> Result<SubscriptionInput, Error> {
let id = if let Some(id) = self.id {
if for_create {
return Err(Error::new(
"a subscription input for creation must not have a subscription id",
));
}
Some(id)
} else {
if !for_create {
return Err(Error::new(
"a subscription input for update must have a subscription id",
));
}
None
};
let owning_application = mandatory(self.owning_application, "owning_application")?;
let event_types = if let Some(event_types) = self.event_types {
if event_types.is_empty() {
return Err(Error::new("event types must not be empty"));
} else {
event_types
}
} else {
return Err(Error::new("event types is mandatory"));
};
let read_from = mandatory(self.read_from, "read_from")?;
let initial_cursors = if let Some(initial_cursors) = self.initial_cursors {
if initial_cursors.is_empty() {
return Err(Error::new("initial cursors must not be empty if set"));
} else {
Some(initial_cursors)
}
} else {
None
};
let authorization = mandatory(self.authorization, "authorization")?;
Ok(SubscriptionInput {
id,
owning_application,
event_types,
consumer_group: self.consumer_group,
read_from,
initial_cursors,
authorization,
})
}
}