1use std::collections::BTreeMap;
2
3use serde_json::{Map, Value};
4
5use crate::generated::{CreateWorkflowNodeTypeName, WorkflowQueuedContactPolicy};
6
7#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum ContactId {
10 Email(String),
12 UserId(String),
14 Both {
16 email: String,
18 user_id: String,
20 },
21}
22
23impl ContactId {
24 pub fn email(email: impl Into<String>) -> Self {
26 Self::Email(email.into())
27 }
28
29 pub fn user_id(user_id: impl Into<String>) -> Self {
31 Self::UserId(user_id.into())
32 }
33
34 pub fn both(email: impl Into<String>, user_id: impl Into<String>) -> Self {
36 Self::Both {
37 email: email.into(),
38 user_id: user_id.into(),
39 }
40 }
41
42 pub fn email_address(&self) -> Option<&str> {
44 match self {
45 Self::Email(email) | Self::Both { email, .. } => Some(email),
46 Self::UserId(_) => None,
47 }
48 }
49
50 pub fn user_id_value(&self) -> Option<&str> {
52 match self {
53 Self::UserId(user_id) | Self::Both { user_id, .. } => Some(user_id),
54 Self::Email(_) => None,
55 }
56 }
57}
58
59#[derive(Debug, Clone, Default, PartialEq)]
62#[non_exhaustive]
63#[must_use]
64pub struct ContactFields {
65 pub first_name: Option<String>,
67 pub last_name: Option<String>,
69 pub source: Option<String>,
71 pub subscribed: Option<bool>,
74 pub user_group: Option<String>,
76 pub mailing_lists: BTreeMap<String, bool>,
78 pub custom_properties: Map<String, Value>,
80}
81
82impl ContactFields {
83 pub fn new() -> Self {
85 Self::default()
86 }
87
88 pub fn first_name(mut self, value: impl Into<String>) -> Self {
90 self.first_name = Some(value.into());
91 self
92 }
93
94 pub fn last_name(mut self, value: impl Into<String>) -> Self {
96 self.last_name = Some(value.into());
97 self
98 }
99
100 pub fn source(mut self, value: impl Into<String>) -> Self {
102 self.source = Some(value.into());
103 self
104 }
105
106 pub fn subscribed(mut self, value: bool) -> Self {
108 self.subscribed = Some(value);
109 self
110 }
111
112 pub fn user_group(mut self, value: impl Into<String>) -> Self {
114 self.user_group = Some(value.into());
115 self
116 }
117
118 pub fn mailing_list(mut self, list_id: impl Into<String>, subscribed: bool) -> Self {
120 self.mailing_lists.insert(list_id.into(), subscribed);
121 self
122 }
123
124 pub fn property(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
126 self.custom_properties.insert(key.into(), value.into());
127 self
128 }
129}
130
131#[derive(Debug, Clone, Default, PartialEq, Eq)]
133#[non_exhaustive]
134#[must_use]
135pub struct Page {
136 pub per_page: Option<u32>,
138 pub cursor: Option<String>,
140}
141
142impl Page {
143 pub fn first() -> Self {
145 Self::default()
146 }
147
148 pub fn per_page(mut self, per_page: u32) -> Self {
150 self.per_page = Some(per_page);
151 self
152 }
153
154 pub fn cursor(mut self, cursor: impl Into<String>) -> Self {
156 self.cursor = Some(cursor.into());
157 self
158 }
159
160 pub(crate) fn per_page_param(&self) -> Option<String> {
161 self.per_page.map(|per_page| per_page.to_string())
162 }
163}
164
165#[derive(Debug, Clone, Default, PartialEq)]
167#[non_exhaustive]
168#[must_use]
169pub struct EventOptions {
170 pub event_properties: Map<String, Value>,
172 pub mailing_lists: BTreeMap<String, bool>,
174 pub contact_properties: Map<String, Value>,
176 pub idempotency_key: Option<String>,
178}
179
180impl EventOptions {
181 pub fn new() -> Self {
183 Self::default()
184 }
185
186 pub fn event_property(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
188 self.event_properties.insert(key.into(), value.into());
189 self
190 }
191
192 pub fn mailing_list(mut self, list_id: impl Into<String>, subscribed: bool) -> Self {
194 self.mailing_lists.insert(list_id.into(), subscribed);
195 self
196 }
197
198 pub fn contact_property(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
200 self.contact_properties.insert(key.into(), value.into());
201 self
202 }
203
204 pub fn idempotency_key(mut self, key: impl Into<String>) -> Self {
206 self.idempotency_key = Some(key.into());
207 self
208 }
209}
210
211#[derive(Debug, Clone, Default, PartialEq)]
213#[non_exhaustive]
214#[must_use]
215pub struct TransactionalOptions {
216 pub data_variables: Map<String, Value>,
219 pub attachments: Vec<crate::generated::TransactionalRequestAttachment>,
221 pub add_to_audience: Option<bool>,
223 pub idempotency_key: Option<String>,
225}
226
227impl TransactionalOptions {
228 pub fn new() -> Self {
230 Self::default()
231 }
232
233 pub fn data_variable(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
235 self.data_variables.insert(key.into(), value.into());
236 self
237 }
238
239 pub fn attachment(
241 mut self,
242 filename: impl Into<String>,
243 content_type: impl Into<String>,
244 data: impl Into<String>,
245 ) -> Self {
246 self.attachments
247 .push(crate::generated::TransactionalRequestAttachment {
248 filename: filename.into(),
249 content_type: content_type.into(),
250 data: data.into(),
251 });
252 self
253 }
254
255 pub fn add_to_audience(mut self, add: bool) -> Self {
257 self.add_to_audience = Some(add);
258 self
259 }
260
261 pub fn idempotency_key(mut self, key: impl Into<String>) -> Self {
263 self.idempotency_key = Some(key.into());
264 self
265 }
266}
267
268#[derive(Debug, Clone, Copy, PartialEq, Eq)]
270pub enum EmailFormat {
271 Plain,
273 Styled,
275}
276
277#[derive(Debug, Clone, Default, PartialEq, Eq)]
282#[non_exhaustive]
283#[must_use]
284pub struct EmailMessageUpdate {
285 pub subject: Option<String>,
287 pub preview_text: Option<String>,
289 pub from_name: Option<String>,
291 pub from_email: Option<String>,
293 pub reply_to_email: Option<String>,
295 pub lmx: Option<String>,
297 pub expected_revision_id: Option<String>,
299 pub cc_email: Option<String>,
302 pub bcc_email: Option<String>,
304 pub language_code: Option<String>,
306 pub email_format: Option<EmailFormat>,
308 pub contact_properties_fallbacks: Option<BTreeMap<String, Option<String>>>,
310 pub event_properties_fallbacks: Option<BTreeMap<String, Option<String>>>,
312 pub data_variables_fallbacks: Option<BTreeMap<String, Option<String>>>,
314}
315
316impl EmailMessageUpdate {
317 pub fn new() -> Self {
319 Self::default()
320 }
321
322 pub fn subject(mut self, value: impl Into<String>) -> Self {
324 self.subject = Some(value.into());
325 self
326 }
327
328 pub fn preview_text(mut self, value: impl Into<String>) -> Self {
330 self.preview_text = Some(value.into());
331 self
332 }
333
334 pub fn from_name(mut self, value: impl Into<String>) -> Self {
336 self.from_name = Some(value.into());
337 self
338 }
339
340 pub fn from_email(mut self, value: impl Into<String>) -> Self {
342 self.from_email = Some(value.into());
343 self
344 }
345
346 pub fn reply_to_email(mut self, value: impl Into<String>) -> Self {
348 self.reply_to_email = Some(value.into());
349 self
350 }
351
352 pub fn lmx(mut self, value: impl Into<String>) -> Self {
354 self.lmx = Some(value.into());
355 self
356 }
357
358 pub fn expected_revision_id(mut self, value: impl Into<String>) -> Self {
360 self.expected_revision_id = Some(value.into());
361 self
362 }
363
364 pub fn cc_email(mut self, value: impl Into<String>) -> Self {
366 self.cc_email = Some(value.into());
367 self
368 }
369
370 pub fn bcc_email(mut self, value: impl Into<String>) -> Self {
372 self.bcc_email = Some(value.into());
373 self
374 }
375
376 pub fn language_code(mut self, value: impl Into<String>) -> Self {
378 self.language_code = Some(value.into());
379 self
380 }
381
382 pub fn email_format(mut self, value: EmailFormat) -> Self {
384 self.email_format = Some(value);
385 self
386 }
387
388 pub fn contact_property_fallback(
390 mut self,
391 key: impl Into<String>,
392 value: Option<&str>,
393 ) -> Self {
394 self.contact_properties_fallbacks
395 .get_or_insert_with(BTreeMap::new)
396 .insert(key.into(), value.map(str::to_owned));
397 self
398 }
399
400 pub fn event_property_fallback(mut self, key: impl Into<String>, value: Option<&str>) -> Self {
402 self.event_properties_fallbacks
403 .get_or_insert_with(BTreeMap::new)
404 .insert(key.into(), value.map(str::to_owned));
405 self
406 }
407
408 pub fn data_variable_fallback(mut self, key: impl Into<String>, value: Option<&str>) -> Self {
410 self.data_variables_fallbacks
411 .get_or_insert_with(BTreeMap::new)
412 .insert(key.into(), value.map(str::to_owned));
413 self
414 }
415}
416
417#[derive(Debug, Clone, Default, PartialEq)]
419#[non_exhaustive]
420#[must_use]
421pub struct PreviewOptions {
422 pub contact_properties: Map<String, Value>,
424 pub event_properties: Map<String, Value>,
426 pub data_variables: Map<String, Value>,
428}
429
430impl PreviewOptions {
431 pub fn new() -> Self {
433 Self::default()
434 }
435
436 pub fn contact_property(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
438 self.contact_properties.insert(key.into(), value.into());
439 self
440 }
441
442 pub fn event_property(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
444 self.event_properties.insert(key.into(), value.into());
445 self
446 }
447
448 pub fn data_variable(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
450 self.data_variables.insert(key.into(), value.into());
451 self
452 }
453}
454
455#[derive(Debug, Clone, PartialEq, Eq)]
457pub enum NodePlacement {
458 Between {
460 from_node_id: String,
462 to_node_id: String,
464 },
465 Before {
468 to_node_id: String,
470 },
471 After {
474 from_node_id: String,
476 },
477}
478
479impl NodePlacement {
480 pub fn between(from_node_id: impl Into<String>, to_node_id: impl Into<String>) -> Self {
482 Self::Between {
483 from_node_id: from_node_id.into(),
484 to_node_id: to_node_id.into(),
485 }
486 }
487
488 pub fn before(to_node_id: impl Into<String>) -> Self {
490 Self::Before {
491 to_node_id: to_node_id.into(),
492 }
493 }
494
495 pub fn after(from_node_id: impl Into<String>) -> Self {
497 Self::After {
498 from_node_id: from_node_id.into(),
499 }
500 }
501}
502
503#[derive(Debug, Clone, Default, PartialEq, Eq)]
505#[non_exhaustive]
506#[must_use]
507pub struct DeleteNodeOptions {
508 pub dry_run: Option<bool>,
510 pub queued_contact_policy: Option<WorkflowQueuedContactPolicy>,
512 pub recursive: bool,
515}
516
517impl DeleteNodeOptions {
518 pub fn new() -> Self {
520 Self::default()
521 }
522
523 pub fn dry_run(mut self, dry_run: bool) -> Self {
525 self.dry_run = Some(dry_run);
526 self
527 }
528
529 pub fn queued_contact_policy(mut self, policy: WorkflowQueuedContactPolicy) -> Self {
531 self.queued_contact_policy = Some(policy);
532 self
533 }
534
535 pub fn recursive(mut self, recursive: bool) -> Self {
537 self.recursive = recursive;
538 self
539 }
540}
541
542pub(crate) fn node_type_name(node_type: &CreateWorkflowNodeTypeName) -> Value {
543 serde_json::to_value(node_type).unwrap_or(Value::Null)
544}