Skip to main content

io_gmail/v1/rest/labels/
list.rs

1//! List the Gmail labels (`users.labels.list`).
2//!
3//! <https://developers.google.com/gmail/api/reference/rest/v1/users.labels/list>
4
5use alloc::format;
6
7use io_http::rfc6750::bearer::HttpAuthBearer;
8use log::{debug, trace};
9use url::Url;
10
11use crate::{
12    coroutine::*,
13    gmail_try,
14    v1::{
15        rest::labels::GmailLabelsListResponse,
16        send::{GMAIL_API_BASE, GmailSend, GmailSendError, GmailSendOutput},
17    },
18};
19
20/// I/O-free coroutine listing the Gmail labels (`users.labels.list`).
21pub struct GmailLabelsList {
22    send: GmailSend<GmailLabelsListResponse>,
23}
24
25impl GmailLabelsList {
26    /// Builds the `users.labels.list` request for the given user id
27    /// (the mailbox owner, usually `me`).
28    pub fn new(auth: &HttpAuthBearer, user_id: &str) -> Result<Self, GmailSendError> {
29        debug!("prepare gmail labels listing");
30        trace!("user_id: {user_id:?}");
31
32        let url = Url::parse(GMAIL_API_BASE)?.join(&format!("users/{user_id}/labels"))?;
33        let send = GmailSend::get(auth, url);
34
35        Ok(Self { send })
36    }
37}
38
39impl GmailCoroutine for GmailLabelsList {
40    type Yield = GmailYield;
41    type Return = Result<GmailSendOutput<GmailLabelsListResponse>, GmailSendError>;
42
43    fn resume(&mut self, arg: Option<&[u8]>) -> GmailCoroutineState<Self::Yield, Self::Return> {
44        let out = gmail_try!(&mut self.send, arg);
45        debug!("labels listed");
46        trace!("out: {out:?}");
47        GmailCoroutineState::Complete(Ok(out))
48    }
49}