Skip to main content

io_gmail/v1/rest/settings/
update_auto_forwarding.rs

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