nylas-types 0.1.1

Type definitions for Nylas API v3
Documentation
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! Draft types for the Nylas API v3.

use serde::{Deserialize, Serialize};

use crate::{Attachment, DraftId, EmailAddress, FolderId, GrantId, ThreadId};

/// A draft message object from the Nylas API.
///
/// Drafts are email messages that are being composed but not yet sent.
///
/// # Example
///
/// ```
/// # use nylas_types::{Draft, DraftId, GrantId, EmailAddress};
/// let draft = Draft {
///     id: DraftId::new("draft_123"),
///     grant_id: GrantId::new("grant_123"),
///     thread_id: None,
///     subject: Some("Draft Subject".to_string()),
///     from: vec![],
///     to: vec![],
///     cc: vec![],
///     bcc: vec![],
///     reply_to: vec![],
///     body: Some("Draft body".to_string()),
///     starred: Some(false),
///     snippet: Some("Draft snippet...".to_string()),
///     attachments: vec![],
///     folders: vec![],
///     created_at: Some(1234567890),
/// };
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Draft {
    /// Unique identifier for the draft.
    pub id: DraftId,

    /// Grant ID associated with this draft.
    pub grant_id: GrantId,

    /// Thread ID this draft belongs to (for replies).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thread_id: Option<ThreadId>,

    /// Subject line of the draft.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subject: Option<String>,

    /// Sender email address(es).
    #[serde(default)]
    pub from: Vec<EmailAddress>,

    /// Recipient email address(es).
    #[serde(default)]
    pub to: Vec<EmailAddress>,

    /// CC recipient email address(es).
    #[serde(default)]
    pub cc: Vec<EmailAddress>,

    /// BCC recipient email address(es).
    #[serde(default)]
    pub bcc: Vec<EmailAddress>,

    /// Reply-to email address(es).
    #[serde(default)]
    pub reply_to: Vec<EmailAddress>,

    /// Body of the draft.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<String>,

    /// Whether the draft is starred.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub starred: Option<bool>,

    /// Short snippet of the draft body.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub snippet: Option<String>,

    /// Attachments included with the draft.
    #[serde(default)]
    pub attachments: Vec<Attachment>,

    /// Folder IDs this draft belongs to.
    #[serde(default)]
    pub folders: Vec<FolderId>,

    /// Unix timestamp when the draft was created.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_at: Option<i64>,
}

/// Request to create a new draft.
///
/// # Example
///
/// ```
/// # use nylas_types::{CreateDraftRequest, EmailAddress};
/// let to = vec![EmailAddress::new("recipient@example.com").unwrap()];
/// let draft = CreateDraftRequest::builder()
///     .to(to)
///     .subject("Hello")
///     .body("Draft message")
///     .build();
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct CreateDraftRequest {
    /// Subject line of the draft.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subject: Option<String>,

    /// Sender email address(es).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from: Option<Vec<EmailAddress>>,

    /// Recipient email address(es).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to: Option<Vec<EmailAddress>>,

    /// CC recipient email address(es).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cc: Option<Vec<EmailAddress>>,

    /// BCC recipient email address(es).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bcc: Option<Vec<EmailAddress>>,

    /// Reply-to email address(es).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reply_to: Option<Vec<EmailAddress>>,

    /// Body of the draft.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<String>,

    /// Thread ID for reply drafts.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thread_id: Option<String>,

    /// Whether the draft is starred.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub starred: Option<bool>,
}

impl CreateDraftRequest {
    /// Create a new builder for draft creation.
    pub fn builder() -> CreateDraftRequestBuilder {
        CreateDraftRequestBuilder::default()
    }
}

/// Builder for create draft request.
#[derive(Debug, Clone, Default)]
pub struct CreateDraftRequestBuilder {
    request: CreateDraftRequest,
}

impl CreateDraftRequestBuilder {
    /// Set the subject line.
    pub fn subject(mut self, subject: impl Into<String>) -> Self {
        self.request.subject = Some(subject.into());
        self
    }

    /// Set sender email address(es).
    pub fn from(mut self, from: Vec<EmailAddress>) -> Self {
        self.request.from = Some(from);
        self
    }

