io_gmail/v1/rest/labels/
update.rs1use 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::GmailLabel,
16 send::{GMAIL_API_BASE, GmailSend, GmailSendError, GmailSendOutput},
17 },
18};
19
20pub struct GmailLabelUpdate {
22 send: GmailSend<GmailLabel>,
23}
24
25impl GmailLabelUpdate {
26 pub fn new(
29 auth: &HttpAuthBearer,
30 user_id: &str,
31 label: &GmailLabel,
32 ) -> Result<Self, GmailSendError> {
33 debug!("prepare gmail label update");
34 trace!("label: {label:?}");
35
36 if label.name.trim().is_empty() {
37 let err = GmailSendError::InvalidRequest("Label name cannot be empty".into());
38 return Err(err);
39 }
40
41 let id = &label.id;
42 let url = Url::parse(GMAIL_API_BASE)?.join(&format!("users/{user_id}/labels/{id}"))?;
43 let send = GmailSend::put_json(auth, url, label)?;
44
45 Ok(Self { send })
46 }
47}
48
49impl GmailCoroutine for GmailLabelUpdate {
50 type Yield = GmailYield;
51 type Return = Result<GmailSendOutput<GmailLabel>, 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!("label updated");
56 trace!("out: {out:?}");
57 GmailCoroutineState::Complete(Ok(out))
58 }
59}