nylas-types 0.1.0

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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Message types for the Nylas API v3.

use serde::{Deserialize, Serialize};

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

/// A message object from the Nylas API.
///
/// Messages are email messages in a user's mailbox.
///
/// # Example
///
/// ```
/// # use nylas_types::{Message, MessageId, GrantId, ThreadId};
/// let message = Message {
///     id: MessageId::new("msg_123"),
///     grant_id: GrantId::new("grant_123"),
///     thread_id: Some(ThreadId::new("thread_123")),
///     subject: Some("Hello".to_string()),
///     from: vec![],
///     to: vec![],
///     cc: vec![],
///     bcc: vec![],
///     reply_to: vec![],
///     date: 1234567890,
///     unread: Some(false),
///     starred: Some(false),
///     snippet: Some("Message snippet...".to_string()),
///     body: Some("Message body".to_string()),
///     attachments: vec![],
///     folders: vec![],
///     created_at: Some(1234567890),
/// };
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Message {
    /// Unique identifier for the message.
    pub id: MessageId,

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

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

    /// Subject line of the message.
    #[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>,

    /// Unix timestamp when the message was sent/received.
    pub date: i64,

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

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

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

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

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

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

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

/// Query parameters for listing messages.
///
/// # Example
///
/// ```
/// # use nylas_types::MessageQueryParams;
/// let params = MessageQueryParams::builder()
///     .subject("invoice")
///     .unread(true)
///     .limit(50)
///     .build();
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub struct MessageQueryParams {
    /// Filter by subject (substring match).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subject: Option<String>,

    /// Filter by sender email.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from: Option<String>,

    /// Filter by recipient email (to field).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to: Option<String>,

    /// Filter by CC recipient email.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cc: Option<String>,

    /// Filter by BCC recipient email.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bcc: Option<String>,

    /// Filter by any email (to, from, cc, bcc).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub any_email: Option<String>,

    /// Filter by thread ID.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thread_id: Option<String>,

    /// Filter by unread status.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unread: Option<bool>,

    /// Filter by starred status.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub starred: Option<bool>,

    /// Filter by messages with attachments.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub has_attachment: Option<bool>,

    /// Filter messages received before this timestamp.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub received_before: Option<i64>,

    /// Filter messages received after this timestamp.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub received_after: Option<i64>,

    /// Filter by folder ID.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub in_: Option<String>,

    /// Maximum number of results to return.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,

    /// Page token for pagination.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_token: Option<String>,
}

impl MessageQueryParams {
    /// Create a new builder for message query parameters.
    pub fn builder() -> MessageQueryParamsBuilder {
        MessageQueryParamsBuilder::default()
    }
}

/// Builder for message query parameters.
#[derive(Debug, Clone, Default)]
pub struct MessageQueryParamsBuilder {
    params: MessageQueryParams,
}

impl MessageQueryParamsBuilder {
    /// Filter by subject (substring match).
    pub fn subject(mut self, subject: impl Into<String>) -> Self {
        self.params.subject = Some(subject.into());
        self
    }

    /// Filter by sender email.
    pub fn from(mut self, from: impl Into<String>) -> Self {
        self.params.from = Some(from.into());
        self
    }

    /// Filter by recipient email (to field).
    pub fn to(mut self, to: impl Into<String>) -> Self {
        self.params.to = Some(to.into());
        self
    }

    /// Filter by CC recipient email.
    pub fn cc(mut self, cc: impl Into<String>) -> Self {
        self.params.cc = Some(cc.into());
        self
    }

    /// Filter by BCC recipient email.
    pub fn bcc(mut self, bcc: impl Into<String>) -> Self {
        self.params.bcc = Some(bcc.into());
        self
    }

    /// Filter by any email (to, from, cc, bcc).
    pub fn any_email(mut self, email: impl Into<String>) -> Self {
        self.params.any_email = Some(email.into());
        self
    }

    /// Filter by thread ID.
    pub fn thread_id(mut self, thread_id: impl Into<String>) -> Self {
        self.params.thread_id = Some(thread_id.into());
        self
    }

    /// Filter by unread status.
    pub fn unread(mut self, unread: bool) -> Self {
        self.params.unread = Some(unread);
        self
    }

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

    /// Filter by messages with attachments.
    pub fn has_attachment(mut self, has_attachment: bool) -> Self {
        self.params.has_attachment = Some(has_attachment);
        self
    }

    /// Filter messages received before this timestamp.
    pub fn received_before(mut self, timestamp: i64) -> Self {
        self.params.received_before = Some(timestamp);
        self
    }

    /// Filter messages received after this timestamp.
    pub fn received_after(mut self, timestamp: i64) -> Self {
        self.params.received_after = Some(timestamp);
        self
    }

    /// Filter by folder ID.
    pub fn in_folder(mut self, folder_id: impl Into<String>) -> Self {
        self.params.in_ = Some(folder_id.into());
        self
    }

    /// Maximum number of results to return.
    pub fn limit(mut self, limit: u32) -> Self {
        self.params.limit = Some(limit);
        self
    }

    /// Page token for pagination.
    pub fn page_token(mut self, token: impl Into<String>) -> Self {
        self.params.page_token = Some(token.into());
        self
    }

    /// Build the query parameters.
    pub fn build(self) -> MessageQueryParams {
        self.params
    }
}

/// Request to update a message.
///
/// # Example
///
/// ```
/// # use nylas_types::UpdateMessageRequest;
/// let update = UpdateMessageRequest::builder()
///     .unread(false)
///     .starred(true)
///     .build();
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct UpdateMessageRequest {
    /// Mark message as read/unread.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unread: Option<bool>,

    /// Mark message as starred/unstarred.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub starred: Option<bool>,

    /// Update folders the message belongs to.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub folders: Option<Vec<String>>,
}

impl UpdateMessageRequest {
    /// Create a new builder for update message request.
    pub fn builder() -> UpdateMessageRequestBuilder {
        UpdateMessageRequestBuilder::default()
    }
}

/// Builder for update message request.
#[derive(Debug, Clone, Default)]
pub struct UpdateMessageRequestBuilder {
    request: UpdateMessageRequest,
}

impl UpdateMessageRequestBuilder {
    /// Mark message as read/unread.
    pub fn unread(mut self, unread: bool) -> Self {
        self.request.unread = Some(unread);
        self
    }

    /// Mark message as starred/unstarred.
    pub fn starred(mut self, starred: bool) -> Self {
        self.request.starred = Some(starred);
        self
    }

    /// Update folders the message belongs to.
    pub fn folders(mut self, folders: Vec<String>) -> Self {
        self.request.folders = Some(folders);
        self
    }

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

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

    #[test]
    fn test_message_creation() {
        let message = Message {
            id: MessageId::new("msg_123"),
            grant_id: GrantId::new("grant_123"),
            thread_id: Some(ThreadId::new("thread_123")),
            subject: Some("Test Subject".to_string()),
            from: vec![],
            to: vec![],
            cc: vec![],
            bcc: vec![],
            reply_to: vec![],
            date: 1234567890,
            unread: Some(true),
            starred: Some(false),
            snippet: Some("Test snippet".to_string()),
            body: Some("Test body".to_string()),
            attachments: vec![],
            folders: vec![],
            created_at: Some(1234567890),
        };

        assert_eq!(message.id.as_str(), "msg_123");
        assert_eq!(message.grant_id.as_str(), "grant_123");
        assert_eq!(message.subject, Some("Test Subject".to_string()));
        assert_eq!(message.unread, Some(true));
    }

    #[test]
    fn test_message_serialization() {
        let message = Message {
            id: MessageId::new("msg_123"),
            grant_id: GrantId::new("grant_123"),
            thread_id: Some(ThreadId::new("thread_123")),
            subject: Some("Test".to_string()),
            from: vec![],
            to: vec![],
            cc: vec![],
            bcc: vec![],
            reply_to: vec![],
            date: 1234567890,
            unread: Some(true),
            starred: Some(false),
            snippet: Some("Snippet".to_string()),
            body: Some("Body".to_string()),
            attachments: vec![],
            folders: vec![],
            created_at: Some(1234567890),
        };

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

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

    #[test]
    fn test_query_params_builder() {
        let params = MessageQueryParams::builder()
            .subject("invoice")
            .unread(true)
            .limit(50)
            .build();

        assert_eq!(params.subject, Some("invoice".to_string()));
        assert_eq!(params.unread, Some(true));
        assert_eq!(params.limit, Some(50));
    }

    #[test]
    fn test_query_params_all_fields() {
        let params = MessageQueryParams::builder()
            .subject("test")
            .from("sender@example.com")
            .to("recipient@example.com")
            .cc("cc@example.com")
            .bcc("bcc@example.com")
            .any_email("any@example.com")
            .thread_id("thread_123")
            .unread(true)
            .starred(false)
            .has_attachment(true)
            .received_before(2000000000)
            .received_after(1000000000)
            .in_folder("folder_123")
            .limit(100)
            .page_token("token_abc")
            .build();

        assert_eq!(params.subject, Some("test".to_string()));
        assert_eq!(params.from, Some("sender@example.com".to_string()));
        assert_eq!(params.to, Some("recipient@example.com".to_string()));
        assert_eq!(params.limit, Some(100));
        assert_eq!(params.page_token, Some("token_abc".to_string()));
    }

    #[test]
    fn test_query_params_serialization() {
        let params = MessageQueryParams::builder()
            .subject("test")
            .unread(true)
            .limit(50)
            .build();

        let json = serde_json::to_string(&params).unwrap();
        assert!(json.contains("test"));
        assert!(json.contains("true"));
        assert!(json.contains("50"));
    }

    #[test]
    fn test_update_message_request_builder() {
        let update = UpdateMessageRequest::builder()
            .unread(false)
            .starred(true)
            .build();

        assert_eq!(update.unread, Some(false));
        assert_eq!(update.starred, Some(true));
    }

    #[test]
    fn test_update_message_request_with_folders() {
        let update = UpdateMessageRequest::builder()
            .unread(false)
            .folders(vec!["folder1".to_string(), "folder2".to_string()])
            .build();

        assert_eq!(update.unread, Some(false));
        assert_eq!(
            update.folders,
            Some(vec!["folder1".to_string(), "folder2".to_string()])
        );
    }

    #[test]
    fn test_update_message_request_serialization() {
        let update = UpdateMessageRequest::builder()
            .unread(false)
            .starred(true)
            .build();

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