Skip to main content

io_gmail/v1/rest/drafts/
list.rs

1//! List the Gmail drafts (`users.drafts.list`).
2//!
3//! <https://developers.google.com/gmail/api/reference/rest/v1/users.drafts/list>
4
5use alloc::{format, string::String, vec::Vec};
6
7use io_http::rfc6750::bearer::HttpAuthBearer;
8use log::{debug, trace};
9use serde::{Deserialize, Serialize};
10use url::Url;
11
12use crate::{
13    coroutine::*,
14    gmail_try,
15    v1::{
16        query::{is_false, to_query_pairs},
17        rest::drafts::GmailDraft,
18        send::{GMAIL_API_BASE, GmailSend, GmailSendError, GmailSendOutput},
19    },
20};
21
22/// Query parameters for listing drafts (`users.drafts.list`).
23#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
24#[serde(rename_all = "camelCase")]
25pub struct GmailDraftsListParams<'a> {
26    /// Search query filtering drafts, using the Gmail search box syntax.
27    pub q: Option<&'a str>,
28    /// Maximum number of drafts to return per page.
29    pub max_results: Option<u32>,
30    /// Page token from a previous listing response.
31    pub page_token: Option<&'a str>,
32    /// Whether to include drafts from SPAM and TRASH.
33    #[serde(skip_serializing_if = "is_false")]
34    pub include_spam_trash: bool,
35}
36
37/// Response returned when listing drafts (`users.drafts.list`).
38#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
39#[serde(rename_all = "camelCase")]
40pub struct GmailDraftsListResponse {
41    /// Drafts of the current page.
42    #[serde(default)]
43    pub drafts: Vec<GmailDraft>,
44    /// Token to fetch the next page, absent on the last page.
45    #[serde(default)]
46    pub next_page_token: Option<String>,
47    /// Estimated total number of results.
48    #[serde(default)]
49    pub result_size_estimate: Option<u64>,
50}
51
52/// I/O-free coroutine listing Gmail drafts (`users.drafts.list`).
53pub struct GmailDraftsList {
54    send: GmailSend<GmailDraftsListResponse>,
55}
56
57impl GmailDraftsList {
58    /// Builds the `users.drafts.list` request from the given query
59    /// parameters; `user_id` is the mailbox owner (usually `me`).
60    pub fn new(
61        auth: &HttpAuthBearer,
62        user_id: &str,
63        params: &GmailDraftsListParams,
64    ) -> Result<Self, GmailSendError> {
65        debug!("prepare gmail drafts listing");
66        trace!("params: {params:?}");
67
68        let mut url = Url::parse(GMAIL_API_BASE)?.join(&format!("users/{user_id}/drafts"))?;
69        url.query_pairs_mut().extend_pairs(to_query_pairs(params));
70
71        let send = GmailSend::get(auth, url);
72
73        Ok(Self { send })
74    }
75}
76
77impl GmailCoroutine for GmailDraftsList {
78    type Yield = GmailYield;
79    type Return = Result<GmailSendOutput<GmailDraftsListResponse>, GmailSendError>;
80
81    fn resume(&mut self, arg: Option<&[u8]>) -> GmailCoroutineState<Self::Yield, Self::Return> {
82        let out = gmail_try!(&mut self.send, arg);
83        debug!("drafts listed");
84        trace!("out: {out:?}");
85        GmailCoroutineState::Complete(Ok(out))
86    }
87}