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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//! Event types for the Nylas API v3.

use serde::{Deserialize, Serialize};

use crate::{CalendarId, EmailAddress, EventId, GrantId};

/// An event object from the Nylas API.
///
/// Events represent calendar events from various providers (Google, Microsoft, etc.).
///
/// # Example
///
/// ```
/// # use nylas_types::{Event, EventId, GrantId, CalendarId, When, EventStatus};
/// let event = Event {
///     id: EventId::new("event_123"),
///     grant_id: GrantId::new("grant_123"),
///     calendar_id: CalendarId::new("cal_123"),
///     title: Some("Team Meeting".to_string()),
///     description: Some("Weekly team sync".to_string()),
///     when: When::Timespan {
///         start_time: 1234567890,
///         end_time: 1234571490,
///         start_timezone: Some("America/New_York".to_string()),
///         end_timezone: Some("America/New_York".to_string()),
///     },
///     location: Some("Conference Room A".to_string()),
///     busy: true,
///     status: EventStatus::Confirmed,
///     participants: vec![],
///     organizer: None,
///     creator: None,
///     conferencing: None,
///     recurrence: None,
///     reminders: None,
///     capacity: None,
///     hide_participants: false,
///     read_only: false,
///     html_link: None,
///     ical_uid: None,
///     resources: vec![],
///     visibility: None,
///     object: "event".to_string(),
///     metadata: None,
///     created_at: Some(1234567890),
///     updated_at: Some(1234567890),
/// };
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Event {
    /// Unique identifier for the event.
    pub id: EventId,

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

    /// Calendar ID this event belongs to.
    pub calendar_id: CalendarId,

    /// Title of the event.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    /// Description of the event.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// When the event occurs.
    pub when: When,

    /// Location of the event.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub location: Option<String>,

    /// Whether the event shows as busy.
    #[serde(default = "default_busy")]
    pub busy: bool,

    /// Status of the event.
    #[serde(default)]
    pub status: EventStatus,

    /// List of participants.
    #[serde(default)]
    pub participants: Vec<Participant>,

    /// Organizer of the event.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub organizer: Option<EmailAddress>,

    /// Creator of the event.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub creator: Option<EmailAddress>,

    /// Conference details.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub conferencing: Option<Conferencing>,

    /// Recurrence rules for repeating events.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub recurrence: Option<Recurrence>,

    /// Reminder settings.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reminders: Option<Reminders>,

    /// Maximum capacity for the event.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub capacity: Option<i32>,

    /// Whether to hide participants from each other.
    #[serde(default)]
    pub hide_participants: bool,

    /// Whether the event is read-only.
    #[serde(default)]
    pub read_only: bool,

    /// Link to view the event in the provider's interface.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub html_link: Option<String>,

    /// iCalendar UID for the event.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ical_uid: Option<String>,

    /// Resources (rooms, equipment) booked for the event.
    #[serde(default)]
    pub resources: Vec<Resource>,

    /// Visibility of the event (default, public, private).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub visibility: Option<EventVisibility>,

    /// Object type identifier (always "event").
    #[serde(default = "default_event_object_type")]
    pub object: String,

    /// Optional metadata for the event.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<serde_json::Value>,

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

    /// Unix timestamp when the event was last updated.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<i64>,
}

fn default_busy() -> bool {
    true
}

fn default_event_object_type() -> String {
    "event".to_string()
}

/// When the event occurs.
///
/// This can be a specific timespan, an all-day event, or a datespan.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "object", rename_all = "lowercase")]
pub enum When {
    /// A specific time period with start and end times.
    Timespan {
        /// Unix timestamp for the start time.
        start_time: i64,

        /// Unix timestamp for the end time.
        end_time: i64,

        /// Timezone for the start time (IANA format).
        #[serde(skip_serializing_if = "Option::is_none")]
        start_timezone: Option<String>,

        /// Timezone for the end time (IANA format).
        #[serde(skip_serializing_if = "Option::is_none")]
        end_timezone: Option<String>,
    },

    /// An all-day event spanning multiple days.
    Datespan {
        /// Start date in YYYY-MM-DD format.
        start_date: String,

        /// End date in YYYY-MM-DD format.
        end_date: String,
    },

    /// A single all-day event.
    Date {
        /// Date in YYYY-MM-DD format.
        date: String,
    },
}

/// Event status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum EventStatus {
    /// Event is confirmed.
    #[default]
    Confirmed,

    /// Event is tentative.
    Tentative,

    /// Event is cancelled.
    Cancelled,
}

/// Event visibility.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum EventVisibility {
    /// Default visibility.
    Default,

    /// Public event.
    Public,

    /// Private event.
    Private,
}

/// A participant in an event.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Participant {
    /// Email address of the participant.
    pub email: String,

    /// Name of the participant.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// RSVP status of the participant.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<ParticipantStatus>,

    /// Comment from the participant.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,

    /// Phone number (Microsoft Graph only).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub phone_number: Option<String>,
}

impl Participant {
    /// Create a new participant with an email address.
    pub fn new(email: impl Into<String>) -> Self {
        Self {
            email: email.into(),
            name: None,
            status: None,
            comment: None,
            phone_number: None,
        }
    }

    /// Set the participant's name.
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Set the participant's RSVP status.
    pub fn status(mut self, status: ParticipantStatus) -> Self {
        self.status = Some(status);
        self
    }
}

