Skip to main content

io_gmail/v1/rest/settings/send_as/
list.rs

1//! List the Gmail send-as aliases (`users.settings.sendAs.list`).
2//!
3//! <https://developers.google.com/gmail/api/reference/rest/v1/users.settings.sendAs/list>
4
5use alloc::{format, 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        rest::settings::send_as::GmailSendAs,
17        send::{GMAIL_API_BASE, GmailSend, GmailSendError, GmailSendOutput},
18    },
19};
20
21/// Response wrapping the send-as aliases of a Gmail account.
22#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
23#[serde(rename_all = "camelCase")]
24pub struct GmailSendAsListResponse {
25    /// Send-as aliases of the Gmail account.
26    #[serde(default)]
27    pub send_as: Vec<GmailSendAs>,
28}
29
30/// I/O-free coroutine listing the send-as aliases of a Gmail account
31/// (`users.settings.sendAs.list`).
32pub struct GmailSendAsList {
33    send: GmailSend<GmailSendAsListResponse>,
34}
35
36impl GmailSendAsList {
37    /// Builds the `users.settings.sendAs.list` request for the given user.
38    pub fn new(auth: &HttpAuthBearer, user_id: &str) -> Result<Self, GmailSendError> {
39        debug!("prepare gmail send-as aliases listing");
40        trace!("user_id: {user_id:?}");
41
42        let url = Url::parse(GMAIL_API_BASE)?.join(&format!("users/{user_id}/settings/sendAs"))?;
43        let send = GmailSend::get(auth, url);
44
45        Ok(Self { send })
46    }
47}
48
49impl GmailCoroutine for GmailSendAsList {
50    type Yield = GmailYield;
51    type Return = Result<GmailSendOutput<GmailSendAsListResponse>, GmailSendError>;
52
53    fn resume(&mut self, arg: Option<&[u8]>) -> GmailCoroutineState<Self::Yield, Self::Return> {
54        let out = gmail_try!(&mut self.send, arg);
55        debug!("send-as aliases listed");
56        trace!("out: {out:?}");
57        GmailCoroutineState::Complete(Ok(out))
58    }
59}