Skip to main content

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

1//! Create a Microsoft Graph contact folder
2//! (`POST /me/contactFolders`).
3//!
4//! <https://learn.microsoft.com/en-us/graph/api/user-post-contactfolders>
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::contact_folders::MsgraphContactFolder,
17        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
18    },
19};
20
21/// Creates a Microsoft Graph contact folder.
22pub struct MsgraphContactFolderCreate {
23    send: MsgraphSend<MsgraphContactFolder>,
24}
25
26impl MsgraphContactFolderCreate {
27    /// Creates `folder`, whose `display_name` must not be empty.
28    pub fn new(
29        auth: &HttpAuthBearer,
30        user_id: &str,
31        folder: &MsgraphContactFolder,
32    ) -> Result<Self, MsgraphSendError> {
33        debug!("prepare microsoft graph contact folder for creation");
34        trace!("folder: {folder:?}");
35
36        if folder.display_name.trim().is_empty() {
37            let err =
38                MsgraphSendError::InvalidRequest("Contact folder name cannot be empty".into());
39            return Err(err);
40        }
41
42        let user = user_path(user_id);
43        let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/contactFolders"))?;
44        let send = MsgraphSend::post_json(auth, url, folder)?;
45
46        Ok(Self { send })
47    }
48}
49
50impl MsgraphCoroutine for MsgraphContactFolderCreate {
51    type Yield = MsgraphYield;
52    type Return = Result<MsgraphSendOutput<MsgraphContactFolder>, 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!("contact folder created");
57        trace!("out: {out:?}");
58        MsgraphCoroutineState::Complete(Ok(out))
59    }
60}