    /// Set recipient email address(es).
    pub fn to(mut self, to: Vec<EmailAddress>) -> Self {
        self.request.to = Some(to);
        self
    }

    /// Set CC recipient email address(es).
    pub fn cc(mut self, cc: Vec<EmailAddress>) -> Self {
        self.request.cc = Some(cc);
        self
    }

    /// Set BCC recipient email address(es).
    pub fn bcc(mut self, bcc: Vec<EmailAddress>) -> Self {
        self.request.bcc = Some(bcc);
        self
    }

    /// Set reply-to email address(es).
    pub fn reply_to(mut self, reply_to: Vec<EmailAddress>) -> Self {
        self.request.reply_to = Some(reply_to);
        self
    }

    /// Set the body content.
    pub fn body(mut self, body: impl Into<String>) -> Self {
        self.request.body = Some(body.into());
        self
    }

    /// Set thread ID for reply drafts.
    pub fn thread_id(mut self, thread_id: impl Into<String>) -> Self {
        self.request.thread_id = Some(thread_id.into());
        self
    }

    /// Set starred status.
    pub fn starred(mut self, starred: bool) -> Self {
        self.request.starred = Some(starred);
        self
    }

    /// Build the create draft request.
    pub fn build(self) -> CreateDraftRequest {
        self.request
    }
}

/// Request to update an existing draft.
///
/// # Example
///
/// ```
/// # use nylas_types::UpdateDraftRequest;
/// let update = UpdateDraftRequest::builder()
///     .subject("Updated subject")
///     .body("Updated body")
///     .build();
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct UpdateDraftRequest {
    /// Subject line of the draft.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subject: Option<String>,

    /// Sender email address(es).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from: Option<Vec<EmailAddress>>,

    /// Recipient email address(es).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to: Option<Vec<EmailAddress>>,

    /// CC recipient email address(es).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cc: Option<Vec<EmailAddress>>,

    /// BCC recipient email address(es).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bcc: Option<Vec<EmailAddress>>,

    /// Reply-to email address(es).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reply_to: Option<Vec<EmailAddress>>,

    /// Body of the draft.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<String>,

    /// Whether the draft is starred.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub starred: Option<bool>,
}

impl UpdateDraftRequest {
    /// Create a new builder for draft update.
    pub fn builder() -> UpdateDraftRequestBuilder {
        UpdateDraftRequestBuilder::default()
    }
}

/// Builder for update draft request.
#[derive(Debug, Clone, Default)]
pub struct UpdateDraftRequestBuilder {
    request: UpdateDraftRequest,
}

impl UpdateDraftRequestBuilder {
    /// Update the subject line.
    pub fn subject(mut self, subject: impl Into<String>) -> Self {
        self.request.subject = Some(subject.into());
        self
    }

    /// Update sender email address(es).
    pub fn from(mut self, from: Vec<EmailAddress>) -> Self {
        self.request.from = Some(from);
        self
    }

    /// Update recipient email address(es).
    pub fn to(mut self, to: Vec<EmailAddress>) -> Self {
        self.request.to = Some(to);
        self
    }

    /// Update CC recipient email address(es).
    pub fn cc(mut self, cc: Vec<EmailAddress>) -> Self {
        self.request.cc = Some(cc);
        self
    }

    /// Update BCC recipient email address(es).
    pub fn bcc(mut self, bcc: Vec<EmailAddress>) -> Self {
        self.request.bcc = Some(bcc);
        self
    }

    /// Update reply-to email address(es).
    pub fn reply_to(mut self, reply_to: Vec<EmailAddress>) -> Self {
        self.request.reply_to = Some(reply_to);
        self
    }

    /// Update the body content.
    pub fn body(mut self, body: impl Into<String>) -> Self {
        self.request.body = Some(body.into());
        self
    }

    /// Update starred status.
    pub fn starred(mut self, starred: bool) -> Self {
        self.request.starred = Some(starred);
        self
    }