/// Participant RSVP status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ParticipantStatus {
    /// Accepted.
    Yes,

    /// Declined.
    No,

    /// Maybe attending.
    Maybe,

    /// No reply yet.
    Noreply,
}

/// Conference details for an event.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Conferencing {
    /// Conferencing provider (e.g., "Google Meet", "Microsoft Teams", "Zoom Meeting").
    #[serde(skip_serializing_if = "Option::is_none")]
    pub provider: Option<String>,

    /// Conference details.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<ConferencingDetails>,
}

/// Details for a conference.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ConferencingDetails {
    /// URL for the virtual meeting.
    pub url: String,

    /// Meeting code.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meeting_code: Option<String>,

    /// Password for the meeting.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub password: Option<String>,

    /// PIN for the meeting.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pin: Option<String>,

    /// Dial-in phone numbers.
    #[serde(default)]
    pub phone: Vec<String>,
}

/// Recurrence rules for repeating events.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Recurrence {
    /// Recurrence rule in RRULE format (e.g., "FREQ=WEEKLY;BYDAY=MO,WE,FR").
    #[serde(rename = "rrule")]
    pub rrule: Vec<String>,

    /// Exception dates in EXDATE format (e.g., "20240101T120000Z").
    #[serde(default)]
    pub exdate: Vec<String>,
}

/// Reminder settings for an event.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Reminders {
    /// Whether to use default calendar reminders.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub use_default: Option<bool>,

    /// Custom reminder overrides.
    #[serde(default)]
    pub overrides: Vec<ReminderOverride>,
}

/// A custom reminder override.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReminderOverride {
    /// Number of minutes before the event to trigger the reminder.
    pub reminder_minutes: i32,

    /// Reminder method (email, popup, display, sound).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reminder_method: Option<String>,
}

/// A resource (room, equipment) for an event.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Resource {
    /// Email address of the resource.
    pub email: String,

    /// Name of the resource.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// Resource type.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub object: Option<String>,
}

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

    #[test]
    fn test_event_creation() {
        let event = Event {
            id: EventId::new("event_123"),
            grant_id: GrantId::new("grant_123"),
            calendar_id: CalendarId::new("cal_123"),
            title: Some("Test Event".to_string()),
            description: None,
            when: When::Timespan {
                start_time: 1234567890,
                end_time: 1234571490,
                start_timezone: Some("UTC".to_string()),
                end_timezone: Some("UTC".to_string()),
            },
            location: None,
            busy: true,
            status: EventStatus::Confirmed,
            participants: vec![],
            organizer: None,
            creator: None,
            conferencing: None,
            recurrence: None,
            reminders: None,
            capacity: None,
            hide_participants: false,
            read_only: false,
            html_link: None,
            ical_uid: None,
            resources: vec![],
            visibility: None,
            object: "event".to_string(),
            metadata: None,
            created_at: None,
            updated_at: None,
        };

        assert_eq!(event.title, Some("Test Event".to_string()));
        assert!(event.busy);
        assert_eq!(event.status, EventStatus::Confirmed);
    }

    #[test]
    fn test_when_timespan_serialization() {
        let when = When::Timespan {
            start_time: 1234567890,
            end_time: 1234571490,
            start_timezone: Some("America/New_York".to_string()),
            end_timezone: Some("America/New_York".to_string()),
        };

        let json = serde_json::to_string(&when).unwrap();
        assert!(json.contains("timespan"));
        assert!(json.contains("1234567890"));
        assert!(json.contains("America/New_York"));

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

    #[test]
    fn test_when_datespan_serialization() {
        let when = When::Datespan {
            start_date: "2024-01-01".to_string(),
            end_date: "2024-01-03".to_string(),
        };

        let json = serde_json::to_string(&when).unwrap();
        assert!(json.contains("datespan"));
        assert!(json.contains("2024-01-01"));
        assert!(json.contains("2024-01-03"));

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

    #[test]
    fn test_when_date_serialization() {
        let when = When::Date {
            date: "2024-01-15".to_string(),
        };

        let json = serde_json::to_string(&when).unwrap();
        assert!(json.contains("date"));
        assert!(json.contains("2024-01-15"));

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

    #[test]
    fn test_participant_builder() {
        let participant = Participant::new("user@example.com")
            .name("John Doe")
            .status(ParticipantStatus::Yes);

        assert_eq!(participant.email, "user@example.com");
        assert_eq!(participant.name, Some("John Doe".to_string()));
        assert_eq!(participant.status, Some(ParticipantStatus::Yes));
    }

    #[test]
    fn test_participant_status_serialization() {
        let status = ParticipantStatus::Yes;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, "\"yes\"");

        let status = ParticipantStatus::Maybe;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, "\"maybe\"");
    }

    #[test]
    fn test_event_status_serialization() {
        let status = EventStatus::Confirmed;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, "\"confirmed\"");

        let status = EventStatus::Cancelled;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, "\"cancelled\"");
    }

    #[test]
    fn test_conferencing_serialization() {
        let conferencing = Conferencing {
            provider: Some("Google Meet".to_string()),
            details: Some(ConferencingDetails {
                url: "https://meet.google.com/abc-def-ghi".to_string(),
                meeting_code: Some("abc-def-ghi".to_string()),
                password: None,
                pin: None,
                phone: vec![],
            }),
        };

        let json = serde_json::to_string(&conferencing).unwrap();
        assert!(json.contains("Google Meet"));
        assert!(json.contains("meet.google.com"));

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