Skip to main content

io_msgraph/v1/rest/users/mail_folders/
update.rs

1//! Update a Microsoft Graph mail folder (`PATCH /me/mailFolders/{id}`);
2//! used to rename a folder via its `displayName`.
3//!
4//! <https://learn.microsoft.com/en-us/graph/api/mailfolder-update>
5
6use alloc::format;
7
8use io_http::rfc6750::bearer::HttpAuthBearer;
9use log::{debug, trace};
10use url::Url;
11
12use crate::{
13    coroutine::*,
14    msgraph_try,
15    v1::{
16        rest::users::mail_folders::MsgraphMailFolder,
17        send::{
18            MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, path_segment,
19            user_path,
20        },
21    },
22};
23
24/// Updates a Microsoft Graph mail folder.
25pub struct MsgraphMailFolderUpdate {
26    send: MsgraphSend<MsgraphMailFolder>,
27}
28
29impl MsgraphMailFolderUpdate {
30    /// Patches the mail folder `id` with the set fields of `folder`.
31    pub fn new(
32        auth: &HttpAuthBearer,
33        user_id: &str,
34        id: &str,
35        folder: &MsgraphMailFolder,
36    ) -> Result<Self, MsgraphSendError> {
37        debug!("prepare microsoft graph mail folder update");
38        trace!("id: {id:?}");
39        trace!("folder: {folder:?}");
40
41        let user = user_path(user_id)?;
42        let url = Url::parse(MSGRAPH_API_BASE)?
43            .join(&format!("{user}/mailFolders/{}", path_segment(id)?))?;
44        let send = MsgraphSend::patch_json(auth, url, folder)?;
45
46        Ok(Self { send })
47    }
48}
49
50impl MsgraphCoroutine for MsgraphMailFolderUpdate {
51    type Yield = MsgraphYield;
52    type Return = Result<MsgraphSendOutput<MsgraphMailFolder>, MsgraphSendError>;
53
54    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
55        let out = msgraph_try!(&mut self.send, arg);
56        debug!("mail folder updated");
57        trace!("out: {out:?}");
58        MsgraphCoroutineState::Complete(Ok(out))
59    }
60}