agentmail-rs 0.4.1

Unofficial typed Rust client for AgentMail (agentmail.to), the email API for agents.
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
use serde::{Deserialize, Serialize};

use crate::util::QueryBuilder;

/// An inbox's calendar, from `get_calendar` / `update_calendar`.
#[derive(Clone, Debug, Deserialize)]
pub struct Calendar {
    /// The inbox the calendar belongs to.
    pub inbox_id: String,
    /// IANA timezone the calendar renders in, e.g. `America/New_York`.
    pub timezone: String,
    /// Number of events on the calendar.
    pub event_count: u64,
    /// Creation timestamp (RFC 3339).
    pub created_at: String,
    /// Last-update timestamp (RFC 3339).
    pub updated_at: String,
    /// Opaque revision token; pass it as `etag` on writes to reject
    /// concurrent modifications (428 otherwise).
    pub etag: String,
}

/// Whether an event occurs once or as part of a series.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum EventKind {
    /// A one-off event.
    Single,
    /// The defining event of a recurring series.
    Series,
    /// One materialized occurrence of a series.
    Instance,
    /// A kind this client version does not recognize.
    #[serde(other)]
    Unknown,
}

/// Confirmation state of an event.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum EventStatus {
    /// The event is on as scheduled.
    Confirmed,
    /// The event is provisional.
    Tentative,
    /// The event was cancelled.
    Cancelled,
    /// A status this client version does not recognize.
    #[serde(other)]
    Unknown,
}

/// How an event's `duration_value` is interpreted.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DurationMode {
    /// `duration_value` counts nominal calendar units (a `P1D` day stays a day
    /// across DST shifts).
    Nominal,
    /// `duration_value` counts exact seconds.
    Exact,
    /// A mode this client version does not recognize.
    #[serde(other)]
    Unknown,
}

/// Where an event came from.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum EventSource {
    /// Created through the API.
    Api,
    /// Parsed out of an emailed invite.
    Email,
    /// A source this client version does not recognize.
    #[serde(other)]
    Unknown,
}

/// The organizer's response state on an event the inbox was invited to.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ResponseStatus {
    /// No reply yet.
    NeedsAction,
    /// Going.
    Accepted,
    /// Not going.
    Declined,
    /// Maybe.
    Tentative,
    /// A status this client version does not recognize.
    #[serde(other)]
    Unknown,
}

/// An attendee on an event.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct CalendarAttendee {
    /// The attendee's email.
    pub email: String,
    /// Display name, when known.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// The attendee's reply state.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    /// `required` or `optional`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
    /// The attendee's reply comment, when they left one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
    /// When the attendee last replied (RFC 3339).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub responded_at: Option<String>,
}

/// Recurrence rule and exceptions on an event. `rdates` entries are either an
/// RFC 3339 timestamp or a `{start, end?, duration?}` object, so they are kept
/// as raw JSON.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Recurrence {
    /// RFC 5545 RRULE, e.g. `FREQ=WEEKLY;BYDAY=MO,WE`.
    pub rule: String,
    /// Occurrence start timestamps to exclude (RFC 3339).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub exdates: Vec<String>,
    /// Extra occurrences to add: RFC 3339 strings or `{start, end?, duration?}`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub rdates: Vec<serde_json::Value>,
    /// Cutoff after which the series stops recurring (RFC 3339).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub truncate_before: Option<String>,
}

