Skip to main content

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

1//! Create a Microsoft Graph mail folder (`POST /me/mailFolders`).
2//!
3//! <https://learn.microsoft.com/en-us/graph/api/user-post-mailfolders>
4
5use alloc::format;
6
7use io_http::rfc6750::bearer::HttpAuthBearer;
8use log::{debug, trace};
9use url::Url;
10
11use crate::{
12    coroutine::*,
13    msgraph_try,
14    v1::{
15        rest::users::mail_folders::MsgraphMailFolder,
16        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
17    },
18};
19
20/// Creates a Microsoft Graph mail folder.
21pub struct MsgraphMailFolderCreate {
22    send: MsgraphSend<MsgraphMailFolder>,
23}
24
25impl MsgraphMailFolderCreate {
26    /// Creates `folder`, whose `display_name` must not be empty.
27    pub fn new(
28        auth: &HttpAuthBearer,
29        user_id: &str,
30        folder: &MsgraphMailFolder,
31    ) -> Result<Self, MsgraphSendError> {
32        debug!("prepare microsoft graph mail folder for creation");
33        trace!("folder: {folder:?}");
34
35        if folder.display_name.trim().is_empty() {
36            let err = MsgraphSendError::InvalidRequest("Mail folder name cannot be empty".into());
37            return Err(err);
38        }
39
40        let user = user_path(user_id);
41        let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/mailFolders"))?;
42        let send = MsgraphSend::post_json(auth, url, folder)?;
43
44        Ok(Self { send })
45    }
46}
47
48impl MsgraphCoroutine for MsgraphMailFolderCreate {
49    type Yield = MsgraphYield;
50    type Return = Result<MsgraphSendOutput<MsgraphMailFolder>, MsgraphSendError>;
51
52    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
53        let out = msgraph_try!(&mut self.send, arg);
54        debug!("mail folder created");
55        trace!("out: {out:?}");
56        MsgraphCoroutineState::Complete(Ok(out))
57    }
58}