    /// Build the update draft request.
    pub fn build(self) -> UpdateDraftRequest {
        self.request
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_draft_creation() {
        let draft = Draft {
            id: DraftId::new("draft_123"),
            grant_id: GrantId::new("grant_123"),
            thread_id: Some(ThreadId::new("thread_123")),
            subject: Some("Test Draft".to_string()),
            from: vec![],
            to: vec![],
            cc: vec![],
            bcc: vec![],
            reply_to: vec![],
            body: Some("Draft body".to_string()),
            starred: Some(false),
            snippet: Some("Draft snippet".to_string()),
            attachments: vec![],
            folders: vec![],
            created_at: Some(1234567890),
        };

        assert_eq!(draft.id.as_str(), "draft_123");
        assert_eq!(draft.grant_id.as_str(), "grant_123");
        assert_eq!(draft.subject, Some("Test Draft".to_string()));
    }

    #[test]
    fn test_draft_serialization() {
        let draft = Draft {
            id: DraftId::new("draft_123"),
            grant_id: GrantId::new("grant_123"),
            thread_id: None,
            subject: Some("Test".to_string()),
            from: vec![],
            to: vec![],
            cc: vec![],
            bcc: vec![],
            reply_to: vec![],
            body: Some("Body".to_string()),
            starred: Some(false),
            snippet: Some("Snippet".to_string()),
            attachments: vec![],
            folders: vec![],
            created_at: Some(1234567890),
        };

        let json = serde_json::to_string(&draft).unwrap();
        assert!(json.contains("draft_123"));
        assert!(json.contains("grant_123"));
        assert!(json.contains("Test"));

        let deserialized: Draft = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, draft);
    }

    #[test]
    fn test_create_draft_request_builder() {
        let to = vec![EmailAddress::new("recipient@example.com").unwrap()];
        let draft = CreateDraftRequest::builder()
            .subject("Test")
            .to(to.clone())
            .body("Test body")
            .build();

        assert_eq!(draft.subject, Some("Test".to_string()));
        assert_eq!(draft.to, Some(to));
        assert_eq!(draft.body, Some("Test body".to_string()));
    }

    #[test]
    fn test_create_draft_request_all_fields() {
        let from = vec![EmailAddress::new("sender@example.com").unwrap()];
        let to = vec![EmailAddress::new("recipient@example.com").unwrap()];
        let cc = vec![EmailAddress::new("cc@example.com").unwrap()];

        let draft = CreateDraftRequest::builder()
            .subject("Subject")
            .from(from.clone())
            .to(to.clone())
            .cc(cc.clone())
            .body("Body content")
            .thread_id("thread_123")
            .starred(true)
            .build();

        assert_eq!(draft.subject, Some("Subject".to_string()));
        assert_eq!(draft.from, Some(from));
        assert_eq!(draft.to, Some(to));
        assert_eq!(draft.cc, Some(cc));
        assert_eq!(draft.body, Some("Body content".to_string()));
        assert_eq!(draft.thread_id, Some("thread_123".to_string()));
        assert_eq!(draft.starred, Some(true));
    }

    #[test]
    fn test_create_draft_request_serialization() {
        let to = vec![EmailAddress::new("recipient@example.com").unwrap()];
        let draft = CreateDraftRequest::builder()
            .subject("Test")
            .to(to)
            .body("Body")
            .build();

        let json = serde_json::to_string(&draft).unwrap();
        let deserialized: CreateDraftRequest = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, draft);
    }

    #[test]
    fn test_update_draft_request_builder() {
        let update = UpdateDraftRequest::builder()
            .subject("Updated subject")
            .body("Updated body")
            .starred(true)
            .build();

        assert_eq!(update.subject, Some("Updated subject".to_string()));
        assert_eq!(update.body, Some("Updated body".to_string()));
        assert_eq!(update.starred, Some(true));
    }

    #[test]
    fn test_update_draft_request_with_recipients() {
        let to = vec![EmailAddress::new("new@example.com").unwrap()];
        let update = UpdateDraftRequest::builder()
            .to(to.clone())
            .subject("New subject")
            .build();

        assert_eq!(update.to, Some(to));
        assert_eq!(update.subject, Some("New subject".to_string()));
    }

    #[test]
    fn test_update_draft_request_serialization() {
        let update = UpdateDraftRequest::builder()
            .subject("Test")
            .body("Body")
            .build();

        let json = serde_json::to_string(&update).unwrap();
        let deserialized: UpdateDraftRequest = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, update);
    }
}