Skip to main content

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

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