/// A calendar event, as the API returns it.
#[derive(Clone, Debug, Deserialize)]
pub struct CalendarEvent {
    /// Unique event id.
    pub event_id: String,
    /// Whether this is a single event, a series definition, or an instance.
    #[serde(default)]
    pub kind: Option<EventKind>,
    /// The series this instance belongs to, when applicable.
    #[serde(default)]
    pub series_id: Option<String>,
    /// The series' start this instance shifted from, when moved (RFC 3339).
    #[serde(default)]
    pub original_start: Option<String>,
    /// Same as [`CalendarEvent::original_start`], on `instance` events.
    #[serde(default)]
    pub original_start_at: Option<String>,
    /// Whether this instance deviates from the series.
    #[serde(default)]
    pub is_exception: Option<bool>,
    /// Event title.
    pub title: String,
    /// Long description.
    #[serde(default)]
    pub description: Option<String>,
    /// Location, when set.
    #[serde(default)]
    pub location: Option<String>,
    /// Your own JSON stored alongside the event.
    #[serde(default)]
    pub metadata: Option<serde_json::Value>,
    /// Confirmation state.
    #[serde(default)]
    pub status: Option<EventStatus>,
    /// Whether the event lasts all day (then `start`/`end` are dates).
    pub all_day: bool,
    /// Display start (RFC 3339, or a date for all-day events).
    pub start: String,
    /// Display end.
    pub end: String,
    /// IANA timezone the event renders in.
    pub timezone: String,
    /// How `duration_value` is interpreted.
    #[serde(default)]
    pub duration_mode: Option<DurationMode>,
    /// Length in units picked by `duration_mode`.
    pub duration_value: i64,
    /// Absolute start (RFC 3339).
    pub start_at: String,
    /// Absolute end (RFC 3339).
    pub end_at: String,
    /// Recurrence rule, when the event repeats.
    #[serde(default)]
    pub recurrence: Option<Recurrence>,
    /// Invitees and their replies.
    #[serde(default)]
    pub attendees: Vec<CalendarAttendee>,
    /// Number of attendees.
    pub attendee_count: u32,
    /// The inbox's own reply state, when it was invited.
    #[serde(default)]
    pub response_status: Option<ResponseStatus>,
    /// iCalendar UID, stable across the series.
    pub uid: String,
    /// iCalendar sequence number, bumped on material changes.
    pub sequence: u32,
    /// Whether the event came from the API or an email.
    #[serde(default)]
    pub source: Option<EventSource>,
    /// Organizer address, when known.
    #[serde(default)]
    pub organizer_email: Option<String>,
    /// The email message the invite arrived in, when from email.
    #[serde(default)]
    pub origin_message_id: Option<String>,
    /// Monotonic revision, for optimistic concurrency.
    #[serde(default)]
    pub resource_revision: Option<u64>,
    /// Opaque revision token for `If-Match` writes, when returned.
    #[serde(default)]
    pub etag: Option<String>,
    /// Creation timestamp (RFC 3339).
    pub created_at: String,
    /// Last-update timestamp (RFC 3339).
    pub updated_at: String,
}

/// Read-staleness control for calendar reads. `primary` (the API default)
/// reads the authoritative store; `eventual` may serve a replica.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Consistency {
    /// May read a replica; cheaper and usually fine.
    Eventual,
    /// Always read the primary store.
    Primary,
}

impl Consistency {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Consistency::Eventual => "eventual",
            Consistency::Primary => "primary",
        }
    }
}

/// Which occurrences a series mutation applies to.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MutationMode {
    /// Only the addressed instance.
    Single,
    /// The addressed instance and every later one.
    Future,
}

impl MutationMode {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            MutationMode::Single => "single",
            MutationMode::Future => "future",
        }
    }
}

/// Windowing and pagination for `get_agenda` and `list_event_instances`.
#[derive(Clone, Debug, Default)]
pub struct CalendarEventsQuery {
    /// Only occurrences starting at or after this instant (RFC 3339).
    pub after: Option<String>,
    /// Only occurrences starting before this instant (RFC 3339).
    pub before: Option<String>,
    /// Also include occurrences overlapping the window.
    pub include_overlapping: Option<bool>,
    /// Maximum occurrences per page.
    pub limit: Option<u32>,
    /// Cursor from a previous response's `next_page_token`.
    pub page_token: Option<String>,
    /// Read-staleness control.
    pub consistency: Option<Consistency>,
}

impl CalendarEventsQuery {
    pub(crate) fn query(&self) -> Vec<(&'static str, String)> {
        QueryBuilder::new()
            .opt("after", self.after.as_ref())
            .opt("before", self.before.as_ref())
            .opt("include_overlapping", self.include_overlapping.as_ref())
            .opt("limit", self.limit.as_ref())
            .opt("page_token", self.page_token.as_ref())
            .opt(
                "consistency",
                self.consistency.map(Consistency::as_str).as_ref(),
            )
            .build()
    }
}

/// Body for `create_calendar_event`. `title`, `start`, and `end` are required
/// by the API.
#[derive(Clone, Debug, Default, Serialize)]
pub struct CreateCalendarEvent {
    /// Your own idempotency/reference id for the event.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub client_id: Option<String>,
    /// Event title.
    pub title: String,
    /// Long description.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Location.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub location: Option<String>,
    /// Your own JSON stored alongside the event.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<serde_json::Value>,
    /// Confirmation state.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<EventStatus>,
    /// All-day event (`start`/`end` become dates).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub all_day: Option<bool>,
    /// Display start (RFC 3339, or a date for all-day events).
    pub start: String,
    /// Display end.
    pub end: String,
    /// IANA timezone; defaults to the calendar's.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timezone: Option<String>,
    /// How to interpret `duration_value` (defaults to the API's).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub duration_mode: Option<DurationMode>,
    /// Recurrence rule, to make the event a series.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub recurrence: Option<Recurrence>,
    /// Invitees.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub attendees: Vec<CalendarAttendee>,
    /// Email invites to the attendees.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub send_invites: Option<bool>,
}

/// Body for `update_calendar_event`. Fields left `None` stay unchanged; there
/// is no way to set a field back to null from this client (the API accepts
/// nulls for `description`/`location`/`metadata`/`recurrence`, but `None`
/// here means "leave alone", matching [`crate::UpdateDraft`]).
#[derive(Clone, Debug, Default, Serialize)]
pub struct UpdateCalendarEvent {
    /// Replace the title.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Replace the description.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Replace the location.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub location: Option<String>,
    /// Replace the stored metadata.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<serde_json::Value>,
    /// Replace the confirmation state.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub status: Option<EventStatus>,
    /// Switch to or from an all-day event.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub all_day: Option<bool>,
    /// Replace the display start.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start: Option<String>,
    /// Replace the display end.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub end: Option<String>,
    /// Replace the IANA timezone.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timezone: Option<String>,
    /// Replace the duration interpretation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub duration_mode: Option<DurationMode>,
    /// Replace the recurrence rule.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub recurrence: Option<Recurrence>,
    /// Replace the attendee list.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub attendees: Vec<CalendarAttendee>,
    /// Email invites to (new) attendees.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub send_invites: Option<bool>,
}

/// Body for `respond_to_calendar_event`: the inbox's reply to an invite.
#[derive(Clone, Debug, Default, Serialize)]
pub struct RespondToCalendarEvent {
    /// The reply: `accepted`, `declined`, or `tentative`.
    pub status: String,
    /// A note sent along with the reply.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
    /// Email the reply to the organizer.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub send_reply: Option<bool>,
}

/// One page of occurrences from `get_agenda` or `list_event_instances`.
#[derive(Clone, Debug, Deserialize)]
pub struct CalendarEventList {
    /// Total occurrences in the window (not just this page).
    pub count: u64,
    /// This page of occurrences.
    #[serde(default)]
    pub events: Vec<CalendarEvent>,
    /// Cursor for the next page; `None` on the last page.
    #[serde(default)]
    pub next_page_token: Option<String>,
}

/// The response to an event create/update: the mutated event plus the
/// operation id (used by the API's event stream to ack the change).
#[derive(Clone, Debug, Deserialize)]
pub struct CalendarEventMutation {
    /// The event after the mutation.
    pub event: CalendarEvent,
    /// Id of the applied operation.
    #[serde(default)]
    pub operation_id: Option<String>,
}

/// The response to `delete_calendar_event`; `event` carries the deleted state
/// when the API returns it.
#[derive(Clone, Debug, Deserialize)]
pub struct DeleteCalendarEventResult {
    /// Id of the deletion operation, when the API issues one.
    #[serde(default)]
    pub deletion_id: Option<String>,
    /// The event as it was, when the API returns it.
    #[serde(default)]
    pub event: Option<CalendarEvent>,
}